console.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 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/type_traits/is_void.hpp>
  18. #include <boost/smart_ptr/shared_ptr.hpp>
  19. #include <boost/smart_ptr/make_shared_object.hpp>
  20. #include <boost/core/null_deleter.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/parameter_tools.hpp>
  23. #include <boost/log/detail/sink_init_helpers.hpp>
  24. #ifndef BOOST_LOG_NO_THREADS
  25. #include <boost/log/sinks/sync_frontend.hpp>
  26. #else
  27. #include <boost/log/sinks/unlocked_frontend.hpp>
  28. #endif
  29. #include <boost/log/sinks/text_ostream_backend.hpp>
  30. #include <boost/log/keywords/format.hpp>
  31. #include <boost/log/keywords/filter.hpp>
  32. #include <boost/log/detail/header.hpp>
  33. #ifdef BOOST_HAS_PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36. #ifndef BOOST_LOG_DOXYGEN_PASS
  37. #ifndef BOOST_LOG_NO_THREADS
  38. #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
  39. #else
  40. #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
  41. #endif
  42. #endif // BOOST_LOG_DOXYGEN_PASS
  43. namespace boost {
  44. BOOST_LOG_OPEN_NAMESPACE
  45. namespace aux {
  46. // The function creates and initializes the sink
  47. template< typename CharT, typename ArgsT >
  48. shared_ptr<
  49. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  50. sinks::basic_text_ostream_backend< CharT >
  51. >
  52. > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
  53. {
  54. shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter());
  55. typedef sinks::basic_text_ostream_backend< CharT > backend_t;
  56. shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
  57. pBackend->add_stream(pStream);
  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(), log::aux::empty_arg_list());
  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, log::aux::empty_arg_list());
  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. template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4 >
  132. inline shared_ptr<
  133. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  134. sinks::basic_text_ostream_backend< CharT >
  135. >
  136. > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3, ArgT3 const& arg4)
  137. {
  138. return aux::add_console_log(strm, (arg1, arg2, arg3, arg4));
  139. }
  140. #else // BOOST_LOG_DOXYGEN_PASS
  141. /*!
  142. * The function constructs sink for the specified console stream and adds it to the core
  143. *
  144. * \param strm One of the standard console streams: <tt>std::cout</tt>, <tt>std::cerr</tt> or <tt>std::clog</tt>
  145. * (or the corresponding wide-character analogues).
  146. * \param args Optional additional named arguments for the sink initialization. The following arguments are supported:
  147. * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
  148. * or a filter lambda expression.
  149. * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
  150. * or a formatter lambda expression (either streaming or Boost.Format-like notation).
  151. * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream
  152. * after each written record.
  153. * \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
  154. * the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
  155. * \return Pointer to the constructed sink.
  156. */
  157. template< typename CharT, typename... ArgsT >
  158. shared_ptr<
  159. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  160. sinks::basic_text_ostream_backend< CharT >
  161. >
  162. > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
  163. /*!
  164. * Equivalent to: <tt>add_console_log(std::clog);</tt> or <tt>add_console_log(std::wclog);</tt>,
  165. * depending on the \c CharT type.
  166. *
  167. * \overload
  168. */
  169. template< typename CharT, typename... ArgsT >
  170. shared_ptr<
  171. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  172. sinks::basic_text_ostream_backend< CharT >
  173. >
  174. > add_console_log(ArgsT... const& args);
  175. #endif // BOOST_LOG_DOXYGEN_PASS
  176. #ifdef BOOST_LOG_USE_CHAR
  177. /*!
  178. * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
  179. *
  180. * \overload
  181. *
  182. * \return Pointer to the constructed sink.
  183. */
  184. inline shared_ptr<
  185. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  186. sinks::text_ostream_backend
  187. >
  188. > add_console_log()
  189. {
  190. return add_console_log(std::clog);
  191. }
  192. #endif // BOOST_LOG_USE_CHAR
  193. #ifdef BOOST_LOG_USE_WCHAR_T
  194. /*!
  195. * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
  196. *
  197. * \return Pointer to the constructed sink.
  198. */
  199. inline shared_ptr<
  200. BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
  201. sinks::wtext_ostream_backend
  202. >
  203. > wadd_console_log()
  204. {
  205. return add_console_log(std::wclog);
  206. }
  207. #endif // BOOST_LOG_USE_WCHAR_T
  208. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  209. } // namespace boost
  210. #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
  211. #include <boost/log/detail/footer.hpp>
  212. #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_