Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file console.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 16.05.2008 Chris@16: * Chris@16: * The header contains implementation of convenience functions for enabling logging to console. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #ifndef BOOST_LOG_NO_THREADS Chris@16: #include Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: #ifndef BOOST_LOG_NO_THREADS Chris@16: #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink Chris@16: #else Chris@16: #define BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink Chris@16: #endif Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: // The function creates and initializes the sink Chris@16: template< typename CharT, typename ArgsT > Chris@16: shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm, ArgsT const& args) Chris@16: { Chris@101: shared_ptr< std::basic_ostream< CharT > > pStream(&strm, boost::null_deleter()); Chris@16: Chris@16: typedef sinks::basic_text_ostream_backend< CharT > backend_t; Chris@16: shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(); Chris@16: Chris@16: pBackend->add_stream(pStream); Chris@16: pBackend->auto_flush(args[keywords::auto_flush | false]); Chris@16: Chris@16: typedef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< backend_t > sink_t; Chris@16: shared_ptr< sink_t > pSink = boost::make_shared< sink_t >(pBackend); Chris@16: Chris@16: aux::setup_filter(*pSink, args, Chris@16: typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type()); Chris@16: Chris@16: aux::setup_formatter(*pSink, args, Chris@16: typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type()); Chris@16: Chris@16: core::get()->add_sink(pSink); Chris@16: Chris@16: return pSink; Chris@16: } Chris@16: Chris@16: template< typename CharT > Chris@16: struct default_console_stream; Chris@16: Chris@16: #ifdef BOOST_LOG_USE_CHAR Chris@16: template< > Chris@16: struct default_console_stream< char > Chris@16: { Chris@16: static std::ostream& get() { return std::clog; } Chris@16: }; Chris@16: #endif // BOOST_LOG_USE_CHAR Chris@16: Chris@16: #ifdef BOOST_LOG_USE_WCHAR_T Chris@16: template< > Chris@16: struct default_console_stream< wchar_t > Chris@16: { Chris@16: static std::wostream& get() { return std::wclog; } Chris@16: }; Chris@16: #endif // BOOST_LOG_USE_WCHAR_T Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: template< typename CharT > Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log() Chris@16: { Chris@16: return aux::add_console_log( Chris@16: aux::default_console_stream< CharT >::get(), keywords::auto_flush = false); Chris@16: } Chris@16: Chris@16: Chris@16: template< typename CharT > Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm) Chris@16: { Chris@16: return aux::add_console_log(strm, keywords::auto_flush = false); Chris@16: } Chris@16: Chris@16: template< typename CharT, typename ArgT1 > Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1) Chris@16: { Chris@16: return aux::add_console_log(strm, arg1); Chris@16: } Chris@16: Chris@16: template< typename CharT, typename ArgT1, typename ArgT2 > Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2) Chris@16: { Chris@16: return aux::add_console_log(strm, (arg1, arg2)); Chris@16: } Chris@16: Chris@16: template< typename CharT, typename ArgT1, typename ArgT2, typename ArgT3 > Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm, ArgT1 const& arg1, ArgT2 const& arg2, ArgT3 const& arg3) Chris@16: { Chris@16: return aux::add_console_log(strm, (arg1, arg2, arg3)); Chris@16: } Chris@16: Chris@16: #else // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: /*! Chris@16: * The function constructs sink for the specified console stream and adds it to the core Chris@16: * Chris@16: * \param strm One of the standard console streams: std::cout, std::cerr or std::clog Chris@16: * (or the corresponding wide-character analogues). Chris@16: * \param args Optional additional named arguments for the sink initialization. The following arguments are supported: Chris@16: * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter, Chris@16: * or a filter lambda expression. Chris@16: * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter, Chris@16: * or a formatter lambda expression (either streaming or Boost.Format-like notation). Chris@16: * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the stream Chris@16: * after each written record. Chris@16: * \return Pointer to the constructed sink. Chris@16: */ Chris@16: template< typename CharT, typename... ArgsT > Chris@16: shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(std::basic_ostream< CharT >& strm, ArgsT... const& args); Chris@16: Chris@16: /*! Chris@16: * Equivalent to: add_console_log(std::clog); or add_console_log(std::wclog);, Chris@16: * depending on the \c CharT type. Chris@16: * Chris@16: * \overload Chris@16: */ Chris@16: template< typename CharT, typename... ArgsT > Chris@16: shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::basic_text_ostream_backend< CharT > Chris@16: > Chris@16: > add_console_log(ArgsT... const& args); Chris@16: Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: #ifdef BOOST_LOG_USE_CHAR Chris@16: Chris@16: /*! Chris@16: * The function constructs sink for the std::clog stream and adds it to the core Chris@16: * Chris@16: * \overload Chris@16: * Chris@16: * \return Pointer to the constructed sink. Chris@16: */ Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::text_ostream_backend Chris@16: > Chris@16: > add_console_log() Chris@16: { Chris@16: return add_console_log(std::clog); Chris@16: } Chris@16: Chris@16: #endif // BOOST_LOG_USE_CHAR Chris@16: Chris@16: #ifdef BOOST_LOG_USE_WCHAR_T Chris@16: Chris@16: /*! Chris@16: * The function constructs sink for the std::wclog stream and adds it to the core Chris@16: * Chris@16: * \return Pointer to the constructed sink. Chris@16: */ Chris@16: inline shared_ptr< Chris@16: BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL< Chris@16: sinks::wtext_ostream_backend Chris@16: > Chris@16: > wadd_console_log() Chris@16: { Chris@16: return add_console_log(std::wclog); Chris@16: } Chris@16: Chris@16: #endif // BOOST_LOG_USE_WCHAR_T Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #undef BOOST_LOG_CONSOLE_SINK_FRONTEND_INTERNAL Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_UTILITY_SETUP_CONSOLE_HPP_INCLUDED_