BDE 4.14.0 Production release
Loading...
Searching...
No Matches
ball::FileObserver Class Reference

#include <ball_fileobserver.h>

Inheritance diagram for ball::FileObserver:
ball::Observer

Public Types

typedef FileObserver2::OnFileRotationCallback OnFileRotationCallback
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (FileObserver, bslma::UsesBslmaAllocator)
 
 FileObserver ()
 
 FileObserver (bslma::Allocator *basicAllocator)
 
 FileObserver (Severity::Level stdoutThreshold, bslma::Allocator *basicAllocator=0)
 
 FileObserver (Severity::Level stdoutThreshold, bool publishInLocalTime, bslma::Allocator *basicAllocator=0)
 
 ~FileObserver () BSLS_KEYWORD_OVERRIDE
 
void disableFileLogging ()
 
void disableLifetimeRotation ()
 
void disableTimeIntervalRotation ()
 
void disableSizeRotation ()
 
void disableStdoutLoggingPrefix ()
 
void disableUserFieldsLogging ()
 
void disablePublishInLocalTime ()
 
int enableFileLogging (const char *logFilenamePattern)
 
int enableFileLogging (const char *logFilenamePattern, bool appendTimestampFlag)
 
void enableStdoutLoggingPrefix ()
 
void enableUserFieldsLogging ()
 
void enablePublishInLocalTime ()
 
void publish (const Record &record, const Context &context) BSLS_KEYWORD_OVERRIDE
 
void publish (const bsl::shared_ptr< const Record > &record, const Context &context) BSLS_KEYWORD_OVERRIDE
 
void releaseRecords () BSLS_KEYWORD_OVERRIDE
 
void forceRotation ()
 
void rotateOnLifetime (const bdlt::DatetimeInterval &timeInterval)
 
void rotateOnSize (int size)
 
void rotateOnTimeInterval (const bdlt::DatetimeInterval &interval)
 
void rotateOnTimeInterval (const bdlt::DatetimeInterval &interval, const bdlt::Datetime &startTime)
 
void setOnFileRotationCallback (const OnFileRotationCallback &onRotationCallback)
 
void setStdoutThreshold (Severity::Level stdoutThreshold)
 
void setLogFormat (const char *logFileFormat, const char *stdoutFormat)
 
void suppressUniqueFileNameOnRotation (bool suppress)
 
bslma::Allocatorallocator () const
 Return the memory allocator used by this object.
 
void getLogFormat (const char **logFileFormat, const char **stdoutFormat) const
 
bool isFileLoggingEnabled () const
 
bool isFileLoggingEnabled (bsl::string *result) const
 
bool isFileLoggingEnabled (std::string *result) const
 
bool isStdoutLoggingPrefixEnabled () const
 
bool isUserFieldsLoggingEnabled () const
 
bool isPublishInLocalTimeEnabled () const
 
bool isSuppressUniqueFileNameOnRotation () const
 
bdlt::DatetimeInterval localTimeOffset () const
 
bdlt::DatetimeInterval rotationLifetime () const
 
int rotationSize () const
 
Severity::Level stdoutThreshold () const
 
- Public Member Functions inherited from ball::Observer
virtual ~Observer ()
 Destroy this observer.
 

Detailed Description

This class implements the Observer protocol. The publish method of this class outputs the log records that it receives to stdout and optionally to a user-specified file. This class is thread-safe; different threads can operate on an object concurrently. This class is exception-neutral with no guarantee of rollback. In no event is memory leaked.

See ball_fileobserver

Member Typedef Documentation

◆ OnFileRotationCallback

OnFileRotationCallback is an alias for a user-supplied callback function that is invoked after the file observer attempts to rotate its log file. The callback takes two arguments: (1) an integer status value where 0 indicates a new log file was successfully created and a non-zero value indicates an error occurred during rotation, and (2) a string that provides the name of the rotated log file if the rotation was successful. E.g.:

void onLogFileRotation(int rotationStatus,
const bsl::string& rotatedLogFileName);
Definition bslstl_string.h:1281

Constructor & Destructor Documentation

◆ FileObserver() [1/4]

ball::FileObserver::FileObserver ( )

◆ FileObserver() [2/4]

ball::FileObserver::FileObserver ( bslma::Allocator basicAllocator)
explicit

◆ FileObserver() [3/4]

ball::FileObserver::FileObserver ( Severity::Level  stdoutThreshold,
bslma::Allocator basicAllocator = 0 
)
explicit

Create a file observer that publishes log records to stdout if their severity is at least as severe as the optionally specified stdoutThreshold level, and has file logging initially disabled. If stdoutThreshold is not specified, log records are published to stdout if their severity is at least as severe as Severity::e_WARN. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. Note that isPublishInLocalTimeEnabled returns false following construction indicating that the timestamp attribute of published records will be written in UTC time (see enablePublishInLocalTime). Also note that independent default record formats are in effect for stdout and file logging (see setLogFormat).

◆ FileObserver() [4/4]

ball::FileObserver::FileObserver ( Severity::Level  stdoutThreshold,
bool  publishInLocalTime,
bslma::Allocator basicAllocator = 0 
)

Create a file observer that publishes log records to stdout if their severity is at least as severe as the specified stdoutThreshold level, and has file logging initially disabled. The timestamp attribute of published records is written in local time if the specified publishInLocalTime flag is true, and in UTC time otherwise. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used. Note that following construction independent default record formats are in effect for stdout and file logging (see setLogFormat).

◆ ~FileObserver()

ball::FileObserver::~FileObserver ( )

Close the log file of this file observer if file logging is enabled, and destroy this file observer.

Member Function Documentation

◆ allocator()

bslma::Allocator * ball::FileObserver::allocator ( ) const

◆ BSLMF_NESTED_TRAIT_DECLARATION()

ball::FileObserver::BSLMF_NESTED_TRAIT_DECLARATION ( FileObserver  ,
bslma::UsesBslmaAllocator   
)

◆ disableFileLogging()

void ball::FileObserver::disableFileLogging ( )
inline

Disable file logging for this file observer. This method has no effect if file logging is not enabled. Note that records subsequently received through the publish method of this file observer may still be logged to stdout after calling this method.

◆ disableLifetimeRotation()

void ball::FileObserver::disableLifetimeRotation ( )
inline

Disable log file rotation based on a periodic time interval for this file observer. This method has no effect if rotation-on-time-interval is not enabled.

Deprecated:
Use disableTimeIntervalRotation instead.

◆ disablePublishInLocalTime()

void ball::FileObserver::disablePublishInLocalTime ( )

Disable publishing of the timestamp attribute of records in local time by this file observer; henceforth, timestamps will be in UTC time. This method has no effect if publishing in local time is not enabled. Note that this method also affects log filenames (see {Log Filename Patterns}).

◆ disableSizeRotation()

void ball::FileObserver::disableSizeRotation ( )
inline

Disable log file rotation based on log file size for this file observer. This method has no effect if rotation-on-size is not enabled.

◆ disableStdoutLoggingPrefix()

void ball::FileObserver::disableStdoutLoggingPrefix ( )

Disable this file observer from using the long output format when logging to stdout. Henceforth, this file observer will use the default short output format ("\n%s %f:%l %c %m %u\n") when logging to stdout. This method has no effect if the long output format for stdout logging is not enabled. Note that this method omits the "%d %p:%t " prefix from the default long output format.

◆ disableTimeIntervalRotation()

void ball::FileObserver::disableTimeIntervalRotation ( )
inline

Disable log file rotation based on a periodic time interval for this file observer. This method has no effect if rotation-on-time-interval is not enabled.

◆ disableUserFieldsLogging()

void ball::FileObserver::disableUserFieldsLogging ( )

Disable the logging of user-defined fields by this file observer. This method has no effect if logging of user-defined fields is not enabled, or if a format string other than the default one is in effect.

Deprecated:
Use setLogFormat instead.

◆ enableFileLogging() [1/2]

int ball::FileObserver::enableFileLogging ( const char *  logFilenamePattern)
inline

Enable logging of all records published to this file observer to a file whose name is derived from the specified logFilenamePattern. Return 0 on success, a positive value if file logging is already enabled (with no effect), and a negative value otherwise. The basename of logFilenamePattern may contain %-escape sequences that are interpreted as follows:

%Y - current year (4 digits with leading zeros)
%M - current month (2 digits with leading zeros)
%D - current day (2 digits with leading zeros)
%h - current hour (2 digits with leading zeros)
%m - current minute (2 digits with leading zeros)
%s - current second (2 digits with leading zeros)
%T - current datetime, equivalent to "%Y%M%D_%h%m%s"
%p - process ID

Each time a log file is opened by this file observer (upon a successful call to this method and following each log file rotation), the name of the new log file is derived from logFilenamePattern by interpreting the above recognized %-escape sequences. If isPublishInLocalTimeEnabled returns true, the %-escape sequences related to time will be substituted with local time values, and UTC time values otherwise. See {Log Filename Patterns}.

◆ enableFileLogging() [2/2]

int ball::FileObserver::enableFileLogging ( const char *  logFilenamePattern,
bool  appendTimestampFlag 
)
inline

Enable logging of all records published to this file observer to a file whose name is derived from the specified logFilenamePattern and append a timestamp to the log filename if the specified appendTimestampFlag is true. Return 0 on success, a positive value if file logging is already enabled (with no effect), and a negative value otherwise. If the appendTimestampFlag is true and logFilenamePattern does not contain any %-escape sequences, this method behaves as if ".%T" is appended to logFilenamePattern.

Deprecated:
Use enableFileLogging(logFilenamePattern) instead (use the ".%T" pattern to replicate true == appendTimestampFlag behavior).

◆ enablePublishInLocalTime()

void ball::FileObserver::enablePublishInLocalTime ( )

Enable publishing of the timestamp attribute of records in local time by this file observer. This method has no effect if publishing in local time is already enabled. Note that this method also affects log filenames (see {Log Filename Patterns}).

◆ enableStdoutLoggingPrefix()

void ball::FileObserver::enableStdoutLoggingPrefix ( )

Enable this file observer to use the long output format when logging to stdout. Henceforth, this file observer will use the output format for stdout logging that was set by the most recent call to setLogFormat, or the default long output format ("\n%d %p:%t %s %f:%l %c %m %u\n") if setLogFormat has not yet been called. This method has no effect if the long output format for stdout logging is already enabled.

◆ enableUserFieldsLogging()

void ball::FileObserver::enableUserFieldsLogging ( )

Enable the logging of user-defined fields by this file observer. This method has no effect if logging of user-defined fields is already enabled, or if a format string other than the default one is in effect.

Deprecated:
Use setLogFormat instead.

◆ forceRotation()

void ball::FileObserver::forceRotation ( )
inline

Forcefully perform a log file rotation by this file observer. Close the current log file, rename the log file if necessary, and open a new log file. This method has no effect if file logging is not enabled. See {Rotated File Naming} for details on filenames of rotated log files.

◆ getLogFormat()

void ball::FileObserver::getLogFormat ( const char **  logFileFormat,
const char **  stdoutFormat 
) const

Load the format specification for log records written by this file observer to the log file into the specified *logFileFormat address and the format specification for log records written to stdout into the specified *stdoutFormat address. See {Log Record Formatting} for details on the syntax of format specifications.

◆ isFileLoggingEnabled() [1/3]

bool ball::FileObserver::isFileLoggingEnabled ( ) const
inline

Return true if file logging is enabled for this file observer, and false otherwise. Load the optionally specified result with the name of the current log file if file logging is enabled, and leave result unmodified otherwise. Note that records received through the publish method of this file observer may still be logged to stdout when this method returns false.

◆ isFileLoggingEnabled() [2/3]

bool ball::FileObserver::isFileLoggingEnabled ( bsl::string result) const
inline

◆ isFileLoggingEnabled() [3/3]

bool ball::FileObserver::isFileLoggingEnabled ( std::string *  result) const
inline

◆ isPublishInLocalTimeEnabled()

bool ball::FileObserver::isPublishInLocalTimeEnabled ( ) const

Return true if this file observer writes the timestamp attribute of records that it publishes in local time, and false otherwise (in which case timestamps are written in UTC time). Note that the value returned by this method also affects log filenames (see {Log Filename Patterns}).

◆ isStdoutLoggingPrefixEnabled()

bool ball::FileObserver::isStdoutLoggingPrefixEnabled ( ) const

Return true if this file observer uses the long output format when writing to stdout, and false otherwise (in which case the default short output format is used). See enableStdoutLoggingPrefix and disableStdoutLoggingPrefix.

◆ isSuppressUniqueFileNameOnRotation()

bool ball::FileObserver::isSuppressUniqueFileNameOnRotation ( ) const
inline

Return true if the log filename uniqueness check on rotation is suppressed, and false otherwise.

◆ isUserFieldsLoggingEnabled()

bool ball::FileObserver::isUserFieldsLoggingEnabled ( ) const

Return true if the logging of user-defined fields is enabled for this file observer, and false otherwise.

Deprecated:
Do not use.

◆ localTimeOffset()

bdlt::DatetimeInterval ball::FileObserver::localTimeOffset ( ) const
inline

Return the difference between the local time and UTC time in effect when this file observer was constructed. Note that this value remains unchanged during the lifetime of this object and therefore may become incorrect when the difference between the local time and UTC time changes (e.g., when transitioning into or out of daylight savings time).

Deprecated:
Use bdlt::LocalTimeOffset instead.

◆ publish() [1/2]

void ball::FileObserver::publish ( const bsl::shared_ptr< const Record > &  record,
const Context context 
)
inlinevirtual

Process the record referenced by the specified 'record' shared pointer having the specified publishing 'context' by writing the record and 'context' to the current log file if file logging is enabled for this file observer, and to 'stdout' if the severity of 'record' is at least as severe as the value returned by 'stdoutThreshold'.

Reimplemented from ball::Observer.

◆ publish() [2/2]

void ball::FileObserver::publish ( const Record record,
const Context context 
)
virtual

Process the specified log record having the specified publishing context by writing record and context to the current log file if file logging is enabled for this file observer, and to stdout if the severity of record is at least as severe as the value returned by stdoutThreshold.

Deprecated:
Do not use.

Reimplemented from ball::Observer.

◆ releaseRecords()

void ball::FileObserver::releaseRecords ( )
inlinevirtual

Discard any shared references to Record objects that were supplied to the publish method, and are held by this observer. Note that this operation should be called if resources underlying the previously provided shared pointers must be released.

Reimplemented from ball::Observer.

◆ rotateOnLifetime()

void ball::FileObserver::rotateOnLifetime ( const bdlt::DatetimeInterval timeInterval)
inline

Set this file observer to perform a periodic log file rotation at multiples of the specified timeInterval. The behavior is undefined unless 0 < timeInterval.totalMilliseconds(). This rule replaces any rotation-on-time-interval rule currently in effect.

Deprecated:
Use rotateOnTimeInterval instead.

◆ rotateOnSize()

void ball::FileObserver::rotateOnSize ( int  size)
inline

Set this file observer to perform log file rotation when the size of the file exceeds the specified size (in kilobytes). This rule replaces any rotation-on-size rule currently in effect. The behavior is undefined unless size > 0.

◆ rotateOnTimeInterval() [1/2]

void ball::FileObserver::rotateOnTimeInterval ( const bdlt::DatetimeInterval interval)
inline

◆ rotateOnTimeInterval() [2/2]

void ball::FileObserver::rotateOnTimeInterval ( const bdlt::DatetimeInterval interval,
const bdlt::Datetime startTime 
)
inline

Set this file observer to perform a periodic log file rotation at multiples of the specified interval. Optionally specify a startTime indicating the datetime to use as the starting point for computing the periodic rotation schedule. If isPublishInLocalTimeEnabled is true, the startTime is interpreted as local time, and as a UTC time otherwise. If startTime is not specified, the current time is used. This rule replaces any rotation-on-time-interval rule currently in effect. The behavior is undefined unless 0 < interval.totalMilliseconds(). Note that startTime may be a fixed time in the past; e.g., a reference time of bdlt::Datetime(1, 1, 1) and an interval of 24 hours would configure a periodic rotation at midnight each day.

◆ rotationLifetime()

bdlt::DatetimeInterval ball::FileObserver::rotationLifetime ( ) const
inline

Return the lifetime of the log file that will trigger a file rotation by this file observer if rotation-on-lifetime is in effect, and a 0 time interval otherwise.

◆ rotationSize()

int ball::FileObserver::rotationSize ( ) const
inline

Return the size (in kilobytes) of the log file that will trigger a file rotation by this file observer if rotation-on-size is in effect, and 0 otherwise.

◆ setLogFormat()

void ball::FileObserver::setLogFormat ( const char *  logFileFormat,
const char *  stdoutFormat 
)

Set the format specifications for log records written to the log file and to stdout to the specified logFileFormat and stdoutFormat, respectively. If the default short output format is currently in effect for logging to stdout, this method has the effect of calling enableStdoutLoggingPrefix (see that method and disableStdoutLoggingPrefix). See {Log Record Formatting} for details on the syntax of format specifications. Note that default formats are in effect following construction until this method is called ("\n%d %p:%t %s %f:%l %c %m %u\n" for both file and stdout logging). Also note that the observer emits newline characters at the beginning and at the end of a log record by default, so the user needs to add them explicitly to the format string to preserve this behavior.

◆ setOnFileRotationCallback()

void ball::FileObserver::setOnFileRotationCallback ( const OnFileRotationCallback onRotationCallback)
inline

Set the specified onRotationCallback to be invoked after each time this file observer attempts to perform a log file rotation. The behavior is undefined if the supplied function calls either setOnFileRotationCallback, forceRotation, or publish on this file observer (i.e., the supplied callback should not attempt to write to the ball log).

◆ setStdoutThreshold()

void ball::FileObserver::setStdoutThreshold ( Severity::Level  stdoutThreshold)

Set the minimum severity of records logged to stdout by this file observer to the specified stdoutThreshold level. Note that if the value of stdoutThreshold is Severity::e_OFF, logging to stdout is disabled.

◆ stdoutThreshold()

Severity::Level ball::FileObserver::stdoutThreshold ( ) const

Return the minimum severity of records that will be logged to stdout by this file observer. Note that records with a threshold less severe than stdoutThreshold() may still be output to the log file if file logging is enabled.

◆ suppressUniqueFileNameOnRotation()

void ball::FileObserver::suppressUniqueFileNameOnRotation ( bool  suppress)
inline

Suppress generating a unique log file name upon rotation if the specified suppress is true, and generate a unique filename otherwise. See {Rotated File Naming} for details.


The documentation for this class was generated from the following file: