Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions

ball::FileObserver Class Reference

#include <ball_fileobserver.h>

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

List of all members.

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 ()
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)
void publish (const bsl::shared_ptr< const Record > &record, const Context &context)
void releaseRecords ()
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
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

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 Component ball_fileobserver


Member Typedef Documentation


Constructor & Destructor Documentation

ball::FileObserver::FileObserver (  ) 
ball::FileObserver::FileObserver ( bslma::Allocator basicAllocator  )  [explicit]
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).

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).

ball::FileObserver::~FileObserver (  ) 

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


Member Function Documentation

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

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.

void ball::FileObserver::disableLifetimeRotation (  ) 

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.

void ball::FileObserver::disableTimeIntervalRotation (  ) 

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.

void ball::FileObserver::disableSizeRotation (  ) 

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.

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.

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.

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).

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

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.

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

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).

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.

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.

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).

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.

Reimplemented from ball::Observer.

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

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.

void ball::FileObserver::releaseRecords (  )  [virtual]

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.

void ball::FileObserver::forceRotation (  ) 

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.

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

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.

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

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.

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

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.

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

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).

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.

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.

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

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.

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

Return the memory allocator used by this object.

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.

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

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.

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.

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.

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).

bool ball::FileObserver::isSuppressUniqueFileNameOnRotation (  )  const

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

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

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.

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

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.

int ball::FileObserver::rotationSize (  )  const

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.

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.


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