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)
8 * \file utility/manipulators/auto_newline.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a stream manipulator for inserting a newline, unless there is already one inserted.
15 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_
18 #include <boost/log/detail/config.hpp>
19 #include <boost/log/utility/formatting_ostream_fwd.hpp>
20 #include <boost/log/detail/header.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
28 BOOST_LOG_OPEN_NAMESPACE
31 * Stream manipulator for inserting a newline character, unless the last character
32 * inserted into the stream is already a newline.
34 struct auto_newline_manip {}
35 BOOST_INLINE_VARIABLE const auto_newline = {};
38 * Stream output operator for the \c auto_newline manipulator
40 template< typename CharT, typename TraitsT, typename AllocatorT >
41 inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >& operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, auto_newline_manip)
43 typedef basic_formatting_ostream< CharT, TraitsT, AllocatorT > stream_type;
44 typedef typename stream_type::char_type char_type;
45 typedef typename stream_type::string_type string_type;
46 if (BOOST_LIKELY(strm.good()))
48 string_type* str = strm.rdbuf()->storage();
49 if (BOOST_LIKELY(!!str))
51 strm.rdbuf()->pubsync();
52 if (str->empty() || *str->rbegin() != static_cast< char_type >('\n'))
53 strm.rdbuf()->push_back(static_cast< char_type >('\n'));
60 BOOST_LOG_CLOSE_NAMESPACE // namespace log
64 #include <boost/log/detail/footer.hpp>
66 #endif // BOOST_LOG_UTILITY_MANIPULATORS_AUTO_NEWLINE_HPP_INCLUDED_