nidas  v1.2-1520
Public Member Functions | Private Attributes | List of all members
nidas::util::LogMessage Class Reference

A class for formatting and streaming a log message. More...

#include <Logger.h>

Public Member Functions

 LogMessage (const std::string &s="")
 Create a LogMessage, optionally set to an initial string. More...
 
 LogMessage (const LogContext *lp, const std::string &s="")
 Associate this LogMessage with a LogContext. More...
 
 LogMessage (const LogMessage &right)
 
LogMessageoperator= (const LogMessage &right)
 
LogMessageformat (const char *fmt,...)
 
std::string getMessage () const
 
 operator std::string () const
 
template<typename T >
LogMessageoperator<< (const T &t)
 Everything streamed to a LogMessage is passed on to the underlying ostringstream, including ostream manipulators. More...
 
 ~LogMessage ()
 If this message was associated with a LogContext, then send the completed message to it when this instance is destroyed, ie, when it goes out of scope. More...
 
std::streampos length ()
 Return the length of the current message buffer. More...
 
void log ()
 If this LogMessage is associated with a LogContext and if the current message is not empty, then log the current message with the LogContext. More...
 

Private Attributes

std::ostringstream msg
 
const LogContext_log_context
 

Detailed Description

A class for formatting and streaming a log message.

Text can be appended to the message with a printf() format or streamed with the stream output operator<<. The implementation essentially wraps an ostringstream.

Constructor & Destructor Documentation

nidas::util::LogMessage::LogMessage ( const std::string &  s = "")
inline

Create a LogMessage, optionally set to an initial string.

References msg.

nidas::util::LogMessage::LogMessage ( const LogContext lp,
const std::string &  s = "" 
)
inline

Associate this LogMessage with a LogContext.

When the LogMessage is destroyed (eg, goes out of scope) or when the log() method is called, the current message (if any) is logged through the LogContext::log() method. The message optionally can be given an initial value.

This can be used to build up complicated logging messages which must be streamed incrementally, possibly dispersed throughout the code. Instantiate a LogContext and associate a LogMessage with it, then stream to the LogMessage if the context is active. The LogMessage can be reused to send multiple log messages, or to stream data and push the log message when it gets too long.

static n_u::LogContext sdlog(LOG_VERBOSE, "slice_debug");
static n_u::LogMessage sdmsg(&sdlog);
if (sdlog.active())
{
sdmsg << "initial data: " << value;
}
...
if (sdlog.active())
{
sdmsg << "more data: " << value2;
if (sdmsg.length() > 80)
{
sdmsg << endlog;
}
}

References msg.

nidas::util::LogMessage::LogMessage ( const LogMessage right)
inline

References msg.

nidas::util::LogMessage::~LogMessage ( )
inline

If this message was associated with a LogContext, then send the completed message to it when this instance is destroyed, ie, when it goes out of scope.

References log().

Member Function Documentation

LogMessage & LogMessage::format ( const char *  fmt,
  ... 
)
std::string nidas::util::LogMessage::getMessage ( ) const
inline

References msg.

Referenced by nidas::util::Logger::msg().

std::streampos nidas::util::LogMessage::length ( )
inline

Return the length of the current message buffer.

This can be used to test whether there is anything to log yet, or to cut off a stream of log info to limit the line length.

if (logmsg.length() > 80)
{
logmsg << endlog;
}
logmsg << data << ",";

References msg.

Referenced by log(), and nidas::dynld::raf::TwoD64_USB::processImageRecord().

void nidas::util::LogMessage::log ( )
inline

If this LogMessage is associated with a LogContext and if the current message is not empty, then log the current message with the LogContext.

Clear the buffer and start a new message.

References _log_context, length(), nidas::util::LogContext::log(), and msg.

Referenced by nidas::util::endlog(), nidas::dynld::raf::TwoD64_USB::processImageRecord(), and ~LogMessage().

nidas::util::LogMessage::operator std::string ( ) const
inline

References msg.

LogMessage& nidas::util::LogMessage::operator= ( const LogMessage right)
inline

References _log_context, and msg.

Member Data Documentation

const LogContext* nidas::util::LogMessage::_log_context
private

Referenced by log(), and operator=().

std::ostringstream nidas::util::LogMessage::msg
private

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