text_ostream_backend.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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/sinks/basic_sink_backend.hpp>
  20. #include <boost/log/sinks/frontend_requirements.hpp>
  21. #include <boost/log/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. namespace sinks {
  28. /*!
  29. * \brief An implementation of a text output stream logging sink backend
  30. *
  31. * The sink backend puts formatted log records to one or more text streams.
  32. */
  33. template< typename CharT >
  34. class basic_text_ostream_backend :
  35. public basic_formatted_sink_backend<
  36. CharT,
  37. combine_requirements< synchronized_feeding, flushing >::type
  38. >
  39. {
  40. //! Base type
  41. typedef basic_formatted_sink_backend<
  42. CharT,
  43. combine_requirements< synchronized_feeding, flushing >::type
  44. > base_type;
  45. public:
  46. //! Character type
  47. typedef typename base_type::char_type char_type;
  48. //! String type to be used as a message text holder
  49. typedef typename base_type::string_type string_type;
  50. //! Output stream type
  51. typedef std::basic_ostream< char_type > stream_type;
  52. private:
  53. //! \cond
  54. struct implementation;
  55. implementation* m_pImpl;
  56. //! \endcond
  57. public:
  58. /*!
  59. * Constructor. No streams attached to the constructed backend, auto flush feature disabled.
  60. */
  61. BOOST_LOG_API basic_text_ostream_backend();
  62. /*!
  63. * Destructor
  64. */
  65. BOOST_LOG_API ~basic_text_ostream_backend();
  66. /*!
  67. * The method adds a new stream to the sink.
  68. *
  69. * \param strm Pointer to the stream. Must not be NULL.
  70. */
  71. BOOST_LOG_API void add_stream(shared_ptr< stream_type > const& strm);
  72. /*!
  73. * The method removes a stream from the sink. If the stream is not attached to the sink,
  74. * the method has no effect.
  75. *
  76. * \param strm Pointer to the stream. Must not be NULL.
  77. */
  78. BOOST_LOG_API void remove_stream(shared_ptr< stream_type > const& strm);
  79. /*!
  80. * Sets the flag to automatically flush buffers of all attached streams after each log record
  81. */
  82. BOOST_LOG_API void auto_flush(bool f = true);
  83. /*!
  84. * The method writes the message to the sink
  85. */
  86. BOOST_LOG_API void consume(record_view const& rec, string_type const& formatted_message);
  87. /*!
  88. * The method flushes the associated streams
  89. */
  90. BOOST_LOG_API void flush();
  91. };
  92. #ifdef BOOST_LOG_USE_CHAR
  93. typedef basic_text_ostream_backend< char > text_ostream_backend; //!< Convenience typedef for narrow-character logging
  94. #endif
  95. #ifdef BOOST_LOG_USE_WCHAR_T
  96. typedef basic_text_ostream_backend< wchar_t > wtext_ostream_backend; //!< Convenience typedef for wide-character logging
  97. #endif
  98. } // namespace sinks
  99. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  100. } // namespace boost
  101. #include <boost/log/detail/footer.hpp>
  102. #endif // BOOST_LOG_SINKS_TEXT_OSTREAM_BACKEND_HPP_INCLUDED_