BDE 4.14.0 Production release
|
Provide a utility class for removing log files.
This component defines a struct
, ball::LogFileCleanerUtil
, that provides utility functions for converting log file patterns used by ball
file observers into filesystem patterns and installing a custom rotation callback into file observers to perform log file cleanup.
The ball
file observers allow the use of %
-escape sequences to specify log filenames. The recognized sequences are as follows:
The logPatternToFilePattern
conversion function does the following:
%
-escape sequence is converted to *
.%
-escape sequences without any separator between them are collapsed into a single *
.%
.%
-escape sequences and characters are passed to output as-is.*
is appended to the converted pattern when it does not terminate with *
. (This is necessary for capturing rotated log files.)This section illustrates intended use of this component.
The following snippets of code illustrate the basic usage of ball::LogFileCleanerUtil
.
Suppose that the application was set up to do its logging using one of the ball
file observers (see ball_fileobserver2 ) with the following log pattern:
First, we need to convert the log filename pattern to the pattern that can be used for filename matching on the filesystem:
Finally, we test the resulting file pattern:
The following snippets of code illustrate how the application can implement automatic log file cleanup from the observer's file rotation callback.
Suppose that the application was set up to do its logging using one of the file observers (see ball_fileobserver2 ) with the following log pattern:
First, we need to convert the log filename pattern to the pattern that can be used for filename matching on the filesystem:
Then, we create a configuration for the file cleaner utility. The sample configuration below instructs the file cleaner to remove all log files that match the specified file pattern and are older than a week, but to keep at least 4 most recent log files:
Next, we create a file observer and enable file logging:
Finally, we use the utility function to install the file rotation callback that will invoke file cleanup with the specified configuration:
Note that the file cleanup will be performed immediately and on every log file rotation performed by the file observer. Also note that this method overrides the file rotation callback currently installed in the file observer.