Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:28

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   console.hpp
0009  * \author Andrey Semashev
0010  * \date   16.05.2008
0011  *
0012  * The header contains implementation of convenience functions for enabling logging to console.
0013  */
0014 
0015 #ifndef BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
0016 #define BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_
0017 
0018 #include <iostream>
0019 #include <boost/type_traits/is_void.hpp>
0020 #include <boost/smart_ptr/shared_ptr.hpp>
0021 #include <boost/smart_ptr/make_shared_object.hpp>
0022 #include <boost/core/null_deleter.hpp>
0023 #include <boost/log/detail/config.hpp>
0024 #include <boost/log/detail/parameter_tools.hpp>
0025 #include <boost/log/detail/sink_init_helpers.hpp>
0026 #ifndef BOOST_LOG_NO_THREADS
0027 #include <boost/log/sinks/sync_frontend.hpp>
0028 #else
0029 #include <boost/log/sinks/unlocked_frontend.hpp>
0030 #endif
0031 #include <boost/log/sinks/text_ostream_backend.hpp>
0032 #include <boost/log/keywords/format.hpp>
0033 #include <boost/log/keywords/filter.hpp>
0034 #include <boost/log/detail/header.hpp>
0035 
0036 #ifdef BOOST_HAS_PRAGMA_ONCE
0037 #pragma once
0038 #endif
0039 
0040 
0041 #ifndef BOOST_LOG_DOXYGEN_PASS
0042 #ifndef BOOST_LOG_NO_THREADS
0043 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
0044 #else
0045 #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
0046 #endif
0047 #endif // BOOST_LOG_DOXYGEN_PASS
0048 
0049 namespace boost {
0050 
0051 BOOST_LOG_OPEN_NAMESPACE
0052 
0053 namespace aux {
0054 
0055 // The function creates and initializes the sink
0056 template< typename CharT, typename ArgsT >
0057 shared_ptr<
0058     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0059         sinks::basic_text_ostream_backend< CharT >
0060     >
0061 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args)
0062 {
0063     shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter());
0064 
0065     typedef sinks::basic_text_ostream_backend< CharT > backend_t;
0066     shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
0067 
0068     pBackend->add_stream(pStream);
0069 
0070     typedef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< backend_t > sink_t;
0071     shared_ptr< sink_t > pSink = boost::make_shared< sink_t >(pBackend);
0072 
0073     aux::setup_filter(*pSink, args,
0074         typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
0075 
0076     aux::setup_formatter(*pSink, args,
0077         typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
0078 
0079     core::get()->add_sink(pSink);
0080 
0081     return pSink;
0082 }
0083 
0084 template< typename CharT >
0085 struct default_console_stream;
0086 
0087 #ifdef BOOST_LOG_USE_CHAR
0088 template< >
0089 struct default_console_stream< char >
0090 {
0091     static std::ostream& get() { return std::clog; }
0092 };
0093 #endif // BOOST_LOG_USE_CHAR
0094 
0095 #ifdef BOOST_LOG_USE_WCHAR_T
0096 template< >
0097 struct default_console_stream< wchar_t >
0098 {
0099     static std::wostream& get() { return std::wclog; }
0100 };
0101 #endif // BOOST_LOG_USE_WCHAR_T
0102 
0103 } // namespace aux
0104 
0105 #ifndef BOOST_LOG_DOXYGEN_PASS
0106 
0107 template< typename CharT >
0108 inline shared_ptr<
0109     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0110         sinks::basic_text_ostream_backend< CharT >
0111     >
0112 > add_console_log()
0113 {
0114     return aux::add_console_log(
0115         aux::default_console_stream< CharT >::get(), log::aux::empty_arg_list());
0116 }
0117 
0118 
0119 template< typename CharT >
0120 inline shared_ptr<
0121     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0122         sinks::basic_text_ostream_backend< CharT >
0123     >
0124 > add_console_log(std::basic_ostream< CharT >& strm)
0125 {
0126     return aux::add_console_log(strm, log::aux::empty_arg_list());
0127 }
0128 
0129 template< typename CharT, typename ArgT1 >
0130 inline shared_ptr<
0131     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0132         sinks::basic_text_ostream_backend< CharT >
0133     >
0134 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1)
0135 {
0136     return aux::add_console_log(strm, arg1);
0137 }
0138 
0139 template< typename CharT, typename ArgT1, typename ArgT2 >
0140 inline shared_ptr<
0141     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0142         sinks::basic_text_ostream_backend< CharT >
0143     >
0144 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2)
0145 {
0146     return aux::add_console_log(strm, (arg1, arg2));
0147 }
0148 
0149 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3 >
0150 inline shared_ptr<
0151     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0152         sinks::basic_text_ostream_backend< CharT >
0153     >
0154 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3)
0155 {
0156     return aux::add_console_log(strm, (arg1, arg2, arg3));
0157 }
0158 
0159 template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4 >
0160 inline shared_ptr<
0161     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0162         sinks::basic_text_ostream_backend< CharT >
0163     >
0164 > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3, ArgT3 const& arg4)
0165 {
0166     return aux::add_console_log(strm, (arg1, arg2, arg3, arg4));
0167 }
0168 
0169 #else // BOOST_LOG_DOXYGEN_PASS
0170 
0171 /*!
0172  * The function constructs sink for the specified console stream and adds it to the core
0173  *
0174  * \param strm One of the standard console streams: <tt>std::cout</tt>, <tt>std::cerr</tt> or <tt>std::clog</tt>
0175  *             (or the corresponding wide-character analogues).
0176  * \param args Optional additional named arguments for the sink initialization. The following arguments are supported:
0177  *             \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
0178  *                           or a filter lambda expression.
0179  *             \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
0180  *                           or a formatter lambda expression (either streaming or Boost.Format-like notation).
0181  *             \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream
0182  *                               after each written record.
0183  *             \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
0184  *                                        the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
0185  * \return Pointer to the constructed sink.
0186  */
0187 template< typename CharT, typename... ArgsT >
0188 shared_ptr<
0189     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0190         sinks::basic_text_ostream_backend< CharT >
0191     >
0192 > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args);
0193 
0194 /*!
0195  * Equivalent to: <tt>add_console_log(std::clog);</tt> or <tt>add_console_log(std::wclog);</tt>,
0196  * depending on the \c CharT type.
0197  *
0198  * \overload
0199  */
0200 template< typename CharT, typename... ArgsT >
0201 shared_ptr<
0202     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0203         sinks::basic_text_ostream_backend< CharT >
0204     >
0205 > add_console_log(ArgsT... const& args);
0206 
0207 #endif // BOOST_LOG_DOXYGEN_PASS
0208 
0209 #ifdef BOOST_LOG_USE_CHAR
0210 
0211 /*!
0212  * The function constructs sink for the <tt>std::clog</tt> stream and adds it to the core
0213  *
0214  * \overload
0215  *
0216  * \return Pointer to the constructed sink.
0217  */
0218 inline shared_ptr<
0219     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0220         sinks::text_ostream_backend
0221     >
0222 > add_console_log()
0223 {
0224     return add_console_log(std::clog);
0225 }
0226 
0227 #endif // BOOST_LOG_USE_CHAR
0228 
0229 #ifdef BOOST_LOG_USE_WCHAR_T
0230 
0231 /*!
0232  * The function constructs sink for the <tt>std::wclog</tt> stream and adds it to the core
0233  *
0234  * \return Pointer to the constructed sink.
0235  */
0236 inline shared_ptr<
0237     BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL<
0238         sinks::wtext_ostream_backend
0239     >
0240 > wadd_console_log()
0241 {
0242     return add_console_log(std::wclog);
0243 }
0244 
0245 #endif // BOOST_LOG_USE_WCHAR_T
0246 
0247 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0248 
0249 } // namespace boost
0250 
0251 #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL
0252 
0253 #include <boost/log/detail/footer.hpp>
0254 
0255 #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_