20.3. Usage

After a logger configuration, its usage is quite straightforward - generally just one method is used to process log messages (log message must be created first as an instance of LogMessage class):

LogMessage    message
                (
                STATUS_DEBUG
                  (
                  Status::FACILITY_TEST, Status::PRIORITY_LOWEST
                  ),
                "msg"
                );

logger.log( message );

[Note]Note

For your convenience you can use the more comfortable logging interface provided by the Global class, see Section 16.1.2, “Global Logging Interface” for more information.

Configuration of the logger is more complicated - the following is the general example of how to configure logger directly; for indirect configuration using registry configuration files see Logger configuration using the Registry.

First of all selectors must be created and selector elements created and inserted into appropriate selectors. Following lines create one selector consisting of two selector elements. The first selector element matches log messages of types warning and error and with priority greater or equal to Status::PRIORITY_LOW and from facility FACILITY_TEST. The second one matches messages of any type from any facility having its priority less or equal to Status::PRIORITY_HIGH. The whole selector matches all messages satisfying all of the previous constraints.

Logger::SelectorElement     selem1
    (
    Logger::SelectorElement::TYPE_GEQ | 
                              Logger::SelectorElement::PRIORITY_GEQ,
    Status::FACILITY_TEST,
    Status::TYPE_WARNING,
    Status::PRIORITY_LOW
    );
Logger::SelectorElement     selem2
    (
    Logger::SelectorElement::PRIORITY_LEQ,
    Status::FACILITY_ANY,
    Status::TYPE_ANY,
    Status::PRIORITY_HIGH
    );

Logger::Selector            selector;
selector.insert( selem1 );
selector.insert( selem2 );

Initialization of logger itself follows: insertion of rules first.

Logger & logger = ( Global::logger() );
logger.clear_rules();

Logger::RuleIterator    ri;
ri = logger.insert_rule( selector );
assert( ri != logger.end_rule() );

Destination registration:

if( !logger.register_destination( logdest ) )
    {
    delete logdest;
    throw ExLoggerError( "Cannot register 'logdest'." );
    }

After the rules has been inserted into logger and destinations have been registered, each rule must be associated with its destinations (i.e. destinations for all messages matching each specific rule):

logger.insert_destination( ri, "test" );
    // lets suppose "test" is name of the logdest destination, i.e.
    // logdest's get_name() method returns "test" string.

Logger supposed it will be uninitialized:

logdest->reset();
logger.clear_rules();
logger.unregister_destination( logdest );
logdest = NULL;