console.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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 console.hpp
  9. * \author Andrey Semashev
  10. * \date 16.05.2008
  11. *
  12. * The header contains implementation of convenience functions for enabling logging to console.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
  16. #include <iostream>
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/smart_ptr/make_shared_object.hpp>
  19. #include <boost/utility/empty_deleter.hpp>
  20. #include <boost/log/detail/config.hpp>
  21. #include <boost/log/detail/sink_init_helpers.hpp>
  22. #ifndef BOOST_LOG_NO_THREADS
  23. #include <boost/log/sinks/sync_frontend.hpp>
  24. #else
  25. #include <boost/log/sinks/unlocked_frontend.hpp>
  26. #endif
  27. #include <boost/log/sinks/text_ostream_backend.hpp>
  28. #include <boost/log/keywords/format.hpp>
  29. #include <boost/log/keywords/filter.hpp>
  30. #include <boost/log/keywords/auto_flush.hpp>
  31. #include <boost/log/detail/header.hpp>
  32. #ifdef BOOST_HAS_PRAGMA_ONCE
  33. #pragma once
  34. #endif
  35. #ifndef BOOST_LOG_DOXYGEN_PASS
  36. #ifndef BOOST_LOG_NO_THREADS
  37. #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
  38. #else
  39. #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
  40. #endif
  41. #endif // BOOST_LOG_DOXYGEN_PASS
  42. namespace boost {
  43. BOOST_LOG_OPEN_NAMESPACE
  44. namespace aux {
  45. // The function creates and initializes the sink
  46. template< typename CharT, typename ArgsT >
  47. shared_ptr<
  48. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  49. sinks::basic_text_ostream_backend< CharT >
  50. >
  51. > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
  52. {
  53. shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::empty_deleter());
  54. typedef sinks::basic_text_ostream_backend< CharT > backend_t;
  55. shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >();
  56. pBackend->add_stream(pStream);
  57. pBackend->auto_flush(args[keywords::auto_flush | false]);
  58. typedef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< backend_t > sink_t;
  59. shared_ptr< sink_t > pSink = boost::make_shared< sink_t >(pBackend);
  60. aux::setup_filter(*pSink, args,
  61. typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
  62. aux::setup_formatter(*pSink, args,
  63. typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
  64. core::get()->add_sink(pSink);
  65. return pSink;
  66. }
  67. template< typename CharT >
  68. struct default_console_stream;
  69. #ifdef BOOST_LOG_USE_CHAR
  70. template< >
  71. struct default_console_stream< char >
  72. {
  73. static std::ostream& get() { return std::clog; }
  74. };
  75. #endif // BOOST_LOG_USE_CHAR
  76. #ifdef BOOST_LOG_USE_WCHAR_T
  77. template< >
  78. struct default_console_stream< wchar_t >
  79. {
  80. static std::wostream& get() { return std::wclog; }
  81. };
  82. #endif // BOOST_LOG_USE_WCHAR_T
  83. } // namespace aux
  84. #ifndef BOOST_LOG_DOXYGEN_PASS
  85. template< typename CharT >
  86. inline shared_ptr<
  87. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  88. sinks::basic_text_ostream_backend< CharT >
  89. >
  90. > add_console_log()
  91. {
  92. return aux::add_console_log(
  93. aux::default_console_stream< CharT >::get(), keywords::auto_flush = false);
  94. }
  95. template< typename CharT >
  96. inline shared_ptr<
  97. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  98. sinks::basic_text_ostream_backend< CharT >
  99. >
  100. > add_console_log(std::basic_ostream< CharT >& strm)
  101. {
  102. return aux::add_console_log(strm, keywords::auto_flush = false);
  103. }
  104. template< typename CharT, typename ArgT1 >
  105. inline shared_ptr<
  106. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  107. sinks::basic_text_ostream_backend< CharT >
  108. >
  109. > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1)
  110. {
  111. return aux::add_console_log(strm, arg1);
  112. }
  113. template< typename CharT, typename ArgT1, typename ArgT2 >
  114. inline shared_ptr<
  115. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  116. sinks::basic_text_ostream_backend< CharT >
  117. >
  118. > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2)
  119. {
  120. return aux::add_console_log(strm, (arg1, arg2));
  121. }
  122. template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3 >
  123. inline shared_ptr<
  124. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  125. sinks::basic_text_ostream_backend< CharT >
  126. >
  127. > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3)
  128. {
  129. return aux::add_console_log(strm, (arg1, arg2, arg3));
  130. }
  131. #else // BOOST_LOG_DOXYGEN_PASS
  132. /*!
  133. * The function constructs sink for the specified console stream and adds it to the core
  134. *
  135. * \param strm One of the standard console streams: <tt>std::cout</tt>, <tt>std::cerr</tt> or <tt>std::clog</tt>
  136. * (or the corresponding wide-character analogues).
  137. * \param args Optional additional named arguments for the sink initialization. The following arguments are supported:
  138. * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
  139. * or a filter lambda expression.
  140. * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
  141. * or a formatter lambda expression (either streaming or Boost.Format-like notation).
  142. * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream
  143. * after each written record.
  144. * \return Pointer to the constructed sink.
  145. */
  146. template< typename CharT, typename... ArgsT >
  147. shared_ptr<
  148. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  149. sinks::basic_text_ostream_backend< CharT >
  150. >
  151. > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
  152. /*!
  153. * Equivalent to: <tt>add_console_log(std::clog);</tt> or <tt>add_console_log(std::wclog);</tt>,
  154. * depending on the \c CharT type.
  155. *
  156. * \overload
  157. */
  158. template< typename CharT, typename... ArgsT >
  159. shared_ptr<
  160. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  161. sinks::basic_text_ostream_backend< CharT >
  162. >
  163. > add_console_log(ArgsT... const& args);
  164. #endif // BOOST_LOG_DOXYGEN_PASS
  165. #ifdef BOOST_LOG_USE_CHAR
  166. /*!
  167. * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
  168. *
  169. * \overload
  170. *
  171. * \return Pointer to the constructed sink.
  172. */
  173. inline shared_ptr<
  174. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  175. sinks::text_ostream_backend
  176. >
  177. > add_console_log()
  178. {
  179. return add_console_log(std::clog);
  180. }
  181. #endif // BOOST_LOG_USE_CHAR
  182. #ifdef BOOST_LOG_USE_WCHAR_T
  183. /*!
  184. * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
  185. *
  186. * \return Pointer to the constructed sink.
  187. */
  188. inline shared_ptr<
  189. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  190. sinks::wtext_ostream_backend
  191. >
  192. > wadd_console_log()
  193. {
  194. return add_console_log(std::wclog);
  195. }
  196. #endif // BOOST_LOG_USE_WCHAR_T
  197. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  198. } // namespace boost
  199. #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
  200. #include <boost/log/detail/footer.hpp>
  201. #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_