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 
15 #ifndef BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
17 
18 #include <iostream>
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>
28 #else
29 #include <boost/log/sinks/unlocked_frontend.hpp>
30 #endif
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>
35 
36 #ifdef BOOST_HAS_PRAGMA_ONCE
37 #pragma once
38 #endif
39 
40 
41 #ifndef BOOST_LOG_DOXYGEN_PASS
42 #ifndef BOOST_LOG_NO_THREADS
43 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
44 #else
45 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
46 #endif
47 #endif // BOOST_LOG_DOXYGEN_PASS
48 
49 namespace boost {
50 
51 BOOST_LOG_OPEN_NAMESPACE
52 
53 namespace aux {
54 
55 // The function creates and initializes the sink
56 template< typename CharT, typename ArgsT >
57 shared_ptr<
58     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
59         sinks::basic_text_ostream_backend< CharT >
60     >
add_console_log(std::basic_ostream<CharT> & strm,ArgsT const & args)61 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
62 {
63     shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter());
64 
65     typedef sinks::basic_text_ostream_backend< CharT > backend_t;
66     shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
67 
68     pBackend->add_stream(pStream);
69 
70     typedef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< backend_t > sink_t;
71     shared_ptr< sink_t > pSink = boost::make_shared< sink_t >(pBackend);
72 
73     aux::setup_filter(*pSink, args,
74         typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
75 
76     aux::setup_formatter(*pSink, args,
77         typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
78 
79     core::get()->add_sink(pSink);
80 
81     return pSink;
82 }
83 
84 template< typename CharT >
85 struct default_console_stream;
86 
87 #ifdef BOOST_LOG_USE_CHAR
88 template< >
89 struct default_console_stream< char >
90 {
getboost::aux::default_console_stream91     static std::ostream& get() { return std::clog; }
92 };
93 #endif // BOOST_LOG_USE_CHAR
94 
95 #ifdef BOOST_LOG_USE_WCHAR_T
96 template< >
97 struct default_console_stream< wchar_t >
98 {
getboost::aux::default_console_stream99     static std::wostream& get() { return std::wclog; }
100 };
101 #endif // BOOST_LOG_USE_WCHAR_T
102 
103 } // namespace aux
104 
105 #ifndef BOOST_LOG_DOXYGEN_PASS
106 
107 template< typename CharT >
108 inline shared_ptr<
109     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
110         sinks::basic_text_ostream_backend< CharT >
111     >
add_console_log()112 > add_console_log()
113 {
114     return aux::add_console_log(
115         aux::default_console_stream< CharT >::get(), log::aux::empty_arg_list());
116 }
117 
118 
119 template< typename CharT >
120 inline shared_ptr<
121     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
122         sinks::basic_text_ostream_backend< CharT >
123     >
add_console_log(std::basic_ostream<CharT> & strm)124 > add_console_log(std::basic_ostream< CharT >& strm)
125 {
126     return aux::add_console_log(strm, log::aux::empty_arg_list());
127 }
128 
129 template< typename CharT, typename ArgT1 >
130 inline shared_ptr<
131     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
132         sinks::basic_text_ostream_backend< CharT >
133     >
add_console_log(std::basic_ostream<CharT> & strm,ArgT1 const & arg1)134 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1)
135 {
136     return aux::add_console_log(strm, arg1);
137 }
138 
139 template< typename CharT, typename ArgT1, typename ArgT2 >
140 inline shared_ptr<
141     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
142         sinks::basic_text_ostream_backend< CharT >
143     >
add_console_log(std::basic_ostream<CharT> & strm,ArgT1 const & arg1,ArgT2 const & arg2)144 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2)
145 {
146     return aux::add_console_log(strm, (arg1, arg2));
147 }
148 
149 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3 >
150 inline shared_ptr<
151     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
152         sinks::basic_text_ostream_backend< CharT >
153     >
add_console_log(std::basic_ostream<CharT> & strm,ArgT1 const & arg1,ArgT2 const & arg2,ArgT3 const & arg3)154 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3)
155 {
156     return aux::add_console_log(strm, (arg1, arg2, arg3));
157 }
158 
159 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4 >
160 inline shared_ptr<
161     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
162         sinks::basic_text_ostream_backend< CharT >
163     >
add_console_log(std::basic_ostream<CharT> & strm,ArgT1 const & arg1,ArgT2 const & arg2,ArgT3 const & arg3,ArgT3 const & arg4)164 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3, ArgT3 const& arg4)
165 {
166     return aux::add_console_log(strm, (arg1, arg2, arg3, arg4));
167 }
168 
169 #else // BOOST_LOG_DOXYGEN_PASS
170 
171 /*!
172  * The function constructs sink for the specified console stream and adds it to the core
173  *
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.
186  */
187 template< typename CharT, typename... ArgsT >
188 shared_ptr<
189     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
190         sinks::basic_text_ostream_backend< CharT >
191     >
192 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
193 
194 /*!
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.
197  *
198  * \overload
199  */
200 template< typename CharT, typename... ArgsT >
201 shared_ptr<
202     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
203         sinks::basic_text_ostream_backend< CharT >
204     >
205 > add_console_log(ArgsT... const& args);
206 
207 #endif // BOOST_LOG_DOXYGEN_PASS
208 
209 #ifdef BOOST_LOG_USE_CHAR
210 
211 /*!
212  * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
213  *
214  * \overload
215  *
216  * \return Pointer to the constructed sink.
217  */
218 inline shared_ptr<
219     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
220         sinks::text_ostream_backend
221     >
add_console_log()222 > add_console_log()
223 {
224     return add_console_log(std::clog);
225 }
226 
227 #endif // BOOST_LOG_USE_CHAR
228 
229 #ifdef BOOST_LOG_USE_WCHAR_T
230 
231 /*!
232  * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
233  *
234  * \return Pointer to the constructed sink.
235  */
236 inline shared_ptr<
237     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
238         sinks::wtext_ostream_backend
239     >
wadd_console_log()240 > wadd_console_log()
241 {
242     return add_console_log(std::wclog);
243 }
244 
245 #endif // BOOST_LOG_USE_WCHAR_T
246 
247 BOOST_LOG_CLOSE_NAMESPACE // namespace log
248 
249 } // namespace boost
250 
251 #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
252 
253 #include <boost/log/detail/footer.hpp>
254 
255 #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
256