Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:25

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   text_multifile_backend.hpp
0009  * \author Andrey Semashev
0010  * \date   09.06.2009
0011  *
0012  * The header contains implementation of a text multi-file sink backend.
0013  */
0014 
0015 #ifndef BOOST_LOG_SINKS_TEXT_MULTIFILE_BACKEND_HPP_INCLUDED_
0016 #define BOOST_LOG_SINKS_TEXT_MULTIFILE_BACKEND_HPP_INCLUDED_
0017 
0018 #include <ios>
0019 #include <string>
0020 #include <locale>
0021 #include <ostream>
0022 #include <boost/mpl/if.hpp>
0023 #include <boost/mpl/bool.hpp>
0024 #include <boost/type_traits/is_same.hpp>
0025 #include <boost/filesystem/path.hpp>
0026 #include <boost/log/detail/config.hpp>
0027 #include <boost/log/detail/light_function.hpp>
0028 #include <boost/log/detail/parameter_tools.hpp>
0029 #include <boost/log/detail/cleanup_scope_guard.hpp>
0030 #include <boost/log/keywords/auto_newline_mode.hpp>
0031 #include <boost/log/sinks/auto_newline_mode.hpp>
0032 #include <boost/log/sinks/basic_sink_backend.hpp>
0033 #include <boost/log/utility/formatting_ostream.hpp>
0034 #include <boost/log/detail/header.hpp>
0035 
0036 #ifdef BOOST_HAS_PRAGMA_ONCE
0037 #pragma once
0038 #endif
0039 
0040 namespace boost {
0041 
0042 BOOST_LOG_OPEN_NAMESPACE
0043 
0044 namespace sinks {
0045 
0046 namespace file {
0047 
0048     /*!
0049      * An adapter class that allows to use regular formatters as file name generators.
0050      */
0051     template< typename FormatterT >
0052     class file_name_composer_adapter
0053     {
0054     public:
0055         //! Functor result type
0056         typedef filesystem::path result_type;
0057         //! File name character type
0058         typedef result_type::string_type::value_type native_char_type;
0059         //! The adopted formatter type
0060         typedef FormatterT formatter_type;
0061         //! Formatting stream type
0062         typedef basic_formatting_ostream< native_char_type > stream_type;
0063 
0064     private:
0065         //! The adopted formatter
0066         formatter_type m_Formatter;
0067         //! Formatted file name storage
0068         mutable result_type::string_type m_FileName;
0069         //! Formatting stream
0070         mutable stream_type m_FormattingStream;
0071 
0072     public:
0073         /*!
0074          * Initializing constructor
0075          */
0076         explicit file_name_composer_adapter(formatter_type const& formatter, std::locale const& loc = std::locale()) :
0077             m_Formatter(formatter),
0078             m_FormattingStream(m_FileName)
0079         {
0080             m_FormattingStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
0081             m_FormattingStream.imbue(loc);
0082         }
0083         /*!
0084          * Copy constructor
0085          */
0086         file_name_composer_adapter(file_name_composer_adapter const& that) :
0087             m_Formatter(that.m_Formatter),
0088             m_FormattingStream(m_FileName)
0089         {
0090             m_FormattingStream.exceptions(std::ios_base::badbit | std::ios_base::failbit);
0091             m_FormattingStream.imbue(that.m_FormattingStream.getloc());
0092         }
0093         /*!
0094          * Assignment
0095          */
0096         file_name_composer_adapter& operator= (file_name_composer_adapter const& that)
0097         {
0098             m_Formatter = that.m_Formatter;
0099             return *this;
0100         }
0101 
0102         /*!
0103          * The operator generates a file name based on the log record
0104          */
0105         result_type operator() (record_view const& rec) const
0106         {
0107             boost::log::aux::cleanup_guard< stream_type > cleanup1(m_FormattingStream);
0108             boost::log::aux::cleanup_guard< result_type::string_type > cleanup2(m_FileName);
0109 
0110             m_Formatter(rec, m_FormattingStream);
0111             m_FormattingStream.flush();
0112 
0113             return result_type(m_FileName);
0114         }
0115     };
0116 
0117     /*!
0118      * The function adopts a log record formatter into a file name generator
0119      *
0120      * \param fmt The formatter function object to adopt
0121      * \param loc The locale to use to character code conversion and formatting
0122      */
0123     template< typename FormatterT >
0124     inline file_name_composer_adapter< FormatterT > as_file_name_composer(
0125         FormatterT const& fmt, std::locale const& loc = std::locale())
0126     {
0127         return file_name_composer_adapter< FormatterT >(fmt, loc);
0128     }
0129 
0130 } // namespace file
0131 
0132 
0133 /*!
0134  * \brief An implementation of a text multiple files logging sink backend
0135  *
0136  * The sink backend puts formatted log records to one of the text files.
0137  * The particular file is chosen upon each record's attribute values, which allows
0138  * to distribute records into individual files or to group records related to
0139  * some entity or process in a separate file.
0140  */
0141 class text_multifile_backend :
0142     public basic_formatted_sink_backend< char >
0143 {
0144     //! Base type
0145     typedef basic_formatted_sink_backend< char > base_type;
0146 
0147 public:
0148     //! Character type
0149     typedef base_type::char_type char_type;
0150     //! String type to be used as a message text holder
0151     typedef base_type::string_type string_type;
0152 
0153     //! File name composer functor type
0154     typedef boost::log::aux::light_function< filesystem::path (record_view const&) > file_name_composer_type;
0155 
0156 private:
0157     //! \cond
0158 
0159     struct implementation;
0160     implementation* m_pImpl;
0161 
0162     //! \endcond
0163 
0164 public:
0165     /*!
0166      * Default constructor. The constructed sink backend has no file name composer and
0167      * thus will not write any files. All other parameters are set to their defaults.
0168      */
0169     BOOST_LOG_API text_multifile_backend();
0170 
0171     /*!
0172      * Constructor. Creates a sink backend with the specified named parameters.
0173      * The following named parameters are supported:
0174      *
0175      * \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
0176      *                            the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
0177      */
0178 #ifndef BOOST_LOG_DOXYGEN_PASS
0179     BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(text_multifile_backend, construct)
0180 #else
0181     template< typename... ArgsT >
0182     explicit text_multifile_backend(ArgsT... const& args);
0183 #endif
0184 
0185     /*!
0186      * Destructor
0187      */
0188     BOOST_LOG_API ~text_multifile_backend();
0189 
0190     /*!
0191      * The method sets file name composer functional object. Log record formatters are accepted, too.
0192      *
0193      * \param composer File name composer functor
0194      */
0195     template< typename ComposerT >
0196     void set_file_name_composer(ComposerT const& composer)
0197     {
0198         set_file_name_composer_internal(composer);
0199     }
0200 
0201     /*!
0202      * Selects whether a trailing newline should be automatically inserted after every log record. See
0203      * \c auto_newline_mode description for the possible modes of operation.
0204      *
0205      * \param mode The trailing newline insertion mode.
0206      */
0207     BOOST_LOG_API void set_auto_newline_mode(auto_newline_mode mode);
0208 
0209     /*!
0210      * The method writes the message to the sink
0211      */
0212     BOOST_LOG_API void consume(record_view const& rec, string_type const& formatted_message);
0213 
0214 private:
0215 #ifndef BOOST_LOG_DOXYGEN_PASS
0216     //! Constructor implementation
0217     template< typename ArgsT >
0218     void construct(ArgsT const& args)
0219     {
0220         construct(args[keywords::auto_newline_mode | insert_if_missing]);
0221     }
0222     //! Constructor implementation
0223     BOOST_LOG_API void construct(auto_newline_mode auto_newline);
0224 
0225     //! The method sets the file name composer
0226     BOOST_LOG_API void set_file_name_composer_internal(file_name_composer_type const& composer);
0227 #endif // BOOST_LOG_DOXYGEN_PASS
0228 };
0229 
0230 } // namespace sinks
0231 
0232 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0233 
0234 } // namespace boost
0235 
0236 #include <boost/log/detail/footer.hpp>
0237 
0238 #endif // BOOST_LOG_SINKS_TEXT_MULTIFILE_BACKEND_HPP_INCLUDED_