2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
9 * \author Andrey Semashev
12 * This header contains logging core class definition.
15 #ifndef BOOST_LOG_CORE_CORE_HPP_INCLUDED_
16 #define BOOST_LOG_CORE_CORE_HPP_INCLUDED_
19 #include <boost/smart_ptr/shared_ptr.hpp>
20 #include <boost/move/core.hpp>
21 #include <boost/log/detail/config.hpp>
22 #include <boost/log/detail/light_function.hpp>
23 #include <boost/log/core/record.hpp>
24 #include <boost/log/attributes/attribute_set.hpp>
25 #include <boost/log/attributes/attribute_name.hpp>
26 #include <boost/log/attributes/attribute.hpp>
27 #include <boost/log/attributes/attribute_value_set.hpp>
28 #include <boost/log/expressions/filter.hpp>
29 #include <boost/log/detail/header.hpp>
31 #ifdef BOOST_HAS_PRAGMA_ONCE
37 BOOST_LOG_OPEN_NAMESPACE
39 #ifndef BOOST_LOG_DOXYGEN_PASS
47 #endif // BOOST_LOG_DOXYGEN_PASS
51 typedef shared_ptr< core > core_ptr;
54 * \brief Logging library core class
56 * The logging core is used to interconnect log sources and sinks. It also provides
57 * a number of basic features, like global filtering and global and thread-specific attribute storage.
59 * The logging core is a singleton. Users can acquire the core instance by calling the static method <tt>get</tt>.
64 //! Exception handler function type
65 typedef boost::log::aux::light_function< void () > exception_handler_type;
68 //! Implementation type
69 struct implementation;
70 friend struct implementation;
73 //! A pointer to the implementation
74 implementation* m_impl;
83 * Destructor. Destroys the core, releases any sinks and attributes that were registered.
88 * \return The method returns a pointer to the logging core singleton instance.
90 BOOST_LOG_API static core_ptr get();
93 * The method enables or disables logging.
95 * Setting this status to \c false allows you to completely wipe out any logging activity, including
96 * filtering and generation of attribute values. It is useful if you want to completely disable logging
97 * in a running application. The state of logging does not alter any other properties of the logging
98 * library, such as filters or sinks, so you can enable logging with the very same settings that you had
99 * when the logging was disabled.
100 * This feature may also be useful if you want to perform major changes to logging configuration and
101 * don't want your application to block on opening or pushing a log record.
103 * By default logging is enabled.
105 * \param enabled The actual flag of logging activity.
106 * \return The previous value of enabled/disabled logging flag
108 BOOST_LOG_API bool set_logging_enabled(bool enabled = true);
110 * The method allows to detect if logging is enabled. See the comment for \c set_logging_enabled.
112 BOOST_LOG_API bool get_logging_enabled() const;
115 * The method sets the global logging filter. The filter is applied to every log record that is processed.
117 * \param filter The filter function object to be installed.
119 BOOST_LOG_API void set_filter(filter const& filter);
121 * The method removes the global logging filter. All log records are passed to sinks without global filtering applied.
123 BOOST_LOG_API void reset_filter();
126 * The method adds a new sink. The sink is included into logging process immediately after being added and until being removed.
127 * No sink can be added more than once at the same time. If the sink is already registered, the call is ignored.
129 * \param s The sink to be registered.
131 BOOST_LOG_API void add_sink(shared_ptr< sinks::sink > const& s);
133 * The method removes the sink from the output. The sink will not receive any log records after removal.
134 * The call has no effect if the sink is not registered.
136 * \param s The sink to be unregistered.
138 BOOST_LOG_API void remove_sink(shared_ptr< sinks::sink > const& s);
140 * The method removes all registered sinks from the output. The sinks will not receive any log records after removal.
142 BOOST_LOG_API void remove_all_sinks();
145 * The method performs flush on all registered sinks.
147 * \note This method may take long time to complete as it may block until all sinks manage to process all buffered log records.
148 * The call will also block all logging attempts until the operation completes.
150 BOOST_LOG_API void flush();
153 * The method adds an attribute to the global attribute set. The attribute will be implicitly added to every log record.
155 * \param name The attribute name.
156 * \param attr The attribute factory.
157 * \return A pair of values. If the second member is \c true, then the attribute is added and the first member points to the
158 * attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents
161 BOOST_LOG_API std::pair< attribute_set::iterator, bool > add_global_attribute(attribute_name const& name, attribute const& attr);
163 * The method removes an attribute from the global attribute set.
165 * \pre The attribute was added with the \c add_global_attribute call.
166 * \post The attribute is no longer registered as a global attribute. The iterator is invalidated after removal.
168 * \param it Iterator to the previously added attribute.
170 BOOST_LOG_API void remove_global_attribute(attribute_set::iterator it);
173 * The method returns a copy of the complete set of currently registered global attributes.
175 BOOST_LOG_API attribute_set get_global_attributes() const;
177 * The method replaces the complete set of currently registered global attributes with the provided set.
179 * \note The method invalidates all iterators and references that may have been returned
180 * from the \c add_global_attribute method.
182 * \param attrs The set of attributes to be installed.
184 BOOST_LOG_API void set_global_attributes(attribute_set const& attrs);
187 * The method adds an attribute to the thread-specific attribute set. The attribute will be implicitly added to
188 * every log record made in the current thread.
190 * \note In single-threaded build the effect is the same as adding the attribute globally. This, however, does
191 * not imply that iterators to thread-specific and global attributes are interchangeable.
193 * \param name The attribute name.
194 * \param attr The attribute factory.
195 * \return A pair of values. If the second member is \c true, then the attribute is added and the first member points to the
196 * attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents
199 BOOST_LOG_API std::pair< attribute_set::iterator, bool > add_thread_attribute(attribute_name const& name, attribute const& attr);
201 * The method removes an attribute from the thread-specific attribute set.
203 * \pre The attribute was added with the \c add_thread_attribute call.
204 * \post The attribute is no longer registered as a thread-specific attribute. The iterator is invalidated after removal.
206 * \param it Iterator to the previously added attribute.
208 BOOST_LOG_API void remove_thread_attribute(attribute_set::iterator it);
211 * The method returns a copy of the complete set of currently registered thread-specific attributes.
213 BOOST_LOG_API attribute_set get_thread_attributes() const;
215 * The method replaces the complete set of currently registered thread-specific attributes with the provided set.
217 * \note The method invalidates all iterators and references that may have been returned
218 * from the \c add_thread_attribute method.
220 * \param attrs The set of attributes to be installed.
222 BOOST_LOG_API void set_thread_attributes(attribute_set const& attrs);
225 * The method sets exception handler function. The function will be called with no arguments
226 * in case if an exception occurs during either \c open_record or \c push_record method
227 * execution. Since exception handler is called from a \c catch statement, the exception
228 * can be rethrown in order to determine its type.
230 * By default no handler is installed, thus any exception is propagated as usual.
232 * \sa See also: <tt>utility/exception_handler.hpp</tt>
233 * \param handler Exception handling function
235 * \note The exception handler can be invoked in several threads concurrently.
236 * Thread interruptions are not affected by exception handlers.
238 BOOST_LOG_API void set_exception_handler(exception_handler_type const& handler);
241 * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
242 * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
243 * destroying the returned object.
245 * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
246 * the record and do not interact between records.
248 * The returned records can be copied, however, they must not be passed between different threads.
250 * \param source_attributes The set of source-specific attributes to be attached to the record to be opened.
251 * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
253 * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
254 * throw if one of the sinks throws, or some system resource limitation is reached.
256 BOOST_LOG_API record open_record(attribute_set const& source_attributes);
258 * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
259 * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
260 * destroying the returned object.
262 * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
263 * the record and do not interact between records.
265 * The returned records can be copied, however, they must not be passed between different threads.
267 * \param source_attributes The set of source-specific attribute values to be attached to the record to be opened.
268 * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
270 * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
271 * throw if one of the sinks throws, or some system resource limitation is reached.
273 BOOST_LOG_API record open_record(attribute_value_set const& source_attributes);
275 * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
276 * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
277 * destroying the returned object.
279 * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
280 * the record and do not interact between records.
282 * The returned records can be copied, however, they must not be passed between different threads.
284 * \param source_attributes The set of source-specific attribute values to be attached to the record to be opened. The contents
285 * of this container are unspecified after this call.
286 * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
288 * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
289 * throw if one of the sinks throws, or some system resource limitation is reached.
291 BOOST_FORCEINLINE record open_record(BOOST_RV_REF(attribute_value_set) source_attributes)
293 return open_record_move(static_cast< attribute_value_set& >(source_attributes));
297 * The method pushes the record to sinks. The record is moved from in the process.
299 * \pre <tt>!!rec == true</tt>
300 * \post <tt>!rec == true</tt>
301 * \param rec A previously successfully opened log record.
303 * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
304 * throw if one of the sinks throws.
306 BOOST_FORCEINLINE void push_record(BOOST_RV_REF(record) rec)
308 push_record_move(static_cast< record& >(rec));
311 BOOST_DELETED_FUNCTION(core(core const&))
312 BOOST_DELETED_FUNCTION(core& operator= (core const&))
314 #ifndef BOOST_LOG_DOXYGEN_PASS
316 //! Opens log record. This function is mostly needed to maintain ABI stable between C++03 and C++11.
317 BOOST_LOG_API record open_record_move(attribute_value_set& source_attributes);
318 //! The method pushes the record to sinks.
319 BOOST_LOG_API void push_record_move(record& rec);
320 #endif // BOOST_LOG_DOXYGEN_PASS
323 BOOST_LOG_CLOSE_NAMESPACE // namespace log
327 #include <boost/log/detail/footer.hpp>
329 #endif // BOOST_LOG_CORE_CORE_HPP_INCLUDED_