LogMessage Class Reference

#include <Pt/System/Logger.h>

Logs records with a logger. More...

Inherits NonCopyable.

Public Member Functions

 LogMessage (Logger &logger, const LogLevel &level, bool always=false)
 Constructs a log message for a logger. More...
 
 ~LogMessage ()
 Destructor.
 
bool enabled () const
 Returns true if the record's log level is enabled for the target.
 
void log ()
 Sends the message's log record to the logger.
 
 operator bool () const
 Returns true if the record's log level is enabled for the target.
 
bool operator! () const
 Returns true if the record's log level is disabled for the target.
 
template<typename T >
LogMessageoperator<< (const T &value)
 Appends value to the log record.
 
LogMessageoperator<< (std::ostream &(*op)(std::ostream &))
 Applies op to the log record.
 
LogMessageoperator<< (std::ios &(*op)(std::ios &))
 Applies op to the log record.
 
LogMessageoperator<< (std::ios_base &(*op)(std::ios_base &))
 Applies op to the log record.
 
LogMessageoperator<< (LogMessage &(*pf)(LogMessage &))
 Applies a manipulator to the log message.
 
const LogRecordrecord () const
 Returns the underlying log record.
 

Related Functions

LogMessageendlog (LogMessage &msg)
 Manipulator to end and send a log-message.
 

Detailed Description

Log messages can be used to log records with a specific logger. They maintain a log record and a reference to a logger. The log record text can be formatted with the stream output operator, just like for the log records itself:

Pt::System::Logger logger("app.module");
if(msg)
{
msg << "Pi is exactly " << 3
<< " No, I was kidding, its really " << 3.1415
<< Pt::System::endlog;
}

To avoid the costs for formatting, it can be checked if the log level is enabled for the target. A log message can be send mutliple times, so formatting has to be done only once and the logging performance can be increased.

Pt::System::Logger logger("app.module");
msg << "pi is: " << 3.1415;
// log for the first time...
msg.log();
// ... and later the second time
msg.log();

Constructor & Destructor Documentation

LogMessage ( Logger logger,
const LogLevel level,
bool  always = false 
)

Contructs a log message, which uses logger to log records of the severity level specified by level. If always is true, the message is send to the target without checking the log level. It is assumed that the caller has already checked the log level of the underlying logger and set this flag accordingly. This flag only persists until the message has been sent.