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