]>
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 | ||
18 | #include <boost/smart_ptr/shared_ptr.hpp> | |
19 | #include <boost/smart_ptr/make_shared_object.hpp> | |
20 | #include <boost/parameter/parameters.hpp> // for is_named_argument | |
21 | #include <boost/preprocessor/comparison/greater.hpp> | |
22 | #include <boost/preprocessor/punctuation/comma_if.hpp> | |
23 | #include <boost/preprocessor/repetition/enum_params.hpp> | |
24 | #include <boost/preprocessor/repetition/enum_binary_params.hpp> | |
25 | #include <boost/preprocessor/repetition/enum_shifted_params.hpp> | |
26 | #include <boost/preprocessor/repetition/repeat_from_to.hpp> | |
27 | #include <boost/log/detail/config.hpp> | |
28 | #include <boost/log/detail/sink_init_helpers.hpp> | |
29 | #include <boost/log/detail/parameter_tools.hpp> | |
30 | #include <boost/log/core/core.hpp> | |
31 | #ifndef BOOST_LOG_NO_THREADS | |
32 | #include <boost/log/sinks/sync_frontend.hpp> | |
33 | #else | |
34 | #include <boost/log/sinks/unlocked_frontend.hpp> | |
35 | #endif | |
36 | #include <boost/log/sinks/text_file_backend.hpp> | |
37 | #include <boost/log/keywords/scan_method.hpp> | |
38 | #include <boost/log/detail/header.hpp> | |
39 | ||
40 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
41 | #pragma once | |
42 | #endif | |
43 | ||
44 | #ifndef BOOST_LOG_DOXYGEN_PASS | |
45 | #ifndef BOOST_LOG_NO_THREADS | |
46 | #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink | |
47 | #else | |
48 | #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink | |
49 | #endif | |
50 | #endif // BOOST_LOG_DOXYGEN_PASS | |
51 | ||
52 | namespace boost { | |
53 | ||
54 | BOOST_LOG_OPEN_NAMESPACE | |
55 | ||
56 | namespace aux { | |
57 | ||
58 | //! The function creates a file collector according to the specified arguments | |
59 | template< typename ArgsT > | |
60 | inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const&, mpl::true_ const&) | |
61 | { | |
62 | return shared_ptr< sinks::file::collector >(); | |
63 | } | |
64 | template< typename ArgsT > | |
65 | inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const& args, mpl::false_ const&) | |
66 | { | |
67 | return sinks::file::make_collector(args); | |
68 | } | |
69 | ||
70 | //! The function constructs the sink and adds it to the core | |
71 | template< typename ArgsT > | |
72 | shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT const& args) | |
73 | { | |
74 | typedef sinks::text_file_backend backend_t; | |
75 | shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args); | |
76 | ||
77 | shared_ptr< sinks::file::collector > pCollector = aux::setup_file_collector(args, | |
78 | typename is_void< typename parameter::binding< ArgsT, keywords::tag::target, void >::type >::type()); | |
79 | if (pCollector) | |
80 | { | |
81 | pBackend->set_file_collector(pCollector); | |
82 | pBackend->scan_for_files(args[keywords::scan_method | sinks::file::scan_matching]); | |
83 | } | |
84 | ||
85 | shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > > pSink = | |
86 | boost::make_shared< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > >(pBackend); | |
87 | ||
88 | aux::setup_filter(*pSink, args, | |
89 | typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type()); | |
90 | ||
91 | aux::setup_formatter(*pSink, args, | |
92 | typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type()); | |
93 | ||
94 | core::get()->add_sink(pSink); | |
95 | ||
96 | return pSink; | |
97 | } | |
98 | ||
99 | //! The function wraps the argument into a file_name named argument, if needed | |
100 | template< typename T > | |
101 | inline T const& wrap_file_name(T const& arg, mpl::true_) | |
102 | { | |
103 | return arg; | |
104 | } | |
105 | template< typename T > | |
106 | inline typename parameter::aux::tag< keywords::tag::file_name, T const >::type | |
107 | wrap_file_name(T const& arg, mpl::false_) | |
108 | { | |
109 | return keywords::file_name = arg; | |
110 | } | |
111 | ||
112 | } // namespace aux | |
113 | ||
114 | #ifndef BOOST_LOG_DOXYGEN_PASS | |
115 | ||
116 | #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\ | |
117 | template< BOOST_PP_ENUM_PARAMS(n, typename T) >\ | |
118 | 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))\ | |
119 | {\ | |
120 | return aux::add_file_log((\ | |
121 | aux::wrap_file_name(arg0, typename parameter::aux::is_named_argument< T0 >::type())\ | |
122 | BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\ | |
123 | BOOST_PP_ENUM_SHIFTED_PARAMS(n, arg)\ | |
124 | ));\ | |
125 | } | |
126 | ||
127 | BOOST_PP_REPEAT_FROM_TO(1, BOOST_LOG_MAX_PARAMETER_ARGS, BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL, ~) | |
128 | ||
129 | #undef BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL | |
130 | ||
131 | #else // BOOST_LOG_DOXYGEN_PASS | |
132 | ||
133 | /*! | |
134 | * The function initializes the logging library to write logs to a file stream. | |
135 | * | |
136 | * \param args A number of named arguments. The following parameters are supported: | |
137 | * \li \c file_name The file name or its pattern. This parameter is mandatory. | |
138 | * \li \c open_mode The mask that describes the open mode for the file. See <tt>std::ios_base::openmode</tt>. | |
139 | * \li \c rotation_size The size of the file at which rotation should occur. See <tt>basic_text_file_backend</tt>. | |
140 | * \li \c time_based_rotation The predicate for time-based file rotations. See <tt>basic_text_file_backend</tt>. | |
141 | * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the file | |
142 | * after each written record. | |
143 | * \li \c target The target directory to store rotated files in. See <tt>sinks::file::make_collector</tt>. | |
144 | * \li \c max_size The maximum total size of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>. | |
145 | * \li \c min_free_space Minimum free space in the target directory. See <tt>sinks::file::make_collector</tt>. | |
146 | * \li \c max_files The maximum total number of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>. | |
147 | * \li \c scan_method The method of scanning the target directory for log files. See <tt>sinks::file::scan_method</tt>. | |
148 | * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter, | |
149 | * or a filter lambda expression. | |
150 | * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter, | |
151 | * or a formatter lambda expression (either streaming or Boost.Format-like notation). | |
152 | * \return Pointer to the constructed sink. | |
153 | */ | |
154 | template< typename... ArgsT > | |
155 | shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args); | |
156 | ||
157 | #endif // BOOST_LOG_DOXYGEN_PASS | |
158 | ||
159 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
160 | ||
161 | } // namespace boost | |
162 | ||
163 | #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL | |
164 | ||
165 | #include <boost/log/detail/footer.hpp> | |
166 | ||
167 | #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_ |