BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball_logfilecleanerutil.h
Go to the documentation of this file.
1/// @file ball_logfilecleanerutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// ball_logfilecleanerutil.h -*-C++-*-
8#ifndef INCLUDED_BALL_LOGFILECLEANERUTIL
9#define INCLUDED_BALL_LOGFILECLEANERUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup ball_logfilecleanerutil ball_logfilecleanerutil
15/// @brief Provide a utility class for removing log files.
16/// @addtogroup bal
17/// @{
18/// @addtogroup ball
19/// @{
20/// @addtogroup ball_logfilecleanerutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#ball_logfilecleanerutil-purpose"> Purpose</a>
25/// * <a href="#ball_logfilecleanerutil-classes"> Classes </a>
26/// * <a href="#ball_logfilecleanerutil-description"> Description </a>
27/// * <a href="#ball_logfilecleanerutil-log-filename-pattern"> Log Filename Pattern </a>
28/// * <a href="#ball_logfilecleanerutil-log-filename-pattern-conversion-rules"> Log Filename Pattern Conversion Rules </a>
29/// * <a href="#ball_logfilecleanerutil-usage"> Usage </a>
30/// * <a href="#ball_logfilecleanerutil-example-1-basic-usage"> Example 1: Basic Usage </a>
31/// * <a href="#ball_logfilecleanerutil-example-2-cleaning-log-files-on-file-rotation"> Example 2: Cleaning Log Files On File Rotation </a>
32///
33/// # Purpose {#ball_logfilecleanerutil-purpose}
34/// Provide a utility class for removing log files.
35///
36/// # Classes {#ball_logfilecleanerutil-classes}
37///
38/// - ball::LogFileCleanerUtil: utility class for removing log files
39///
40/// @see ball_fileobserver2, balb_filecleanerconfiguration
41///
42/// # Description {#ball_logfilecleanerutil-description}
43/// This component defines a `struct`, `ball::LogFileCleanerUtil`,
44/// that provides utility functions for converting log file patterns used by
45/// `ball` file observers into filesystem patterns and installing a custom
46/// rotation callback into file observers to perform log file cleanup.
47///
48/// ## Log Filename Pattern {#ball_logfilecleanerutil-log-filename-pattern}
49///
50///
51/// The `ball` file observers allow the use of `%`-escape sequences to specify
52/// log filenames. The recognized sequences are as follows:
53/// @code
54/// %Y - current year (four digits with leading zeros)
55/// %M - current month (two digits with leading zeros)
56/// %D - current day (two digits with leading zeros)
57/// %h - current hour (two digits with leading zeros)
58/// %m - current minute (two digits with leading zeros)
59/// %s - current second (two digits with leading zeros)
60/// %T - current datetime, equivalent to "%Y%M%D_%h%m%s"
61/// %p - process Id
62/// @endcode
63///
64/// ## Log Filename Pattern Conversion Rules {#ball_logfilecleanerutil-log-filename-pattern-conversion-rules}
65///
66///
67/// The `logPatternToFilePattern` conversion function does the following:
68///
69/// * Every recognized `%`-escape sequence is converted to `*`.
70///
71/// * Successive `%`-escape sequences without any separator between them are
72/// collapsed into a single `*`.
73///
74/// * "%%" is converted into a single `%`.
75///
76/// * All unrecognized `%`-escape sequences and characters are passed to
77/// output as-is.
78///
79/// * A single `*` is appended to the converted pattern when it does not
80/// terminate with `*`. (This is necessary for capturing rotated log files.)
81///
82/// @code
83/// -------------------+---------------
84/// Log File Pattern | File Pattern
85/// -------------------+---------------
86/// "a.log" | "a.log*"
87/// "a.log.%T" | "a.log.*"
88/// "a.log.%Y" | "a.log.*"
89/// "a.log.%Y%M%D" | "a.log.*"
90/// "a.%T.log" | "a.*.log*"
91/// -------------------+---------------
92/// @endcode
93///
94/// ## Usage {#ball_logfilecleanerutil-usage}
95///
96///
97/// This section illustrates intended use of this component.
98///
99/// ### Example 1: Basic Usage {#ball_logfilecleanerutil-example-1-basic-usage}
100///
101///
102/// The following snippets of code illustrate the basic usage of
103/// `ball::LogFileCleanerUtil`.
104///
105/// Suppose that the application was set up to do its logging using one of the
106/// `ball` file observers (see @ref ball_fileobserver2 ) with the following log
107/// pattern:
108/// @code
109/// const char *appLogPattern = "/var/log/myApp/log%T";
110/// @endcode
111/// First, we need to convert the log filename pattern to the pattern that can
112/// be used for filename matching on the filesystem:
113/// @code
114/// bsl::string fileNamePattern;
115/// ball::LogFileCleanerUtil::logPatternToFilePattern(&fileNamePattern,
116/// appLogPattern);
117/// @endcode
118/// Finally, we test the resulting file pattern:
119/// @code
120/// assert("/var/log/myApp/log*" == fileNamePattern);
121/// @endcode
122/// ### Example 2: Cleaning Log Files On File Rotation {#ball_logfilecleanerutil-example-2-cleaning-log-files-on-file-rotation}
123///
124///
125/// The following snippets of code illustrate how the application can implement
126/// automatic log file cleanup from the observer's file rotation callback.
127///
128/// Suppose that the application was set up to do its logging using one of the
129/// file observers (see @ref ball_fileobserver2 ) with the following log pattern:
130/// @code
131/// const char *appLogPattern = "/var/log/myApp/log%T";
132/// @endcode
133/// First, we need to convert the log filename pattern to the pattern that can
134/// be used for filename matching on the filesystem:
135/// @code
136/// bsl::string fileNamePattern;
137/// ball::LogFileCleanerUtil::logPatternToFilePattern(&fileNamePattern,
138/// appLogPattern);
139/// @endcode
140/// Then, we create a configuration for the file cleaner utility. The sample
141/// configuration below instructs the file cleaner to remove all log files that
142/// match the specified file pattern and are older than a week, but to keep at
143/// least 4 most recent log files:
144/// @code
145/// balb::FileCleanerConfiguration config(
146/// fileNamePattern.c_str(),
147/// bsls::TimeInterval(7 * bdlt::TimeUnitRatio::k_SECONDS_PER_DAY),
148/// 4);
149/// @endcode
150/// Next, we create a file observer and enable file logging:
151/// @code
152/// ball::FileObserver2 observer;
153/// observer.enableFileLogging(appLogPattern);
154/// @endcode
155/// Finally, we use the utility function to install the file rotation callback
156/// that will invoke file cleanup with the specified configuration:
157/// @code
158/// ball::LogFileCleanerUtil::enableLogFileCleanup(&observer, config);
159/// @endcode
160/// Note that the file cleanup will be performed immediately and on every log
161/// file rotation performed by the file observer. Also note that this method
162/// overrides the file rotation callback currently installed in the file
163/// observer.
164/// @}
165/** @} */
166/** @} */
167
168/** @addtogroup bal
169 * @{
170 */
171/** @addtogroup ball
172 * @{
173 */
174/** @addtogroup ball_logfilecleanerutil
175 * @{
176 */
177
178#include <balscm_version.h>
179
181#include <balb_filecleanerutil.h>
182
183#include <bdlf_bind.h>
184#include <bdlf_placeholder.h>
185
186#include <bsl_string.h>
187
188
189namespace ball {
190
191 // =========================
192 // struct LogFileCleanerUtil
193 // =========================
194
195/// This utility class provides functions for converting log file patterns
196/// and for cleaning up log files based on a configuration.
198 private:
199 // PRIVATE CLASS METHODS
200 // ** default callback function **
201
202 /// Immediately call `balb::FileCleanerUtil::removeFiles` with the
203 /// specified `config`.
204 static
205 void logFileCleanupOnRotationDefault(
206 int,
207 const bsl::string&,
208 const balb::FileCleanerConfiguration& config);
209
210 public:
211 // CLASS METHODS
212
213 /// Immediately call `balb::FileCleanerUtil::removeFiles` with the
214 /// specified `config` and then install an
215 /// `t_OBSERVER::OnFileRotationCallback` function into the specified
216 /// `observer` that invokes `removeFiles` synchronously on every log
217 /// file rotation. The (template parameter) `t_OBSERVER` type must
218 /// provide concrete implementation of the `ball::Observer` protocol,
219 /// have a `setOnFileRotationCallback` method (see @ref ball_fileobserver ,
220 /// @ref ball_fileobserver2 , and @ref ball_asyncfileobserver ), and define an
221 /// `OnFileRotationCallback` type alias. This method overrides the file
222 /// rotation callback currently installed in the observer (if any).
223 template <class t_OBSERVER>
224 static
226 t_OBSERVER *observer,
227 const balb::FileCleanerConfiguration& config);
228
229 /// Substitute all occurrences of valid `%`-escape sequences in the
230 /// specified `logPattern` with `*` and load the specified `filePattern`
231 /// with the resulting string. This utility function converts the log
232 /// patterns used by various file observers and supplied to their
233 /// `enableFileLogging` method (see @ref ball_fileobserver ,
234 /// @ref ball_fileobserver2 , and @ref ball_asyncfileobserver ) to the file
235 /// pattern used by various utility functions to find the related
236 /// file(s) on the filesystem (see @ref balb_filecleanerutil and
237 /// @ref balb_filecleanerconfiguration ).
238 static
240 const bsl::string_view& logPattern);
241};
242
243// ============================================================================
244// INLINE DEFINITIONS
245// ============================================================================
246
247 // -------------------------
248 // struct LogFileCleanerUtil
249 // -------------------------
250
251// CLASS METHOD
252template <class t_OBSERVER>
253inline
255 t_OBSERVER *observer,
256 const balb::FileCleanerConfiguration& config)
257{
259
260 typename t_OBSERVER::OnFileRotationCallback rotationCallback =
261 bdlf::BindUtil::bind(&logFileCleanupOnRotationDefault,
264 config);
265
266 observer->setOnFileRotationCallback(rotationCallback);
267}
268
269} // close package namespace
270
271
272#endif
273
274// ----------------------------------------------------------------------------
275// Copyright 2017 Bloomberg Finance L.P.
276//
277// Licensed under the Apache License, Version 2.0 (the "License");
278// you may not use this file except in compliance with the License.
279// You may obtain a copy of the License at
280//
281// http://www.apache.org/licenses/LICENSE-2.0
282//
283// Unless required by applicable law or agreed to in writing, software
284// distributed under the License is distributed on an "AS IS" BASIS,
285// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286// See the License for the specific language governing permissions and
287// limitations under the License.
288// ----------------------------- END-OF-FILE ----------------------------------
289
290/** @} */
291/** @} */
292/** @} */
Definition balb_filecleanerconfiguration.h:161
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1830
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition ball_administration.h:214
const PlaceHolder< 1 > _1
const PlaceHolder< 2 > _2
static void removeFiles(const FileCleanerConfiguration &config)
Definition ball_logfilecleanerutil.h:197
static void logPatternToFilePattern(bsl::string *filePattern, const bsl::string_view &logPattern)
static void enableLogFileCleanup(t_OBSERVER *observer, const balb::FileCleanerConfiguration &config)
Definition ball_logfilecleanerutil.h:254