• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
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   file.hpp
9  * \author Andrey Semashev
10  * \date   16.05.2008
11  *
12  * The header contains implementation of convenience functions for enabling logging to a file.
13  */
14 
15 #ifndef BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
17 
18 #include <boost/type_traits/is_void.hpp>
19 #include <boost/smart_ptr/shared_ptr.hpp>
20 #include <boost/smart_ptr/make_shared_object.hpp>
21 #include <boost/parameter/parameters.hpp> // for is_named_argument
22 #include <boost/preprocessor/control/expr_if.hpp>
23 #include <boost/preprocessor/comparison/greater.hpp>
24 #include <boost/preprocessor/punctuation/comma_if.hpp>
25 #include <boost/preprocessor/repetition/enum_params.hpp>
26 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
27 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
28 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
29 #include <boost/log/detail/config.hpp>
30 #include <boost/log/detail/sink_init_helpers.hpp>
31 #include <boost/log/detail/parameter_tools.hpp>
32 #include <boost/log/core/core.hpp>
33 #ifndef BOOST_LOG_NO_THREADS
34 #include <boost/log/sinks/sync_frontend.hpp>
35 #else
36 #include <boost/log/sinks/unlocked_frontend.hpp>
37 #endif
38 #include <boost/log/sinks/text_file_backend.hpp>
39 #include <boost/log/keywords/format.hpp>
40 #include <boost/log/keywords/filter.hpp>
41 #include <boost/log/keywords/scan_method.hpp>
42 #include <boost/log/detail/header.hpp>
43 
44 #ifdef BOOST_HAS_PRAGMA_ONCE
45 #pragma once
46 #endif
47 
48 #ifndef BOOST_LOG_DOXYGEN_PASS
49 #ifndef BOOST_LOG_NO_THREADS
50 #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
51 #else
52 #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
53 #endif
54 #endif // BOOST_LOG_DOXYGEN_PASS
55 
56 namespace boost {
57 
58 BOOST_LOG_OPEN_NAMESPACE
59 
60 namespace aux {
61 
62 //! The function creates a file collector according to the specified arguments
63 template< typename ArgsT >
setup_file_collector(ArgsT const &,mpl::true_ const &)64 inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const&, mpl::true_ const&)
65 {
66     return shared_ptr< sinks::file::collector >();
67 }
68 template< typename ArgsT >
setup_file_collector(ArgsT const & args,mpl::false_ const &)69 inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const& args, mpl::false_ const&)
70 {
71     return sinks::file::make_collector(args);
72 }
73 
74 //! The function constructs the sink and adds it to the core
75 template< typename ArgsT >
add_file_log(ArgsT const & args)76 shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT const& args)
77 {
78     typedef sinks::text_file_backend backend_t;
79     shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
80 
81     shared_ptr< sinks::file::collector > pCollector = aux::setup_file_collector(args,
82         typename is_void< typename parameter::binding< ArgsT, keywords::tag::target, void >::type >::type());
83     if (pCollector)
84     {
85         pBackend->set_file_collector(pCollector);
86         pBackend->scan_for_files(args[keywords::scan_method | sinks::file::scan_matching]);
87     }
88 
89     shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > > pSink =
90         boost::make_shared< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > >(pBackend);
91 
92     aux::setup_filter(*pSink, args,
93         typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
94 
95     aux::setup_formatter(*pSink, args,
96         typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
97 
98     core::get()->add_sink(pSink);
99 
100     return pSink;
101 }
102 
103 //! The trait wraps the argument into a file_name named argument, if needed
104 template< typename T, bool IsNamedArgument = parameter::aux::is_named_argument< T >::value >
105 struct file_name_param_traits
106 {
wrap_add_file_logboost::aux::file_name_param_traits107     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
108     {
109         return aux::add_file_log(file_name_arg);
110     }
111 
112     template< typename ArgsT >
wrap_add_file_logboost::aux::file_name_param_traits113     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)
114     {
115         return aux::add_file_log((args, file_name_arg));
116     }
117 };
118 
119 template< typename T >
120 struct file_name_param_traits< T, false >
121 {
wrap_add_file_logboost::aux::file_name_param_traits122     static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
123     {
124         return aux::add_file_log(keywords::file_name = file_name_arg);
125     }
126 
127     template< typename ArgsT >
wrap_add_file_logboost::aux::file_name_param_traits128     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)
129     {
130         return aux::add_file_log((args, (keywords::file_name = file_name_arg)));
131     }
132 };
133 
134 } // namespace aux
135 
136 #ifndef BOOST_LOG_DOXYGEN_PASS
137 
138 #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\
139     template< BOOST_PP_ENUM_PARAMS(n, typename T) >\
140     inline shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg))\
141     {\
142         return aux::file_name_param_traits< T0 >::wrap_add_file_log(\
143             arg0\
144             BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\
145             BOOST_PP_EXPR_IF(BOOST_PP_GREATER(n, 1), (BOOST_PP_ENUM_SHIFTED_PARAMS(n, arg)))\
146         );\
147     }
148 
149 BOOST_PP_REPEAT_FROM_TO(1, BOOST_LOG_MAX_PARAMETER_ARGS, BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL, ~)
150 
151 #undef BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL
152 
153 #else // BOOST_LOG_DOXYGEN_PASS
154 
155 /*!
156  * The function initializes the logging library to write logs to a file stream.
157  *
158  * \param args A number of named arguments. The following parameters are supported:
159  *             \li \c file_name The active file name or its pattern. This parameter is mandatory.
160  *             \li \c target_file_name - Specifies the target file name pattern to use to rename the log file on rotation,
161  *                                       before passing it to the file collector. The pattern may contain the same
162  *                                       placeholders as the \c file_name parameter. By default, no renaming is done,
163  *                                       i.e. the written log file keeps its name according to \c file_name.
164  *             \li \c open_mode The mask that describes the open mode for the file. See <tt>std::ios_base::openmode</tt>.
165  *             \li \c rotation_size The size of the file at which rotation should occur. See <tt>basic_text_file_backend</tt>.
166  *             \li \c time_based_rotation The predicate for time-based file rotations. See <tt>basic_text_file_backend</tt>.
167  *             \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the file
168  *                               after each written record.
169  *             \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
170  *                                        the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
171  *             \li \c target The target directory to store rotated files in. See <tt>sinks::file::make_collector</tt>.
172  *             \li \c max_size The maximum total size of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
173  *             \li \c min_free_space Minimum free space in the target directory. See <tt>sinks::file::make_collector</tt>.
174  *             \li \c max_files The maximum total number of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
175  *             \li \c scan_method The method of scanning the target directory for log files. See <tt>sinks::file::scan_method</tt>.
176  *             \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
177  *                           or a filter lambda expression.
178  *             \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
179  *                           or a formatter lambda expression (either streaming or Boost.Format-like notation).
180  * \return Pointer to the constructed sink.
181  */
182 template< typename... ArgsT >
183 shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args);
184 
185 #endif // BOOST_LOG_DOXYGEN_PASS
186 
187 BOOST_LOG_CLOSE_NAMESPACE // namespace log
188 
189 } // namespace boost
190 
191 #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL
192 
193 #include <boost/log/detail/footer.hpp>
194 
195 #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
196