KFoundation  v2.1
A Better Foundation Library for C++
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
Public Types | Public Member Functions | List of all members
kfoundation::Logger Class Reference

Multi-channel, muti-level logger utility. More...

#include <kfoundation/Logger.h>

Inheritance diagram for kfoundation::Logger:
Inheritance graph

Public Types

enum  level_t { ,
  ERR = 1, WRN = 2, L1 = 3, L2 = 4,
  L3 = 5
 Log level. More...

Public Member Functions

 Logger ()
 Default constructor.
void addChannel (RefConst< UString > name, Ref< OutputStream > stream)
 Adds a channel that outputs to the given ostream object. More...
Ref< OutputStreamgetChannelByName (RefConst< UString > name) const
 Returns refernce to the channel with the given name. More...
void removeAllChannels ()
 Removes all channels.
Ref< PrintWriterlog (level_t level, const char *fileName, kf_int32_t lineNumebr, const char *functionName)
 Not for direct use. More...
Ref< PrintWriterlog (level_t level)
 Creates and initiates a new Logger:Stream with the given level. More...
void setLevel (level_t level)
 Set the filtering level of all channels to the given value. More...
- Public Member Functions inherited from kfoundation::KFObject
bool equals (RefConst< KFObject > other) const
 Checks if this object is the same as the one reffered by the given refrence.

Detailed Description

Multi-channel, muti-level logger utility.

Normally, you want to use the default logger already provided via System::getLogger(). LOG and LOG_XXX macros expand to the default logger. So,

LOG << "Hello" << ENDS;

is equivalant to

System::getLogger().log(Logger::L3) << "Hello" << ENDS;

Calling log() method starts a log stream. The stream should end with ENDS in order to be flushed into the designated channels. Inbetween, any of the following types can be used:

The default logger has one channel that outputs to standard error console. However it is possible add and remove channels manually via removeAllChannels() and addChannel() methods.

Log level determines the importance of the message being logged. Ordered by importance from lowest to highest, these levels are

A channel will filter any message with a level lower than its designated level. For example a channel designated with WRN level, will pass WRN and ERR but filters L3, L2, and L1.

It is possible to reduce the amount of output produced by logger by setting its level or using mute() method or Logger::Channel::setSilent().

Another way that helps the performance of your released program is to use DLOG_XXX macros. Usage:

DLOG_L3("myCounter: " << myCounter);

And to disable logging define DLOG_L3 as nothing in the begining of your file. For example:

#ifndef DEBUG
#undef DLOG_L3
#define DLOG_L3

This will remove the log part all together from your compiled code.

To customize the header preceeding each log, use Channel::setFormat() method.

Member Enumeration Documentation

Log level.


Error severity level.


Warning severity level.


Severity lower than WRN but higher than L2.


Severity lower than L1 but higher than L3.


The lowest severity level.

Member Function Documentation

void kfoundation::Logger::addChannel ( RefConst< UString >  name,
Ref< OutputStream stream 

Adds a channel that outputs to the given ostream object.

Since ostream is not a ManagedObejct it needs to be deleted by user if necessary.

nameA name for the new channel.
osThe output stream to print to.
Ref< OutputStream > kfoundation::Logger::getChannelByName ( RefConst< UString >  name) const

Returns refernce to the channel with the given name.

Anexception if there is no channel with the given name.
Ref< PrintWriter > kfoundation::Logger::log ( level_t  level,
const char *  fileName,
kf_int32_t  lineNumber,
const char *  functionName 

Not for direct use.

Used by LOG, LOG_XXX and DLOG_XXX macros to create a new Logger::Stream.

Ref< PrintWriter > kfoundation::Logger::log ( level_t  level)

Creates and initiates a new Logger:Stream with the given level.

levelLevel of the log item.
Reference to a new Logger::Stream.
void kfoundation::Logger::setLevel ( level_t  level)

Set the filtering level of all channels to the given value.

Thelevel to set.

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