auto_newline.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright Andrey Semashev 2019.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file utility/manipulators/auto_newline.hpp
  9. * \author Andrey Semashev
  10. * \date 23.06.2019
  11. *
  12. * The header contains implementation of a stream manipulator for inserting a newline, unless there is already one inserted.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <boost/log/utility/formatting_ostream_fwd.hpp>
  18. #include <boost/log/detail/header.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. namespace boost {
  23. BOOST_LOG_OPEN_NAMESPACE
  24. /*!
  25. * Stream manipulator for inserting a newline character, unless the last character
  26. * inserted into the stream is already a newline.
  27. */
  28. struct auto_newline_manip {}
  29. const auto_newline = {};
  30. /*!
  31. * Stream output operator for the \c auto_newline manipulator
  32. */
  33. template< typename CharT, typename TraitsT, typename AllocatorT >
  34. inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >& operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, auto_newline_manip)
  35. {
  36. typedef basic_formatting_ostream< CharT, TraitsT, AllocatorT > stream_type;
  37. typedef typename stream_type::char_type char_type;
  38. typedef typename stream_type::string_type string_type;
  39. if (BOOST_LIKELY(strm.good()))
  40. {
  41. string_type* str = strm.rdbuf()->storage();
  42. if (BOOST_LIKELY(!!str))
  43. {
  44. strm.rdbuf()->pubsync();
  45. if (str->empty() || *str->rbegin() != static_cast< char_type >('\n'))
  46. strm.rdbuf()->push_back(static_cast< char_type >('\n'));
  47. }
  48. }
  49. return strm;
  50. }
  51. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  52. } // namespace boost
  53. #include <boost/log/detail/footer.hpp>
  54. #endif // BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_