core.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  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)
  6. */
  7. /*!
  8. * \file core/core.hpp
  9. * \author Andrey Semashev
  10. * \date 19.04.2007
  11. *
  12. * This header contains logging core class definition.
  13. */
  14. #ifndef BOOST_LOG_CORE_CORE_HPP_INCLUDED_
  15. #define BOOST_LOG_CORE_CORE_HPP_INCLUDED_
  16. #include <utility>
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/move/core.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/detail/light_function.hpp>
  21. #include <boost/log/core/record.hpp>
  22. #include <boost/log/attributes/attribute_set.hpp>
  23. #include <boost/log/attributes/attribute_name.hpp>
  24. #include <boost/log/attributes/attribute.hpp>
  25. #include <boost/log/attributes/attribute_value_set.hpp>
  26. #include <boost/log/expressions/filter.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. #ifndef BOOST_LOG_DOXYGEN_PASS
  34. namespace sinks {
  35. class sink;
  36. } // namespace sinks
  37. #endif // BOOST_LOG_DOXYGEN_PASS
  38. class core;
  39. typedef shared_ptr< core > core_ptr;
  40. /*!
  41. * \brief Logging library core class
  42. *
  43. * The logging core is used to interconnect log sources and sinks. It also provides
  44. * a number of basic features, like global filtering and global and thread-specific attribute storage.
  45. *
  46. * The logging core is a singleton. Users can acquire the core instance by calling the static method <tt>get</tt>.
  47. */
  48. class core
  49. {
  50. public:
  51. //! Exception handler function type
  52. typedef boost::log::aux::light_function< void () > exception_handler_type;
  53. private:
  54. //! Implementation type
  55. struct implementation;
  56. friend struct implementation;
  57. private:
  58. //! A pointer to the implementation
  59. implementation* m_impl;
  60. private:
  61. //! \cond
  62. core();
  63. //! \endcond
  64. public:
  65. /*!
  66. * Destructor. Destroys the core, releases any sinks and attributes that were registered.
  67. */
  68. ~core();
  69. /*!
  70. * \return The method returns a pointer to the logging core singleton instance.
  71. */
  72. BOOST_LOG_API static core_ptr get();
  73. /*!
  74. * The method enables or disables logging.
  75. *
  76. * Setting this status to \c false allows you to completely wipe out any logging activity, including
  77. * filtering and generation of attribute values. It is useful if you want to completely disable logging
  78. * in a running application. The state of logging does not alter any other properties of the logging
  79. * library, such as filters or sinks, so you can enable logging with the very same settings that you had
  80. * when the logging was disabled.
  81. * This feature may also be useful if you want to perform major changes to logging configuration and
  82. * don't want your application to block on opening or pushing a log record.
  83. *
  84. * By default logging is enabled.
  85. *
  86. * \param enabled The actual flag of logging activity.
  87. * \return The previous value of enabled/disabled logging flag
  88. */
  89. BOOST_LOG_API bool set_logging_enabled(bool enabled = true);
  90. /*!
  91. * The method allows to detect if logging is enabled. See the comment for \c set_logging_enabled.
  92. */
  93. BOOST_LOG_API bool get_logging_enabled() const;
  94. /*!
  95. * The method sets the global logging filter. The filter is applied to every log record that is processed.
  96. *
  97. * \param filter The filter function object to be installed.
  98. */
  99. BOOST_LOG_API void set_filter(filter const& filter);
  100. /*!
  101. * The method removes the global logging filter. All log records are passed to sinks without global filtering applied.
  102. */
  103. BOOST_LOG_API void reset_filter();
  104. /*!
  105. * The method adds a new sink. The sink is included into logging process immediately after being added and until being removed.
  106. * No sink can be added more than once at the same time. If the sink is already registered, the call is ignored.
  107. *
  108. * \param s The sink to be registered.
  109. */
  110. BOOST_LOG_API void add_sink(shared_ptr< sinks::sink > const& s);
  111. /*!
  112. * The method removes the sink from the output. The sink will not receive any log records after removal.
  113. * The call has no effect if the sink is not registered.
  114. *
  115. * \param s The sink to be unregistered.
  116. */
  117. BOOST_LOG_API void remove_sink(shared_ptr< sinks::sink > const& s);
  118. /*!
  119. * The method removes all registered sinks from the output. The sinks will not receive any log records after removal.
  120. */
  121. BOOST_LOG_API void remove_all_sinks();
  122. /*!
  123. * The method performs flush on all registered sinks.
  124. *
  125. * \note This method may take long time to complete as it may block until all sinks manage to process all buffered log records.
  126. * The call will also block all logging attempts until the operation completes.
  127. */
  128. BOOST_LOG_API void flush();
  129. /*!
  130. * The method adds an attribute to the global attribute set. The attribute will be implicitly added to every log record.
  131. *
  132. * \param name The attribute name.
  133. * \param attr The attribute factory.
  134. * \return A pair of values. If the second member is \c true, then the attribute is added and the first member points to the
  135. * attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents
  136. * addition.
  137. */
  138. BOOST_LOG_API std::pair< attribute_set::iterator, bool > add_global_attribute(attribute_name const& name, attribute const& attr);
  139. /*!
  140. * The method removes an attribute from the global attribute set.
  141. *
  142. * \pre The attribute was added with the \c add_global_attribute call.
  143. * \post The attribute is no longer registered as a global attribute. The iterator is invalidated after removal.
  144. *
  145. * \param it Iterator to the previously added attribute.
  146. */
  147. BOOST_LOG_API void remove_global_attribute(attribute_set::iterator it);
  148. /*!
  149. * The method returns a copy of the complete set of currently registered global attributes.
  150. */
  151. BOOST_LOG_API attribute_set get_global_attributes() const;
  152. /*!
  153. * The method replaces the complete set of currently registered global attributes with the provided set.
  154. *
  155. * \note The method invalidates all iterators and references that may have been returned
  156. * from the \c add_global_attribute method.
  157. *
  158. * \param attrs The set of attributes to be installed.
  159. */
  160. BOOST_LOG_API void set_global_attributes(attribute_set const& attrs);
  161. /*!
  162. * The method adds an attribute to the thread-specific attribute set. The attribute will be implicitly added to
  163. * every log record made in the current thread.
  164. *
  165. * \note In single-threaded build the effect is the same as adding the attribute globally. This, however, does
  166. * not imply that iterators to thread-specific and global attributes are interchangeable.
  167. *
  168. * \param name The attribute name.
  169. * \param attr The attribute factory.
  170. * \return A pair of values. If the second member is \c true, then the attribute is added and the first member points to the
  171. * attribute. Otherwise the attribute was not added and the first member points to the attribute that prevents
  172. * addition.
  173. */
  174. BOOST_LOG_API std::pair< attribute_set::iterator, bool > add_thread_attribute(attribute_name const& name, attribute const& attr);
  175. /*!
  176. * The method removes an attribute from the thread-specific attribute set.
  177. *
  178. * \pre The attribute was added with the \c add_thread_attribute call.
  179. * \post The attribute is no longer registered as a thread-specific attribute. The iterator is invalidated after removal.
  180. *
  181. * \param it Iterator to the previously added attribute.
  182. */
  183. BOOST_LOG_API void remove_thread_attribute(attribute_set::iterator it);
  184. /*!
  185. * The method returns a copy of the complete set of currently registered thread-specific attributes.
  186. */
  187. BOOST_LOG_API attribute_set get_thread_attributes() const;
  188. /*!
  189. * The method replaces the complete set of currently registered thread-specific attributes with the provided set.
  190. *
  191. * \note The method invalidates all iterators and references that may have been returned
  192. * from the \c add_thread_attribute method.
  193. *
  194. * \param attrs The set of attributes to be installed.
  195. */
  196. BOOST_LOG_API void set_thread_attributes(attribute_set const& attrs);
  197. /*!
  198. * The method sets exception handler function. The function will be called with no arguments
  199. * in case if an exception occurs during either \c open_record or \c push_record method
  200. * execution. Since exception handler is called from a \c catch statement, the exception
  201. * can be rethrown in order to determine its type.
  202. *
  203. * By default no handler is installed, thus any exception is propagated as usual.
  204. *
  205. * \sa See also: <tt>utility/exception_handler.hpp</tt>
  206. * \param handler Exception handling function
  207. *
  208. * \note The exception handler can be invoked in several threads concurrently.
  209. * Thread interruptions are not affected by exception handlers.
  210. */
  211. BOOST_LOG_API void set_exception_handler(exception_handler_type const& handler);
  212. /*!
  213. * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
  214. * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
  215. * destroying the returned object.
  216. *
  217. * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
  218. * the record and do not interact between records.
  219. *
  220. * The returned records can be copied, however, they must not be passed between different threads.
  221. *
  222. * \param source_attributes The set of source-specific attributes to be attached to the record to be opened.
  223. * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
  224. *
  225. * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
  226. * throw if one of the sinks throws, or some system resource limitation is reached.
  227. */
  228. BOOST_LOG_API record open_record(attribute_set const& source_attributes);
  229. /*!
  230. * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
  231. * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
  232. * destroying the returned object.
  233. *
  234. * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
  235. * the record and do not interact between records.
  236. *
  237. * The returned records can be copied, however, they must not be passed between different threads.
  238. *
  239. * \param source_attributes The set of source-specific attribute values to be attached to the record to be opened.
  240. * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
  241. *
  242. * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
  243. * throw if one of the sinks throws, or some system resource limitation is reached.
  244. */
  245. BOOST_LOG_API record open_record(attribute_value_set const& source_attributes);
  246. /*!
  247. * The method attempts to open a new record to be written. While attempting to open a log record all filtering is applied.
  248. * A successfully opened record can be pushed further to sinks by calling the \c push_record method or simply destroyed by
  249. * destroying the returned object.
  250. *
  251. * More than one open records are allowed, such records exist independently. All attribute values are acquired during opening
  252. * the record and do not interact between records.
  253. *
  254. * The returned records can be copied, however, they must not be passed between different threads.
  255. *
  256. * \param source_attributes The set of source-specific attribute values to be attached to the record to be opened. The contents
  257. * of this container are unspecified after this call.
  258. * \return A valid log record if the record is opened, an invalid record object if not (e.g. because it didn't pass filtering).
  259. *
  260. * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
  261. * throw if one of the sinks throws, or some system resource limitation is reached.
  262. */
  263. BOOST_FORCEINLINE record open_record(BOOST_RV_REF(attribute_value_set) source_attributes)
  264. {
  265. return open_record_move(static_cast< attribute_value_set& >(source_attributes));
  266. }
  267. /*!
  268. * The method pushes the record to sinks. The record is moved from in the process.
  269. *
  270. * \pre <tt>!!rec == true</tt>
  271. * \post <tt>!rec == true</tt>
  272. * \param rec A previously successfully opened log record.
  273. *
  274. * \b Throws: If an exception handler is installed, only throws if the handler throws. Otherwise may
  275. * throw if one of the sinks throws.
  276. */
  277. BOOST_FORCEINLINE void push_record(BOOST_RV_REF(record) rec)
  278. {
  279. push_record_move(static_cast< record& >(rec));
  280. }
  281. BOOST_DELETED_FUNCTION(core(core const&))
  282. BOOST_DELETED_FUNCTION(core& operator= (core const&))
  283. #ifndef BOOST_LOG_DOXYGEN_PASS
  284. private:
  285. //! Opens log record. This function is mostly needed to maintain ABI stable between C++03 and C++11.
  286. BOOST_LOG_API record open_record_move(attribute_value_set& source_attributes);
  287. //! The method pushes the record to sinks.
  288. BOOST_LOG_API void push_record_move(record& rec);
  289. #endif // BOOST_LOG_DOXYGEN_PASS
  290. };
  291. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  292. } // namespace boost
  293. #include <boost/log/detail/footer.hpp>
  294. #endif // BOOST_LOG_CORE_CORE_HPP_INCLUDED_