Boost C++ Libraries

PrevUpHomeNext

Class template basic_core

boost::log::basic_core — Logging library core class.

Synopsis

// In header: <boost/log/core/core.hpp>

template<typename CharT> 
class basic_core {
public:
  // types
  typedef CharT                                       char_type;               // Character type. 
  typedef basic_record< char_type >                   record_type;             // Log record type. 
  typedef record_type::string_type                    string_type;             // String type to be used as a message text holder. 
  typedef basic_attribute_set< char_type >            attribute_set_type;      // Attribute set type. 
  typedef record_type::values_view_type               values_view_type;        // Attribute values view type. 
  typedef sinks::sink< char_type >                    sink_type;               // Sink interface type. 
  typedef function1< bool, values_view_type const & > filter_type;             // Filter function type. 
  typedef function0< void >                           exception_handler_type;  // Exception handler function type. 

  // construct/copy/destruct
  ~basic_core();

  // public member functions
  bool set_logging_enabled(bool = true);
  void set_filter(filter_type const &);
  void reset_filter();
  void add_sink(shared_ptr< sink_type > const &);
  void remove_sink(shared_ptr< sink_type > const &);
  std::pair< typename attribute_set_type::iterator, bool > 
  add_global_attribute(string_type const &, shared_ptr< attribute > const &);
  void remove_global_attribute(typename attribute_set_type::iterator);
  attribute_set_type get_global_attributes() const;
  void set_global_attributes(attribute_set_type const &);
  std::pair< typename attribute_set_type::iterator, bool > 
  add_thread_attribute(string_type const &, shared_ptr< attribute > const &);
  void remove_thread_attribute(typename attribute_set_type::iterator);
  attribute_set_type get_thread_attributes() const;
  void set_thread_attributes(attribute_set_type const &);
  void set_exception_handler(exception_handler_type const &);
  record_type open_record(attribute_set_type const &);
  void push_record(record_type const &);

  // public static functions
  static shared_ptr< basic_core > get();
};

Description

The logging core is used to interconnect log sources and sinks. It also provides a number of basic features, like global filtering and global and thread-specific attribute storage.

The logging core is a singleton. Users can acquire the core instance by calling the static method get.

basic_core public construct/copy/destruct

  1. ~basic_core();

    Destructor. Destroys the core, releases any sinks and attributes that were registered.

basic_core public member functions

  1. bool set_logging_enabled(bool enabled = true);

    The method enables or disables logging.

    Setting this status to false allows you to completely wipe out any logging activity, including filtering and generation of attribute values. It is useful if you want to completely disable logging in a running application. The state of logging does not alter any other properties of the logging library, such as filters or sinks, so you can enable logging with the very same settings that you had when the logging was disabled. This feature may also be useful if you want to perform major changes to logging configuration and don't want your application to block on opening or pushing a log record.

    By default logging is enabled.

    Parameters:
    enabled

    The actual flag of logging activity.

    Returns:

    The previous value of enabled/disabled logging flag

  2. void set_filter(filter_type const & filter);

    The method sets the global logging filter. The filter is applied to every log record that is processed.

    Parameters:
    filter

    The filter function object to be installed.

  3. void reset_filter();

    The method removes the global logging filter. All log records are passed to sinks without global filtering applied.

  4. void add_sink(shared_ptr< sink_type > const & s);

    The method adds a new sink. The sink is included into logging process immediately after being added and until being removed. No sink can be added more than once at the same time. If the sink is already registered, the call is ignored.

    Parameters:
    s

    The sink to be registered.

  5. void remove_sink(shared_ptr< sink_type > const & s);

    The method removes the sink from the output. The sink will not receive any log records after removal. The call has no effect if the sink is not registered.

    Parameters:
    s

    The sink to be unregistered.

  6. std::pair< typename attribute_set_type::iterator, bool > 
    add_global_attribute(string_type const & name, 
                         shared_ptr< attribute > const & attr);

    The method adds an attribute to the global attribute set. The attribute will be implicitly added to every log record.

    Parameters:
    attr

    Pointer to the attribute. Must not be NULL.

    name

    The attribute name.

    Returns:

    A pair of values. If the second member is true, then the attribute is added and the first member points to the attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents addition.

  7. void remove_global_attribute(typename attribute_set_type::iterator it);

    The method removes an attribute from the global attribute set.

    Parameters:
    it

    Iterator to the previously added attribute.

    Requires:

    The attribute was added with the add_global_attribute call.

    Postconditions:

    The attribute is no longer registered as a global attribute. The iterator is invalidated after removal.

  8. attribute_set_type get_global_attributes() const;

    The method returns a copy of the complete set of currently registered global attributes.

  9. void set_global_attributes(attribute_set_type const & attrs);

    The method replaces the complete set of currently registered global attributes with the provided set.

    [Note] Note

    The method invalidates all iterators and references that may have been returned from the add_global_attribute method.

    Parameters:
    attrs

    The set of attributes to be installed.

  10. std::pair< typename attribute_set_type::iterator, bool > 
    add_thread_attribute(string_type const & name, 
                         shared_ptr< attribute > const & attr);

    The method adds an attribute to the thread-specific attribute set. The attribute will be implicitly added to every log record made in the current thread.

    [Note] Note

    In single-threaded build the effect is the same as adding the attribute globally. This, however, does not imply that iterators to thread-specific and global attributes are interchangable.

    Parameters:
    attr

    Pointer to the attribute. Must not be NULL.

    name

    The attribute name.

    Returns:

    A pair of values. If the second member is true, then the attribute is added and the first member points to the attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents addition.

  11. void remove_thread_attribute(typename attribute_set_type::iterator it);

    The method removes an attribute from the thread-specific attribute set.

    Parameters:
    it

    Iterator to the previously added attribute.

    Requires:

    The attribute was added with the add_thread_attribute call.

    Postconditions:

    The attribute is no longer registered as a thread-specific attribute. The iterator is invalidated after removal.

  12. attribute_set_type get_thread_attributes() const;

    The method returns a copy of the complete set of currently registered thread-specific attributes.

  13. void set_thread_attributes(attribute_set_type const & attrs);

    The method replaces the complete set of currently registered thread-specific attributes with the provided set.

    [Note] Note

    The method invalidates all iterators and references that may have been returned from the add_thread_attribute method.

    Parameters:
    attrs

    The set of attributes to be installed.

  14. void set_exception_handler(exception_handler_type const & handler);

    The method sets exception handler function. The function will be called with no arguments in case if an exception occurs during either open_record or push_record method execution. Since exception handler is called from a catch statement, the exception can be rethrown in order to determine its type.

    By default no handler is installed, thus any exception is propagated as usual.

    See Also:

    See also: utility/exception_handler.hpp

    [Note] Note

    The exception handler can be invoked in several threads concurrently. Thread interruptions are not affected by exception handlers.

    Parameters:
    handler

    Exception handling function

  15. record_type open_record(attribute_set_type const & source_attributes);

    The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied. A successfully opened record may be pushed further to sinks by calling the push_record method or simply destroyed by destroying the returned handle.

    More than one open records are allowed, such records exist independently. All attribute values are acquired during opening the record and do not interact between records.

    The returned record handles may be copied, however, they must not be passed between different threads.

    Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may throw if one of the sinks throws, or some system resource limitation has been reached.

    Parameters:
    source_attributes

    The set of source-specific attributes to be attached to the record to be opened.

    Returns:

    A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).

  16. void push_record(record_type const & rec);

    The method pushes the record to sinks.

    Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may throw if one of the sinks throws.

    Parameters:
    rec

    A previously successfully opened log record.

    Requires:

    !!rec == true

basic_core public static functions

  1. static shared_ptr< basic_core > get();

    Returns:

    The method returns a pointer to the logging core singleton instance.


PrevUpHomeNext