]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
92f5a8d4 | 18 | #include <boost/type_traits/is_void.hpp> |
7c673cae FG |
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 | |
92f5a8d4 | 22 | #include <boost/preprocessor/control/expr_if.hpp> |
7c673cae FG |
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> | |
92f5a8d4 TL |
39 | #include <boost/log/keywords/format.hpp> |
40 | #include <boost/log/keywords/filter.hpp> | |
7c673cae FG |
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 > | |
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 > | |
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 > | |
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 | ||
92f5a8d4 TL |
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 | |
7c673cae | 106 | { |
92f5a8d4 TL |
107 | 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 > | |
113 | 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 | ||
7c673cae | 119 | template< typename T > |
92f5a8d4 | 120 | struct file_name_param_traits< T, false > |
7c673cae | 121 | { |
92f5a8d4 TL |
122 | 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 > | |
128 | 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 | }; | |
7c673cae FG |
133 | |
134 | } // namespace aux | |
135 | ||
136 | #ifndef BOOST_LOG_DOXYGEN_PASS | |
137 | ||
138 | #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\ | |
20effc67 TL |
139 | template< BOOST_PP_ENUM_PARAMS_Z(z, 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_Z(z, n, T, const& arg))\ | |
7c673cae | 141 | {\ |
92f5a8d4 TL |
142 | return aux::file_name_param_traits< T0 >::wrap_add_file_log(\ |
143 | arg0\ | |
7c673cae | 144 | BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\ |
20effc67 | 145 | BOOST_PP_EXPR_IF(BOOST_PP_GREATER(n, 1), (BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, arg)))\ |
92f5a8d4 | 146 | );\ |
7c673cae FG |
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: | |
92f5a8d4 TL |
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. | |
7c673cae FG |
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. | |
92f5a8d4 TL |
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>. | |
1e59de90 TL |
171 | * \li \c target The target directory to store rotated files in. Enables file collector and, if specified, limits associated |
172 | * with the target directory. See <tt>sinks::file::make_collector</tt>. | |
7c673cae FG |
173 | * \li \c max_size The maximum total size of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>. |
174 | * \li \c min_free_space Minimum free space in the target directory. See <tt>sinks::file::make_collector</tt>. | |
175 | * \li \c max_files The maximum total number of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>. | |
176 | * \li \c scan_method The method of scanning the target directory for log files. See <tt>sinks::file::scan_method</tt>. | |
177 | * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter, | |
178 | * or a filter lambda expression. | |
179 | * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter, | |
180 | * or a formatter lambda expression (either streaming or Boost.Format-like notation). | |
1e59de90 | 181 | * |
7c673cae | 182 | * \return Pointer to the constructed sink. |
1e59de90 TL |
183 | * |
184 | * \note The \c target named argument is required to enable the file collector and the limits associated with the target directory. | |
185 | * If the parameter is not specified, the file collector will not be created and the limits will not be maintained. | |
7c673cae FG |
186 | */ |
187 | template< typename... ArgsT > | |
188 | shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args); | |
189 | ||
190 | #endif // BOOST_LOG_DOXYGEN_PASS | |
191 | ||
192 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
193 | ||
194 | } // namespace boost | |
195 | ||
196 | #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL | |
197 | ||
198 | #include <boost/log/detail/footer.hpp> | |
199 | ||
200 | #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_ |