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 text_ostream_backend.hpp 9 * \author Andrey Semashev 10 * \date 22.04.2007 11 * 12 * The header contains implementation of a text output stream sink backend. 13 */ 14 15 #ifndef BOOST_LOG_SINKS_TEXT_OSTREAM_BACKEND_HPP_INCLUDED_ 16 #define BOOST_LOG_SINKS_TEXT_OSTREAM_BACKEND_HPP_INCLUDED_ 17 18 #include <ostream> 19 #include <boost/smart_ptr/shared_ptr.hpp> 20 #include <boost/log/detail/config.hpp> 21 #include <boost/log/detail/parameter_tools.hpp> 22 #include <boost/log/keywords/auto_flush.hpp> 23 #include <boost/log/keywords/auto_newline_mode.hpp> 24 #include <boost/log/sinks/auto_newline_mode.hpp> 25 #include <boost/log/sinks/basic_sink_backend.hpp> 26 #include <boost/log/sinks/frontend_requirements.hpp> 27 #include <boost/log/detail/header.hpp> 28 29 #ifdef BOOST_HAS_PRAGMA_ONCE 30 #pragma once 31 #endif 32 33 namespace boost { 34 35 BOOST_LOG_OPEN_NAMESPACE 36 37 namespace sinks { 38 39 /*! 40 * \brief An implementation of a text output stream logging sink backend 41 * 42 * The sink backend puts formatted log records to one or more text streams. 43 */ 44 template< typename CharT > 45 class basic_text_ostream_backend : 46 public basic_formatted_sink_backend< 47 CharT, 48 combine_requirements< synchronized_feeding, flushing >::type 49 > 50 { 51 //! Base type 52 typedef basic_formatted_sink_backend< 53 CharT, 54 combine_requirements< synchronized_feeding, flushing >::type 55 > base_type; 56 57 public: 58 //! Character type 59 typedef typename base_type::char_type char_type; 60 //! String type to be used as a message text holder 61 typedef typename base_type::string_type string_type; 62 //! Output stream type 63 typedef std::basic_ostream< char_type > stream_type; 64 65 private: 66 //! \cond 67 68 struct implementation; 69 implementation* m_pImpl; 70 71 //! \endcond 72 73 public: 74 /*! 75 * Constructor. No streams attached to the constructed backend, auto flush feature disabled. 76 */ 77 BOOST_LOG_API basic_text_ostream_backend(); 78 79 /*! 80 * Constructor. Creates a sink backend with the specified named parameters. 81 * The following named parameters are supported: 82 * 83 * \li \c auto_flush - Specifies a flag, whether or not to automatically flush the attached streams after each 84 * written log record. By default, is \c false. 85 * \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of 86 * the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>. 87 */ 88 #ifndef BOOST_LOG_DOXYGEN_PASS 89 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(basic_text_ostream_backend, construct) 90 #else 91 template< typename... ArgsT > 92 explicit basic_text_ostream_backend(ArgsT... const& args); 93 #endif 94 95 /*! 96 * Destructor 97 */ 98 BOOST_LOG_API ~basic_text_ostream_backend(); 99 100 /*! 101 * The method adds a new stream to the sink. 102 * 103 * \param strm Pointer to the stream. Must not be NULL. 104 */ 105 BOOST_LOG_API void add_stream(shared_ptr< stream_type > const& strm); 106 /*! 107 * The method removes a stream from the sink. If the stream is not attached to the sink, 108 * the method has no effect. 109 * 110 * \param strm Pointer to the stream. Must not be NULL. 111 */ 112 BOOST_LOG_API void remove_stream(shared_ptr< stream_type > const& strm); 113 114 /*! 115 * Sets the flag to automatically flush buffers of all attached streams after each log record. 116 * 117 * \param enable The flag indicates whether the automatic buffer flush should be performed. 118 */ 119 BOOST_LOG_API void auto_flush(bool enable = true); 120 121 /*! 122 * Selects whether a trailing newline should be automatically inserted after every log record. See 123 * \c auto_newline_mode description for the possible modes of operation. 124 * 125 * \param mode The trailing newline insertion mode. 126 */ 127 BOOST_LOG_API void set_auto_newline_mode(auto_newline_mode mode); 128 129 /*! 130 * The method writes the message to the sink. 131 */ 132 BOOST_LOG_API void consume(record_view const& rec, string_type const& formatted_message); 133 134 /*! 135 * The method flushes all attached streams. 136 */ 137 BOOST_LOG_API void flush(); 138 139 private: 140 #ifndef BOOST_LOG_DOXYGEN_PASS 141 //! Constructor implementation 142 template< typename ArgsT > construct(ArgsT const & args)143 void construct(ArgsT const& args) 144 { 145 construct( 146 args[keywords::auto_newline_mode | insert_if_missing], 147 args[keywords::auto_flush | false]); 148 } 149 //! Constructor implementation 150 BOOST_LOG_API void construct(auto_newline_mode auto_newline, bool auto_flush); 151 #endif // BOOST_LOG_DOXYGEN_PASS 152 }; 153 154 #ifdef BOOST_LOG_USE_CHAR 155 typedef basic_text_ostream_backend< char > text_ostream_backend; //!< Convenience typedef for narrow-character logging 156 #endif 157 #ifdef BOOST_LOG_USE_WCHAR_T 158 typedef basic_text_ostream_backend< wchar_t > wtext_ostream_backend; //!< Convenience typedef for wide-character logging 159 #endif 160 161 } // namespace sinks 162 163 BOOST_LOG_CLOSE_NAMESPACE // namespace log 164 165 } // namespace boost 166 167 #include <boost/log/detail/footer.hpp> 168 169 #endif // BOOST_LOG_SINKS_TEXT_OSTREAM_BACKEND_HPP_INCLUDED_ 170