Back to home page

EIC code displayed by LXR

 
 

    


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

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   file.hpp
0009  * \author Andrey Semashev
0010  * \date   16.05.2008
0011  *
0012  * The header contains implementation of convenience functions for enabling logging to a file.
0013  */
0014 
0015 #ifndef BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
0016 #define BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
0017 
0018 #include <boost/type_traits/is_void.hpp>
0019 #include <boost/type_traits/integral_constant.hpp>
0020 #include <boost/smart_ptr/shared_ptr.hpp>
0021 #include <boost/smart_ptr/make_shared_object.hpp>
0022 #include <boost/parameter/parameters.hpp> // for is_named_argument
0023 #include <boost/preprocessor/control/expr_if.hpp>
0024 #include <boost/preprocessor/comparison/greater.hpp>
0025 #include <boost/preprocessor/punctuation/comma_if.hpp>
0026 #include <boost/preprocessor/repetition/enum_params.hpp>
0027 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
0028 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
0029 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
0030 #include <boost/log/detail/config.hpp>
0031 #include <boost/log/detail/sink_init_helpers.hpp>
0032 #include <boost/log/detail/parameter_tools.hpp>
0033 #include <boost/log/core/core.hpp>
0034 #ifndef BOOST_LOG_NO_THREADS
0035 #include <boost/log/sinks/sync_frontend.hpp>
0036 #else
0037 #include <boost/log/sinks/unlocked_frontend.hpp>
0038 #endif
0039 #include <boost/log/sinks/text_file_backend.hpp>
0040 #include <boost/log/keywords/format.hpp>
0041 #include <boost/log/keywords/filter.hpp>
0042 #include <boost/log/keywords/scan_method.hpp>
0043 #include <boost/log/detail/header.hpp>
0044 
0045 #ifdef BOOST_HAS_PRAGMA_ONCE
0046 #pragma once
0047 #endif
0048 
0049 #ifndef BOOST_LOG_DOXYGEN_PASS
0050 #ifndef BOOST_LOG_NO_THREADS
0051 #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
0052 #else
0053 #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
0054 #endif
0055 #endif // BOOST_LOG_DOXYGEN_PASS
0056 
0057 namespace boost {
0058 
0059 BOOST_LOG_OPEN_NAMESPACE
0060 
0061 namespace aux {
0062 
0063 //! The function creates a file collector according to the specified arguments
0064 template< typename ArgsT >
0065 inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const&, boost::true_type)
0066 {
0067     return shared_ptr< sinks::file::collector >();
0068 }
0069 template< typename ArgsT >
0070 inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const& args, boost::false_type)
0071 {
0072     return sinks::file::make_collector(args);
0073 }
0074 
0075 //! The function constructs the sink and adds it to the core
0076 template< typename ArgsT >
0077 shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT const& args)
0078 {
0079     typedef sinks::text_file_backend backend_t;
0080     shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
0081 
0082     shared_ptr< sinks::file::collector > pCollector = aux::setup_file_collector(args,
0083         typename is_void< typename parameter::binding< ArgsT, keywords::tag::target, void >::type >::type());
0084     if (pCollector)
0085     {
0086         pBackend->set_file_collector(pCollector);
0087         pBackend->scan_for_files(args[keywords::scan_method | sinks::file::scan_matching]);
0088     }
0089 
0090     shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > > pSink =
0091         boost::make_shared< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > >(pBackend);
0092 
0093     aux::setup_filter(*pSink, args,
0094         typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
0095 
0096     aux::setup_formatter(*pSink, args,
0097         typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
0098 
0099     core::get()->add_sink(pSink);
0100 
0101     return pSink;
0102 }
0103 
0104 //! The trait wraps the argument into a file_name named argument, if needed
0105 template< typename T, bool IsNamedArgument = parameter::aux::is_named_argument< T >::value >
0106 struct file_name_param_traits
0107 {
0108     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
0109     {
0110         return aux::add_file_log(file_name_arg);
0111     }
0112 
0113     template< typename ArgsT >
0114     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg, ArgsT const& args)
0115     {
0116         return aux::add_file_log((args, file_name_arg));
0117     }
0118 };
0119 
0120 template< typename T >
0121 struct file_name_param_traits< T, false >
0122 {
0123     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
0124     {
0125         return aux::add_file_log(keywords::file_name = file_name_arg);
0126     }
0127 
0128     template< typename ArgsT >
0129     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg, ArgsT const& args)
0130     {
0131         return aux::add_file_log((args, (keywords::file_name = file_name_arg)));
0132     }
0133 };
0134 
0135 } // namespace aux
0136 
0137 #ifndef BOOST_LOG_DOXYGEN_PASS
0138 
0139 #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\
0140     template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
0141     inline shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg))\
0142     {\
0143         return aux::file_name_param_traits< T0 >::wrap_add_file_log(\
0144             arg0\
0145             BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\
0146             BOOST_PP_EXPR_IF(BOOST_PP_GREATER(n, 1), (BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, arg)))\
0147         );\
0148     }
0149 
0150 BOOST_PP_REPEAT_FROM_TO(1, BOOST_LOG_MAX_PARAMETER_ARGS, BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL, ~)
0151 
0152 #undef BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL
0153 
0154 #else // BOOST_LOG_DOXYGEN_PASS
0155 
0156 /*!
0157  * The function initializes the logging library to write logs to a file stream.
0158  *
0159  * \param args A number of named arguments. The following parameters are supported:
0160  *             \li \c file_name The active file name or its pattern. This parameter is mandatory.
0161  *             \li \c target_file_name - Specifies the target file name pattern to use to rename the log file on rotation,
0162  *                                       before passing it to the file collector. The pattern may contain the same
0163  *                                       placeholders as the \c file_name parameter. By default, no renaming is done,
0164  *                                       i.e. the written log file keeps its name according to \c file_name.
0165  *             \li \c open_mode The mask that describes the open mode for the file. See <tt>std::ios_base::openmode</tt>.
0166  *             \li \c rotation_size The size of the file at which rotation should occur. See <tt>basic_text_file_backend</tt>.
0167  *             \li \c time_based_rotation The predicate for time-based file rotations. See <tt>basic_text_file_backend</tt>.
0168  *             \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the file
0169  *                               after each written record.
0170  *             \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
0171  *                                        the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
0172  *             \li \c target The target directory to store rotated files in. Enables file collector and, if specified, limits associated
0173  *                           with the target directory. See <tt>sinks::file::make_collector</tt>.
0174  *             \li \c max_size The maximum total size of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
0175  *             \li \c min_free_space Minimum free space in the target directory. See <tt>sinks::file::make_collector</tt>.
0176  *             \li \c max_files The maximum total number of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
0177  *             \li \c scan_method The method of scanning the target directory for log files. See <tt>sinks::file::scan_method</tt>.
0178  *             \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
0179  *                           or a filter lambda expression.
0180  *             \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
0181  *                           or a formatter lambda expression (either streaming or Boost.Format-like notation).
0182  *
0183  * \return Pointer to the constructed sink.
0184  *
0185  * \note The \c target named argument is required to enable the file collector and the limits associated with the target directory.
0186  *       If the parameter is not specified, the file collector will not be created and the limits will not be maintained.
0187  */
0188 template< typename... ArgsT >
0189 shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args);
0190 
0191 #endif // BOOST_LOG_DOXYGEN_PASS
0192 
0193 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0194 
0195 } // namespace boost
0196 
0197 #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL
0198 
0199 #include <boost/log/detail/footer.hpp>
0200 
0201 #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_