nidas v1.2.3
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.
 
 LogMessage (const LogContextState *lp, const std::string &s="")
 Associate this LogMessage with a LogContext.
 
 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.
 
 ~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.
 
std::streampos length ()
 Return the length of the current message buffer.
 
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.
 

Private Attributes

std::ostringstream msg
 
const LogContextState_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

◆ LogMessage() [1/3]

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

Create a LogMessage, optionally set to an initial string.

References msg.

◆ LogMessage() [2/3]

nidas::util::LogMessage::LogMessage ( const LogContextState * 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;
}
}
The LogContext is created at a point in the application code and filled in with details about that lo...
Definition Logger.h:531
A class for formatting and streaming a log message.
Definition Logger.h:952
#define LOG_VERBOSE
Definition Logger.h:221
LogMessage & endlog(LogMessage &logmsg)
LogMessage manipulator which logs the current message buffer, if any, and then clears the message.
Definition Logger.h:1107

References msg.

◆ LogMessage() [3/3]

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

References msg.

◆ ~LogMessage()

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

◆ format()

LogMessage & LogMessage::format ( const char * fmt,
... )

◆ getMessage()

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

References msg.

◆ length()

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

◆ log()

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(), and msg.

Referenced by nidas::dynld::StatisticsCruncher::attach(), nidas::util::endlog(), nidas::core::BadSampleFilter::invalidSampleHeader(), nidas::core::CharacterSensor::searchSampleScanners(), and ~LogMessage().

◆ operator std::string()

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

References msg.

◆ operator=()

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

References _log_context, and msg.

Member Data Documentation

◆ _log_context

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

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

◆ msg

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

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