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/smart_ptr/shared_ptr.hpp>
20 #include <boost/smart_ptr/make_shared_object.hpp>
21 #include <boost/core/null_deleter.hpp>
22 #include <boost/log/detail/config.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/keywords/auto_flush.hpp>
33 #include <boost/log/detail/header.hpp>
34 
35 #ifdef BOOST_HAS_PRAGMA_ONCE
36 #pragma once
37 #endif
38 
39 
40 #ifndef BOOST_LOG_DOXYGEN_PASS
41 #ifndef BOOST_LOG_NO_THREADS
42 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
43 #else
44 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
45 #endif
46 #endif // BOOST_LOG_DOXYGEN_PASS
47 
48 namespace boost {
49 
50 BOOST_LOG_OPEN_NAMESPACE
51 
52 namespace aux {
53 
54 // The function creates and initializes the sink
55 template< typename CharT, typename ArgsT >
56 shared_ptr<
57     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
58         sinks::basic_text_ostream_backend< CharT >
59     >
add_console_log(std::basic_ostream<CharT> & strm,ArgsT const & args)60 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
61 {
62     shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter());
63 
64     typedef sinks::basic_text_ostream_backend< CharT > backend_t;
65     shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >();
66 
67     pBackend->add_stream(pStream);
68     pBackend->auto_flush(args[keywords::auto_flush | false]);
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(), keywords::auto_flush = false);
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, keywords::auto_flush = false);
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 #else // BOOST_LOG_DOXYGEN_PASS
160 
161 /*!
162  * The function constructs sink for the specified console stream and adds it to the core
163  *
164  * \param strm One of the standard console streams: <tt>std::cout</tt>, <tt>std::cerr</tt> or <tt>std::clog</tt>
165  *             (or the corresponding wide-character analogues).
166  * \param args Optional additional named arguments for the sink initialization. The following arguments are supported:
167  *             \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
168  *                           or a filter lambda expression.
169  *             \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
170  *                           or a formatter lambda expression (either streaming or Boost.Format-like notation).
171  *             \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream
172  *                               after each written record.
173  * \return Pointer to the constructed sink.
174  */
175 template< typename CharT, typename... ArgsT >
176 shared_ptr<
177     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
178         sinks::basic_text_ostream_backend< CharT >
179     >
180 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
181 
182 /*!
183  * Equivalent to: <tt>add_console_log(std::clog);</tt> or <tt>add_console_log(std::wclog);</tt>,
184  * depending on the \c CharT type.
185  *
186  * \overload
187  */
188 template< typename CharT, typename... ArgsT >
189 shared_ptr<
190     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
191         sinks::basic_text_ostream_backend< CharT >
192     >
193 > add_console_log(ArgsT... const& args);
194 
195 #endif // BOOST_LOG_DOXYGEN_PASS
196 
197 #ifdef BOOST_LOG_USE_CHAR
198 
199 /*!
200  * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
201  *
202  * \overload
203  *
204  * \return Pointer to the constructed sink.
205  */
206 inline shared_ptr<
207     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
208         sinks::text_ostream_backend
209     >
add_console_log()210 > add_console_log()
211 {
212     return add_console_log(std::clog);
213 }
214 
215 #endif // BOOST_LOG_USE_CHAR
216 
217 #ifdef BOOST_LOG_USE_WCHAR_T
218 
219 /*!
220  * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
221  *
222  * \return Pointer to the constructed sink.
223  */
224 inline shared_ptr<
225     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
226         sinks::wtext_ostream_backend
227     >
wadd_console_log()228 > wadd_console_log()
229 {
230     return add_console_log(std::wclog);
231 }
232 
233 #endif // BOOST_LOG_USE_WCHAR_T
234 
235 BOOST_LOG_CLOSE_NAMESPACE // namespace log
236 
237 } // namespace boost
238 
239 #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
240 
241 #include <boost/log/detail/footer.hpp>
242 
243 #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
244