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 * The header contains implementation of convenience functions for enabling logging to console.
15 #ifndef BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
19 #include <boost/type_traits/is_void.hpp>
20 #include <boost/smart_ptr/shared_ptr.hpp>
21 #include <boost/smart_ptr/make_shared_object.hpp>
22 #include <boost/core/null_deleter.hpp>
23 #include <boost/log/detail/config.hpp>
24 #include <boost/log/detail/parameter_tools.hpp>
25 #include <boost/log/detail/sink_init_helpers.hpp>
26 #ifndef BOOST_LOG_NO_THREADS
27 #include <boost/log/sinks/sync_frontend.hpp>
29 #include <boost/log/sinks/unlocked_frontend.hpp>
31 #include <boost/log/sinks/text_ostream_backend.hpp>
32 #include <boost/log/keywords/format.hpp>
33 #include <boost/log/keywords/filter.hpp>
34 #include <boost/log/detail/header.hpp>
36 #ifdef BOOST_HAS_PRAGMA_ONCE
41 #ifndef BOOST_LOG_DOXYGEN_PASS
42 #ifndef BOOST_LOG_NO_THREADS
43 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
45 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
47 #endif // BOOST_LOG_DOXYGEN_PASS
51 BOOST_LOG_OPEN_NAMESPACE
55 // The function creates and initializes the sink
56 template< typename CharT, typename ArgsT >
58 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
59 sinks::basic_text_ostream_backend< CharT >
61 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
63 shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter());
65 typedef sinks::basic_text_ostream_backend< CharT > backend_t;
66 shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
68 pBackend->add_stream(pStream);
70 typedef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< backend_t > sink_t;
71 shared_ptr< sink_t > pSink = boost::make_shared< sink_t >(pBackend);
73 aux::setup_filter(*pSink, args,
74 typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
76 aux::setup_formatter(*pSink, args,
77 typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
79 core::get()->add_sink(pSink);
84 template< typename CharT >
85 struct default_console_stream;
87 #ifdef BOOST_LOG_USE_CHAR
89 struct default_console_stream< char >
91 static std::ostream& get() { return std::clog; }
93 #endif // BOOST_LOG_USE_CHAR
95 #ifdef BOOST_LOG_USE_WCHAR_T
97 struct default_console_stream< wchar_t >
99 static std::wostream& get() { return std::wclog; }
101 #endif // BOOST_LOG_USE_WCHAR_T
105 #ifndef BOOST_LOG_DOXYGEN_PASS
107 template< typename CharT >
109 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
110 sinks::basic_text_ostream_backend< CharT >
114 return aux::add_console_log(
115 aux::default_console_stream< CharT >::get(), log::aux::empty_arg_list());
119 template< typename CharT >
121 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
122 sinks::basic_text_ostream_backend< CharT >
124 > add_console_log(std::basic_ostream< CharT >& strm)
126 return aux::add_console_log(strm, log::aux::empty_arg_list());
129 template< typename CharT, typename ArgT1 >
131 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
132 sinks::basic_text_ostream_backend< CharT >
134 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1)
136 return aux::add_console_log(strm, arg1);
139 template< typename CharT, typename ArgT1, typename ArgT2 >
141 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
142 sinks::basic_text_ostream_backend< CharT >
144 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2)
146 return aux::add_console_log(strm, (arg1, arg2));
149 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3 >
151 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
152 sinks::basic_text_ostream_backend< CharT >
154 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3)
156 return aux::add_console_log(strm, (arg1, arg2, arg3));
159 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4 >
161 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
162 sinks::basic_text_ostream_backend< CharT >
164 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3, ArgT3 const& arg4)
166 return aux::add_console_log(strm, (arg1, arg2, arg3, arg4));
169 #else // BOOST_LOG_DOXYGEN_PASS
172 * The function constructs sink for the specified console stream and adds it to the core
174 * \param strm One of the standard console streams: <tt>std::cout</tt>, <tt>std::cerr</tt> or <tt>std::clog</tt>
175 * (or the corresponding wide-character analogues).
176 * \param args Optional additional named arguments for the sink initialization. The following arguments are supported:
177 * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
178 * or a filter lambda expression.
179 * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
180 * or a formatter lambda expression (either streaming or Boost.Format-like notation).
181 * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream
182 * after each written record.
183 * \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
184 * the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
185 * \return Pointer to the constructed sink.
187 template< typename CharT, typename... ArgsT >
189 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
190 sinks::basic_text_ostream_backend< CharT >
192 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
195 * Equivalent to: <tt>add_console_log(std::clog);</tt> or <tt>add_console_log(std::wclog);</tt>,
196 * depending on the \c CharT type.
200 template< typename CharT, typename... ArgsT >
202 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
203 sinks::basic_text_ostream_backend< CharT >
205 > add_console_log(ArgsT... const& args);
207 #endif // BOOST_LOG_DOXYGEN_PASS
209 #ifdef BOOST_LOG_USE_CHAR
212 * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
216 * \return Pointer to the constructed sink.
219 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
220 sinks::text_ostream_backend
224 return add_console_log(std::clog);
227 #endif // BOOST_LOG_USE_CHAR
229 #ifdef BOOST_LOG_USE_WCHAR_T
232 * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
234 * \return Pointer to the constructed sink.
237 BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
238 sinks::wtext_ostream_backend
242 return add_console_log(std::wclog);
245 #endif // BOOST_LOG_USE_WCHAR_T
247 BOOST_LOG_CLOSE_NAMESPACE // namespace log
251 #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
253 #include <boost/log/detail/footer.hpp>
255 #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_