basic_sink_frontend.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 basic_sink_frontend.hpp
  9. * \author Andrey Semashev
  10. * \date 14.07.2009
  11. *
  12. * The header contains implementation of a base class for sink frontends.
  13. */
  14. #ifndef BOOST_LOG_SINKS_BASIC_SINK_FRONTEND_HPP_INCLUDED_
  15. #define BOOST_LOG_SINKS_BASIC_SINK_FRONTEND_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/detail/code_conversion.hpp>
  19. #include <boost/log/detail/attachable_sstream_buf.hpp>
  20. #include <boost/log/detail/fake_mutex.hpp>
  21. #include <boost/log/core/record_view.hpp>
  22. #include <boost/log/sinks/sink.hpp>
  23. #include <boost/log/sinks/frontend_requirements.hpp>
  24. #include <boost/log/expressions/filter.hpp>
  25. #include <boost/log/expressions/formatter.hpp>
  26. #if !defined(BOOST_LOG_NO_THREADS)
  27. #include <boost/thread/exceptions.hpp>
  28. #include <boost/thread/tss.hpp>
  29. #include <boost/log/detail/locks.hpp>
  30. #include <boost/log/detail/light_rw_mutex.hpp>
  31. #endif // !defined(BOOST_LOG_NO_THREADS)
  32. #include <boost/log/detail/header.hpp>
  33. #ifdef BOOST_HAS_PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36. namespace boost {
  37. BOOST_LOG_OPEN_NAMESPACE
  38. namespace sinks {
  39. //! A base class for a logging sink frontend
  40. class BOOST_LOG_NO_VTABLE basic_sink_frontend :
  41. public sink
  42. {
  43. //! Base type
  44. typedef sink base_type;
  45. public:
  46. //! An exception handler type
  47. typedef base_type::exception_handler_type exception_handler_type;
  48. #if !defined(BOOST_LOG_NO_THREADS)
  49. protected:
  50. //! Mutex type
  51. typedef boost::log::aux::light_rw_mutex mutex_type;
  52. private:
  53. //! Synchronization mutex
  54. mutable mutex_type m_Mutex;
  55. #endif
  56. private:
  57. //! Filter
  58. filter m_Filter;
  59. //! Exception handler
  60. exception_handler_type m_ExceptionHandler;
  61. public:
  62. /*!
  63. * \brief Initializing constructor
  64. *
  65. * \param cross_thread The flag indicates whether the sink passes log records between different threads
  66. */
  67. explicit basic_sink_frontend(bool cross_thread) : sink(cross_thread)
  68. {
  69. }
  70. /*!
  71. * The method sets sink-specific filter functional object
  72. */
  73. template< typename FunT >
  74. void set_filter(FunT const& filter)
  75. {
  76. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< mutex_type > lock(m_Mutex);)
  77. m_Filter = filter;
  78. }
  79. /*!
  80. * The method resets the filter
  81. */
  82. void reset_filter()
  83. {
  84. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< mutex_type > lock(m_Mutex);)
  85. m_Filter.reset();
  86. }
  87. /*!
  88. * The method sets an exception handler function
  89. */
  90. template< typename FunT >
  91. void set_exception_handler(FunT const& handler)
  92. {
  93. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< mutex_type > lock(m_Mutex);)
  94. m_ExceptionHandler = handler;
  95. }
  96. /*!
  97. * The method resets the exception handler function
  98. */
  99. void reset_exception_handler()
  100. {
  101. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< mutex_type > lock(m_Mutex);)
  102. m_ExceptionHandler.clear();
  103. }
  104. /*!
  105. * The method returns \c true if no filter is set or the attribute values pass the filter
  106. *
  107. * \param attrs A set of attribute values of a logging record
  108. */
  109. bool will_consume(attribute_value_set const& attrs)
  110. {
  111. BOOST_LOG_EXPR_IF_MT(boost::log::aux::shared_lock_guard< mutex_type > lock(m_Mutex);)
  112. try
  113. {
  114. return m_Filter(attrs);
  115. }
  116. #if !defined(BOOST_LOG_NO_THREADS)
  117. catch (thread_interrupted&)
  118. {
  119. throw;
  120. }
  121. #endif
  122. catch (...)
  123. {
  124. if (m_ExceptionHandler.empty())
  125. throw;
  126. m_ExceptionHandler();
  127. return false;
  128. }
  129. }
  130. protected:
  131. #if !defined(BOOST_LOG_NO_THREADS)
  132. //! Returns reference to the frontend mutex
  133. mutex_type& frontend_mutex() const { return m_Mutex; }
  134. #endif
  135. //! Returns reference to the exception handler
  136. exception_handler_type& exception_handler() { return m_ExceptionHandler; }
  137. //! Returns reference to the exception handler
  138. exception_handler_type const& exception_handler() const { return m_ExceptionHandler; }
  139. //! Feeds log record to the backend
  140. template< typename BackendMutexT, typename BackendT >
  141. void feed_record(record_view const& rec, BackendMutexT& backend_mutex, BackendT& backend)
  142. {
  143. try
  144. {
  145. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< BackendMutexT > lock(backend_mutex);)
  146. backend.consume(rec);
  147. }
  148. #if !defined(BOOST_LOG_NO_THREADS)
  149. catch (thread_interrupted&)
  150. {
  151. throw;
  152. }
  153. #endif
  154. catch (...)
  155. {
  156. BOOST_LOG_EXPR_IF_MT(boost::log::aux::shared_lock_guard< mutex_type > lock(m_Mutex);)
  157. if (m_ExceptionHandler.empty())
  158. throw;
  159. m_ExceptionHandler();
  160. }
  161. }
  162. //! Attempts to feeds log record to the backend, does not block if \a backend_mutex is locked
  163. template< typename BackendMutexT, typename BackendT >
  164. bool try_feed_record(record_view const& rec, BackendMutexT& backend_mutex, BackendT& backend)
  165. {
  166. #if !defined(BOOST_LOG_NO_THREADS)
  167. try
  168. {
  169. if (!backend_mutex.try_lock())
  170. return false;
  171. }
  172. catch (thread_interrupted&)
  173. {
  174. throw;
  175. }
  176. catch (...)
  177. {
  178. boost::log::aux::shared_lock_guard< mutex_type > frontend_lock(this->frontend_mutex());
  179. if (this->exception_handler().empty())
  180. throw;
  181. this->exception_handler()();
  182. return false;
  183. }
  184. boost::log::aux::exclusive_auto_unlocker< BackendMutexT > unlocker(backend_mutex);
  185. #endif
  186. // No need to lock anything in the feed_record method
  187. boost::log::aux::fake_mutex m;
  188. feed_record(rec, m, backend);
  189. return true;
  190. }
  191. //! Flushes record buffers in the backend, if one supports it
  192. template< typename BackendMutexT, typename BackendT >
  193. void flush_backend(BackendMutexT& backend_mutex, BackendT& backend)
  194. {
  195. typedef typename BackendT::frontend_requirements frontend_requirements;
  196. flush_backend_impl(backend_mutex, backend,
  197. typename has_requirement< frontend_requirements, flushing >::type());
  198. }
  199. private:
  200. //! Flushes record buffers in the backend (the actual implementation)
  201. template< typename BackendMutexT, typename BackendT >
  202. void flush_backend_impl(BackendMutexT& backend_mutex, BackendT& backend, mpl::true_)
  203. {
  204. try
  205. {
  206. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< BackendMutexT > lock(backend_mutex);)
  207. backend.flush();
  208. }
  209. #if !defined(BOOST_LOG_NO_THREADS)
  210. catch (thread_interrupted&)
  211. {
  212. throw;
  213. }
  214. #endif
  215. catch (...)
  216. {
  217. BOOST_LOG_EXPR_IF_MT(boost::log::aux::shared_lock_guard< mutex_type > lock(m_Mutex);)
  218. if (m_ExceptionHandler.empty())
  219. throw;
  220. m_ExceptionHandler();
  221. }
  222. }
  223. //! Flushes record buffers in the backend (stub for backends that don't support flushing)
  224. template< typename BackendMutexT, typename BackendT >
  225. void flush_backend_impl(BackendMutexT&, BackendT&, mpl::false_)
  226. {
  227. }
  228. };
  229. //! A base class for a logging sink frontend with formatting support
  230. template< typename CharT >
  231. class BOOST_LOG_NO_VTABLE basic_formatting_sink_frontend :
  232. public basic_sink_frontend
  233. {
  234. typedef basic_sink_frontend base_type;
  235. public:
  236. //! Character type
  237. typedef CharT char_type;
  238. //! Formatted string type
  239. typedef std::basic_string< char_type > string_type;
  240. //! Formatter function object type
  241. typedef basic_formatter< char_type > formatter_type;
  242. //! Output stream type
  243. typedef typename formatter_type::stream_type stream_type;
  244. #if !defined(BOOST_LOG_NO_THREADS)
  245. protected:
  246. //! Mutex type
  247. typedef typename base_type::mutex_type mutex_type;
  248. #endif
  249. private:
  250. struct formatting_context
  251. {
  252. class cleanup_guard
  253. {
  254. private:
  255. formatting_context& m_context;
  256. public:
  257. explicit cleanup_guard(formatting_context& ctx) BOOST_NOEXCEPT : m_context(ctx)
  258. {
  259. }
  260. ~cleanup_guard()
  261. {
  262. m_context.m_FormattedRecord.clear();
  263. m_context.m_FormattingStream.rdbuf()->max_size(m_context.m_FormattedRecord.max_size());
  264. m_context.m_FormattingStream.rdbuf()->storage_overflow(false);
  265. m_context.m_FormattingStream.clear();
  266. }
  267. BOOST_DELETED_FUNCTION(cleanup_guard(cleanup_guard const&))
  268. BOOST_DELETED_FUNCTION(cleanup_guard& operator=(cleanup_guard const&))
  269. };
  270. #if !defined(BOOST_LOG_NO_THREADS)
  271. //! Object version
  272. const unsigned int m_Version;
  273. #endif
  274. //! Formatted log record storage
  275. string_type m_FormattedRecord;
  276. //! Formatting stream
  277. stream_type m_FormattingStream;
  278. //! Formatter functor
  279. formatter_type m_Formatter;
  280. formatting_context() :
  281. #if !defined(BOOST_LOG_NO_THREADS)
  282. m_Version(0),
  283. #endif
  284. m_FormattingStream(m_FormattedRecord)
  285. {
  286. m_FormattingStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
  287. }
  288. #if !defined(BOOST_LOG_NO_THREADS)
  289. formatting_context(unsigned int version, std::locale const& loc, formatter_type const& formatter) :
  290. m_Version(version),
  291. m_FormattingStream(m_FormattedRecord),
  292. m_Formatter(formatter)
  293. {
  294. m_FormattingStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
  295. m_FormattingStream.imbue(loc);
  296. }
  297. #endif
  298. };
  299. private:
  300. #if !defined(BOOST_LOG_NO_THREADS)
  301. //! State version
  302. volatile unsigned int m_Version;
  303. //! Formatter functor
  304. formatter_type m_Formatter;
  305. //! Locale to perform formatting
  306. std::locale m_Locale;
  307. //! Formatting state
  308. thread_specific_ptr< formatting_context > m_pContext;
  309. #else
  310. //! Formatting state
  311. formatting_context m_Context;
  312. #endif // !defined(BOOST_LOG_NO_THREADS)
  313. public:
  314. /*!
  315. * \brief Initializing constructor
  316. *
  317. * \param cross_thread The flag indicates whether the sink passes log records between different threads
  318. */
  319. explicit basic_formatting_sink_frontend(bool cross_thread) :
  320. basic_sink_frontend(cross_thread)
  321. #if !defined(BOOST_LOG_NO_THREADS)
  322. , m_Version(0)
  323. #endif
  324. {
  325. }
  326. /*!
  327. * The method sets sink-specific formatter function object
  328. */
  329. template< typename FunT >
  330. void set_formatter(FunT const& formatter)
  331. {
  332. #if !defined(BOOST_LOG_NO_THREADS)
  333. boost::log::aux::exclusive_lock_guard< mutex_type > lock(this->frontend_mutex());
  334. m_Formatter = formatter;
  335. ++m_Version;
  336. #else
  337. m_Context.m_Formatter = formatter;
  338. #endif
  339. }
  340. /*!
  341. * The method resets the formatter
  342. */
  343. void reset_formatter()
  344. {
  345. #if !defined(BOOST_LOG_NO_THREADS)
  346. boost::log::aux::exclusive_lock_guard< mutex_type > lock(this->frontend_mutex());
  347. m_Formatter.reset();
  348. ++m_Version;
  349. #else
  350. m_Context.m_Formatter.reset();
  351. #endif
  352. }
  353. /*!
  354. * The method returns the current locale used for formatting
  355. */
  356. std::locale getloc() const
  357. {
  358. #if !defined(BOOST_LOG_NO_THREADS)
  359. boost::log::aux::exclusive_lock_guard< mutex_type > lock(this->frontend_mutex());
  360. return m_Locale;
  361. #else
  362. return m_Context.m_FormattingStream.getloc();
  363. #endif
  364. }
  365. /*!
  366. * The method sets the locale used for formatting
  367. */
  368. void imbue(std::locale const& loc)
  369. {
  370. #if !defined(BOOST_LOG_NO_THREADS)
  371. boost::log::aux::exclusive_lock_guard< mutex_type > lock(this->frontend_mutex());
  372. m_Locale = loc;
  373. ++m_Version;
  374. #else
  375. m_Context.m_FormattingStream.imbue(loc);
  376. #endif
  377. }
  378. protected:
  379. //! Returns reference to the formatter
  380. formatter_type& formatter()
  381. {
  382. #if !defined(BOOST_LOG_NO_THREADS)
  383. return m_Formatter;
  384. #else
  385. return m_Context.m_Formatter;
  386. #endif
  387. }
  388. //! Feeds log record to the backend
  389. template< typename BackendMutexT, typename BackendT >
  390. void feed_record(record_view const& rec, BackendMutexT& backend_mutex, BackendT& backend)
  391. {
  392. formatting_context* context;
  393. #if !defined(BOOST_LOG_NO_THREADS)
  394. context = m_pContext.get();
  395. if (!context || context->m_Version != m_Version)
  396. {
  397. {
  398. boost::log::aux::shared_lock_guard< mutex_type > lock(this->frontend_mutex());
  399. context = new formatting_context(m_Version, m_Locale, m_Formatter);
  400. }
  401. m_pContext.reset(context);
  402. }
  403. #else
  404. context = &m_Context;
  405. #endif
  406. typename formatting_context::cleanup_guard cleanup(*context);
  407. try
  408. {
  409. // Perform the formatting
  410. context->m_Formatter(rec, context->m_FormattingStream);
  411. context->m_FormattingStream.flush();
  412. // Feed the record
  413. BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< BackendMutexT > lock(backend_mutex);)
  414. backend.consume(rec, context->m_FormattedRecord);
  415. }
  416. #if !defined(BOOST_LOG_NO_THREADS)
  417. catch (thread_interrupted&)
  418. {
  419. throw;
  420. }
  421. #endif
  422. catch (...)
  423. {
  424. BOOST_LOG_EXPR_IF_MT(boost::log::aux::shared_lock_guard< mutex_type > lock(this->frontend_mutex());)
  425. if (this->exception_handler().empty())
  426. throw;
  427. this->exception_handler()();
  428. }
  429. }
  430. //! Attempts to feeds log record to the backend, does not block if \a backend_mutex is locked
  431. template< typename BackendMutexT, typename BackendT >
  432. bool try_feed_record(record_view const& rec, BackendMutexT& backend_mutex, BackendT& backend)
  433. {
  434. #if !defined(BOOST_LOG_NO_THREADS)
  435. try
  436. {
  437. if (!backend_mutex.try_lock())
  438. return false;
  439. }
  440. catch (thread_interrupted&)
  441. {
  442. throw;
  443. }
  444. catch (...)
  445. {
  446. boost::log::aux::shared_lock_guard< mutex_type > frontend_lock(this->frontend_mutex());
  447. if (this->exception_handler().empty())
  448. throw;
  449. this->exception_handler()();
  450. return false;
  451. }
  452. boost::log::aux::exclusive_auto_unlocker< BackendMutexT > unlocker(backend_mutex);
  453. #endif
  454. // No need to lock anything in the feed_record method
  455. boost::log::aux::fake_mutex m;
  456. feed_record(rec, m, backend);
  457. return true;
  458. }
  459. };
  460. namespace aux {
  461. template<
  462. typename BackendT,
  463. bool RequiresFormattingV = has_requirement<
  464. typename BackendT::frontend_requirements,
  465. formatted_records
  466. >::value
  467. >
  468. struct make_sink_frontend_base
  469. {
  470. typedef basic_sink_frontend type;
  471. };
  472. template< typename BackendT >
  473. struct make_sink_frontend_base< BackendT, true >
  474. {
  475. typedef basic_formatting_sink_frontend< typename BackendT::char_type > type;
  476. };
  477. } // namespace aux
  478. } // namespace sinks
  479. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  480. } // namespace boost
  481. #include <boost/log/detail/footer.hpp>
  482. #endif // BOOST_LOG_SINKS_BASIC_SINK_FRONTEND_HPP_INCLUDED_