text_ostream_backend.hpp 5.2 KB

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