BDE 4.39.x 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.
197///
198/// See @ref ball_logfilecleanerutil
200 private:
201 // PRIVATE CLASS METHODS
202 // ** default callback function **
203
204 /// Immediately call `balb::FileCleanerUtil::removeFiles` with the
205 /// specified `config`.
206 static
207 void logFileCleanupOnRotationDefault(
208 int,
209 const bsl::string&,
210 const balb::FileCleanerConfiguration& config);
211
212 public:
213 // CLASS METHODS
214
215 /// Immediately call `balb::FileCleanerUtil::removeFiles` with the
216 /// specified `config` and then install an
217 /// `t_OBSERVER::OnFileRotationCallback` function into the specified
218 /// `observer` that invokes `removeFiles` synchronously on every log
219 /// file rotation. The (template parameter) `t_OBSERVER` type must
220 /// provide concrete implementation of the `ball::Observer` protocol,
221 /// have a `setOnFileRotationCallback` method (see @ref ball_fileobserver ,
222 /// @ref ball_fileobserver2 , and @ref ball_asyncfileobserver ), and define an
223 /// `OnFileRotationCallback` type alias. This method overrides the file
224 /// rotation callback currently installed in the observer (if any).
225 template <class t_OBSERVER>
226 static
228 t_OBSERVER *observer,
229 const balb::FileCleanerConfiguration& config);
230
231 /// Substitute all occurrences of valid `%`-escape sequences in the
232 /// specified `logPattern` with `*` and load the specified `filePattern`
233 /// with the resulting string. This utility function converts the log
234 /// patterns used by various file observers and supplied to their
235 /// `enableFileLogging` method (see @ref ball_fileobserver ,
236 /// @ref ball_fileobserver2 , and @ref ball_asyncfileobserver ) to the file
237 /// pattern used by various utility functions to find the related
238 /// file(s) on the filesystem (see @ref balb_filecleanerutil and
239 /// @ref balb_filecleanerconfiguration ).
240 static
242 const bsl::string_view& logPattern);
243};
244
245// ============================================================================
246// INLINE DEFINITIONS
247// ============================================================================
248
249 // -------------------------
250 // struct LogFileCleanerUtil
251 // -------------------------
252
253// CLASS METHOD
254template <class t_OBSERVER>
255inline
257 t_OBSERVER *observer,
258 const balb::FileCleanerConfiguration& config)
259{
261
262 typename t_OBSERVER::OnFileRotationCallback rotationCallback =
263 bdlf::BindUtil::bind(&logFileCleanupOnRotationDefault,
266 config);
267
268 observer->setOnFileRotationCallback(rotationCallback);
269}
270
271} // close package namespace
272
273
274#endif
275
276// ----------------------------------------------------------------------------
277// Copyright 2017 Bloomberg Finance L.P.
278//
279// Licensed under the Apache License, Version 2.0 (the "License");
280// you may not use this file except in compliance with the License.
281// You may obtain a copy of the License at
282//
283// http://www.apache.org/licenses/LICENSE-2.0
284//
285// Unless required by applicable law or agreed to in writing, software
286// distributed under the License is distributed on an "AS IS" BASIS,
287// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
288// See the License for the specific language governing permissions and
289// limitations under the License.
290// ----------------------------- END-OF-FILE ----------------------------------
291
292/** @} */
293/** @} */
294/** @} */
Definition balb_filecleanerconfiguration.h:166
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1835
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition ball_administration.h:214
const PlaceHolder< 1 > _1
const PlaceHolder< 2 > _2
static void removeFiles(const FileCleanerConfiguration &config)
Definition ball_logfilecleanerutil.h:199
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:256