Gaudi Framework, version v20r2

Generated: 18 Jul 2008

Message Class Reference

#include <GaudiKernel/Message.h>

Collaboration diagram for Message:

Collaboration graph
[legend]
List of all members.

Detailed Description

The Message class.

This class is used to contain messages which can then be formatted and sent to a message service

Author:
Iain Last

Definition at line 15 of file Message.h.

Public Member Functions

 Message ()
 Default constructor.
 Message (const char *src, int type, const char *msg)
 Constructor.
 Message (const std::string &src, int type, const std::string &msg)
 Constructor.
 ~Message ()
 Default destructor.
const std::stringgetMessage () const
 Get the message string.
void setMessage (const std::string &msg)
 Set the message string.
int getType () const
 Get the message type.
void setType (int msg_type)
 Set the message type.
const std::stringgetSource () const
 Get the message source.
void setSource (const std::string &src)
 Set the message source.
const std::stringgetFormat () const
 Get the format string.
void setFormat (const std::string &msg) const
 Set the format string.
const std::stringgetTimeFormat () const
 Get the time format string.
void setTimeFormat (const std::string &timeFormat) const
 Set the time format string.
bool operator< (const Message &test)
 Needed to build maps.

Static Public Member Functions

static const std::string getDefaultFormat ()
 Get the default format string.
static const std::string getDefaultTimeFormat ()
 Get the default time format string.

Protected Member Functions

void invalidFormat () const
 Called when an invalid format string is encountered.
void makeFormattedMsg (const std::string &format) const
 Format the message.
void decodeFormat (const std::string &format) const
 Decode format.
void sizeField (const std::string &text) const
 Truncate or pad the output string to the field width as necessary.
void setWidth (const std::string &formatArg) const
 Set the width of a stream.

Protected Attributes

std::string m_message
 The message.
std::string m_source
 The source.
std::string m_format
 The format string.
std::string m_time_format
 Time format string.
int m_type
 The type.
std::string m_formatted_msg
 Formatted message.
char m_fill
 The current fill character.
int m_width
 The current field width.
bool m_left
 Justification.

Static Protected Attributes

static const char FORMAT_PREFIX
 The character used to prefix formatting commands.
static const char JUSTIFY_LEFT
 The character used to indicate start of left text justification.
static const char JUSTIFY_RIGHT
 The character used to indicate start of right text justification.
static const char MESSAGE
 The character used to indicate that the message should be printed.
static const char TYPE
 The character used to indicate that the message type should be printed.
static const char TIME
 The character used to indicate that the message timestamp should be printed.
static const char UTIME
 The character used to indicate that the message timestamp should be printed in UTC time.
static const char SOURCE
 The character used to indicate that the message source should be printed.
static const char FILL
 The character used to indicate that the previous character is to be used for padding out fields if the text is not long enough.
static const char WIDTH
 The character used to indicate that the previous decimal characters should be taken as the field width.
static const char * DEFAULT_FORMAT
 The default message format.
static const char * DEFAULT_TIME_FORMAT
 The default time format.

Friends

std::ostreamoperator<< (std::ostream &stream, const Message &msg)
 Insert the message into a stream.
bool operator== (const Message &a, const Message &b)
 Insert the message into a stream.


Constructor & Destructor Documentation

Message::Message (  ) 

Default constructor.

Definition at line 43 of file Message.cpp.

00043                  : 
00044   m_message( "" ), m_source( "UNKNOWN" ), m_format( DEFAULT_FORMAT ), 
00045   m_time_format(DEFAULT_TIME_FORMAT), m_type( MSG::NIL ), 
00046   m_fill( ' ' ), m_width( 0 ), m_left( true )
00047 {
00048 }

Message::Message ( const char *  src,
int  type,
const char *  msg 
)

Constructor.

Definition at line 56 of file Message.cpp.

00056                                                               :
00057   m_message( msg ), m_source( src ), m_format( DEFAULT_FORMAT ), 
00058   m_time_format(DEFAULT_TIME_FORMAT), m_type( type ), 
00059   m_fill( ' ' ), m_width( 0 ), m_left( true )
00060 {
00061 }

Message::Message ( const std::string src,
int  type,
const std::string msg 
)

Constructor.

Definition at line 69 of file Message.cpp.

00069                                                                         :
00070   m_message( msg ), m_source( src ), m_format( DEFAULT_FORMAT ),
00071   m_time_format(DEFAULT_TIME_FORMAT), m_type( type ), 
00072   m_fill( ' ' ), m_width( 0 ), m_left( true )
00073 {
00074 }

Message::~Message (  )  [inline]

Default destructor.

Definition at line 27 of file Message.h.

00027 {}


Member Function Documentation

const std::string & Message::getMessage (  )  const

Get the message string.

Definition at line 82 of file Message.cpp.

References m_message.

00083 { 
00084   return m_message; 
00085 }

void Message::setMessage ( const std::string msg  ) 

Set the message string.

Definition at line 93 of file Message.cpp.

References m_message.

00094 { 
00095   m_message = msg; 
00096 }

int Message::getType (  )  const

Get the message type.

Definition at line 104 of file Message.cpp.

References m_type.

Referenced by MessageSvc::reportMessage().

00105 { 
00106   return m_type; 
00107 }

void Message::setType ( int  msg_type  ) 

Set the message type.

Definition at line 115 of file Message.cpp.

References m_type.

00116 { 
00117   m_type = msg_type; 
00118 }

const std::string & Message::getSource (  )  const

Get the message source.

Definition at line 126 of file Message.cpp.

References m_source.

Referenced by MessageSvc::reportMessage().

00127 { 
00128   return m_source; 
00129 }

void Message::setSource ( const std::string src  ) 

Set the message source.

Definition at line 137 of file Message.cpp.

References m_source.

Referenced by MessageSvc::reportMessage().

00138 { 
00139   m_source = src; 
00140 }

const std::string & Message::getFormat (  )  const

Get the format string.

Definition at line 187 of file Message.cpp.

References m_format.

Referenced by MessageSvc::reportMessage().

00188 {
00189   return m_format;
00190 }

const std::string Message::getDefaultFormat (  )  [static]

Get the default format string.

Definition at line 198 of file Message.cpp.

References DEFAULT_FORMAT.

Referenced by MessageSvc::MessageSvc().

00199 {
00200   return DEFAULT_FORMAT;
00201 }

void Message::setFormat ( const std::string msg  )  const

Set the format string.

Definition at line 211 of file Message.cpp.

References DEFAULT_FORMAT, std::basic_string< _CharT, _Traits, _Alloc >::empty(), format(), and m_format.

Referenced by MessageSvc::reportMessage().

00212 {
00213   if ( format.empty() )
00214     m_format = DEFAULT_FORMAT;
00215   else
00216     m_format = format;
00217 }

const std::string & Message::getTimeFormat (  )  const

Get the time format string.

Definition at line 225 of file Message.cpp.

References m_time_format.

00226 {
00227   return m_time_format;
00228 }

const std::string Message::getDefaultTimeFormat (  )  [static]

Get the default time format string.

Definition at line 236 of file Message.cpp.

References DEFAULT_TIME_FORMAT.

Referenced by MessageSvc::MessageSvc().

00237 {
00238   return DEFAULT_TIME_FORMAT ;
00239 }

void Message::setTimeFormat ( const std::string timeFormat  )  const

Set the time format string.

Definition at line 249 of file Message.cpp.

References DEFAULT_TIME_FORMAT, std::basic_string< _CharT, _Traits, _Alloc >::empty(), and m_time_format.

Referenced by MessageSvc::reportMessage().

00250 {
00251   if ( timeFormat.empty() )
00252     m_time_format = DEFAULT_TIME_FORMAT;
00253   else
00254     m_time_format = timeFormat;
00255 }  

bool Message::operator< ( const Message test  ) 

Needed to build maps.

Definition at line 161 of file Message.cpp.

References m_message, m_source, and m_type.

00162 {
00163   return m_type   < b.m_type ||
00164          m_source < b.m_source ||
00165          m_message < b.m_message;
00166 }

void Message::invalidFormat (  )  const [protected]

Called when an invalid format string is encountered.

Definition at line 380 of file Message.cpp.

References DEFAULT_FORMAT, and makeFormattedMsg().

Referenced by decodeFormat(), makeFormattedMsg(), and setWidth().

00381 {
00382   makeFormattedMsg( DEFAULT_FORMAT );
00383 }

void Message::makeFormattedMsg ( const std::string format  )  const [protected]

Format the message.

Definition at line 263 of file Message.cpp.

References std::basic_string< _CharT, _Traits, _Alloc >::begin(), decodeFormat(), std::basic_string< _CharT, _Traits, _Alloc >::end(), FILL, format(), FORMAT_PREFIX, invalidFormat(), JUSTIFY_LEFT, JUSTIFY_RIGHT, m_formatted_msg, MESSAGE, SOURCE, TIME, TYPE, UTIME, and WIDTH.

Referenced by invalidFormat(), and operator<<().

00264 {
00265   m_formatted_msg = "";
00266   std::string::const_iterator i = format.begin();
00267   while( i != format.end() ) {
00268     
00269     // Output format string until format statement found.
00270     while(  i != format.end() && *i != FORMAT_PREFIX ) 
00271       m_formatted_msg += *i++;
00272 
00273     // Test for end of format string.
00274     if ( i == format.end() ) break;
00275     i++;
00276 
00277     // Find type of formatting.
00278     std::string this_format = "";
00279     while( i != format.end() && *i != FORMAT_PREFIX &&
00280            *i != MESSAGE && *i != TYPE && *i != SOURCE && 
00281            *i != FILL && *i != WIDTH && *i != TIME && *i != UTIME && 
00282            *i != JUSTIFY_LEFT && *i != JUSTIFY_RIGHT ) {
00283       this_format += *i++;
00284     }
00285     
00286     // Reached end of string with improper format.
00287     if ( i == format.end() ) {
00288       invalidFormat();
00289       break;
00290     }
00291 
00292     this_format += *i++;    
00293     decodeFormat( this_format );
00294   }
00295 }

void Message::decodeFormat ( const std::string format  )  const [protected]

Decode format.

Definition at line 303 of file Message.cpp.

References MSG::ALWAYS, DEBUG, std::basic_string< _CharT, _Traits, _Alloc >::empty(), MSG::ERROR, FATAL, FILL, format(), FORMAT_PREFIX, formattedTime(), INFO, invalidFormat(), JUSTIFY_LEFT, JUSTIFY_RIGHT, std::basic_string< _CharT, _Traits, _Alloc >::length(), m_fill, m_formatted_msg, m_left, m_message, m_source, m_time_format, m_type, MESSAGE, MSG::NIL, SET, setWidth(), sizeField(), SOURCE, std::basic_string< _CharT, _Traits, _Alloc >::substr(), TIME, TYPE, UTIME, MSG::VERBOSE, WARNING, and WIDTH.

Referenced by makeFormattedMsg().

00304 {
00305   if ( ! format.empty() ) {
00306     const char FORMAT_TYPE = format[ format.length() - 1 ];
00307     const std::string FORMAT_PARAM = format.substr( 0, format.length() - 1 );
00308     
00309     // Now test the format.
00310     std::string level;
00311     switch( FORMAT_TYPE ) {
00312     case FILL:
00313       if ( FORMAT_PARAM.length() == 1 ) {
00314         m_fill = FORMAT_PARAM[0];
00315       }
00316       else 
00317         invalidFormat();
00318       break;
00319 
00320     case TIME:
00321       {
00322         const std::string& timeStr = formattedTime ( m_time_format ) ;
00323         sizeField( timeStr );
00324       }
00325       break;
00326       
00327     case UTIME:
00328       {
00329         const std::string& timeStr = formattedTime ( m_time_format, true ) ;
00330         sizeField( timeStr );
00331       }
00332       break;
00333 
00334     case MESSAGE: 
00335       sizeField( m_message );
00336       break;
00337       
00338     case SOURCE: 
00339       sizeField( m_source );
00340       break;
00341       
00342     case TYPE: 
00343       switch ( m_type )    {
00344         using namespace MSG;
00345 #define SET(x)  case x:  level=#x;  break 
00346         SET( NIL );
00347         SET( VERBOSE );
00348         SET( DEBUG );
00349         SET( INFO );
00350         SET( WARNING );
00351         SET( ERROR );
00352         SET( FATAL );
00353         case ALWAYS:       level="SUCCESS"; break;
00354         default:
00355               level = "UNKNOWN";
00356               break;
00357 #undef SET
00358       }
00359       sizeField( level );
00360       break;
00361 
00362     case FORMAT_PREFIX: m_formatted_msg += FORMAT_PREFIX; break;      
00363     case JUSTIFY_RIGHT: m_left = false; break;    
00364     case JUSTIFY_LEFT: m_left = true; break;      
00365     case WIDTH: setWidth( FORMAT_PARAM ); break;      
00366     default: invalidFormat(); break;
00367     }
00368   }
00369   else
00370     invalidFormat();
00371 }

void Message::sizeField ( const std::string text  )  const [protected]

Truncate or pad the output string to the field width as necessary.

Definition at line 423 of file Message.cpp.

References std::basic_string< _CharT, _Traits, _Alloc >::begin(), std::basic_string< _CharT, _Traits, _Alloc >::end(), std::basic_string< _CharT, _Traits, _Alloc >::length(), m_fill, m_formatted_msg, m_left, m_width, std::basic_string< _CharT, _Traits, _Alloc >::replace(), and std::basic_string< _CharT, _Traits, _Alloc >::substr().

Referenced by decodeFormat().

00424 {
00425   std::string newText;
00426   if ( m_width == 0 || m_width == static_cast<int>( text.length() ) ) {
00427     newText = text;
00428   }
00429   else {
00430     
00431     // Truncate the text if it is too long.
00432     if ( m_width < static_cast<int>( text.length() ) ) {
00433       newText = text.substr( 0, m_width );
00434       for ( int i = 0, j = newText.length()-1; i < 3 && j >= 0; i++, j-- )
00435         newText[ j ] = '.';
00436     }
00437     
00438     // Pad the text.
00439     else {
00440       newText = std::string( m_width, m_fill );
00441       if ( m_left ) 
00442         newText.replace( newText.begin(), newText.begin() + text.length(), 
00443                          text.begin(), text.end() );
00444       else
00445         newText.replace( newText.end() - text.length(), newText.end(), 
00446                          text.begin(), text.end() );
00447     }
00448   }
00449 
00450   m_formatted_msg += newText;
00451 }

void Message::setWidth ( const std::string formatArg  )  const [protected]

Set the width of a stream.

Definition at line 392 of file Message.cpp.

References std::basic_string< _CharT, _Traits, _Alloc >::begin(), std::basic_string< _CharT, _Traits, _Alloc >::c_str(), std::basic_string< _CharT, _Traits, _Alloc >::data(), std::basic_string< _CharT, _Traits, _Alloc >::end(), invalidFormat(), isdigit(), and m_width.

Referenced by decodeFormat().

00393 {
00394   // Check that the parameters are only digits.
00395   bool only_digits = true;
00396   for( std::string::const_iterator i = formatArg.begin();
00397        i != formatArg.end(); i++ ) {
00398     
00399     if ( ! isdigit( *i ) ) {
00400       only_digits = false;
00401       invalidFormat();
00402       break;
00403     }
00404   }
00405   
00406   // Convert string to int.
00407   if ( only_digits ) {
00408 #ifdef __GNUG__
00409     m_width = atoi( formatArg.c_str() );
00410 #else
00411     m_width = atoi( formatArg.data() );
00412 #endif
00413   }
00414 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream stream,
const Message msg 
) [friend]

Insert the message into a stream.

Definition at line 148 of file Message.cpp.

00149 {
00150   msg.makeFormattedMsg( msg.m_format );
00151   stream << msg.m_formatted_msg;
00152   return stream;
00153 }

bool operator== ( const Message a,
const Message b 
) [friend]

Insert the message into a stream.

Definition at line 174 of file Message.cpp.

00175 {
00176   return a.m_source == b.m_source &&
00177     a.m_type == b.m_type &&
00178     a.m_message == b.m_message;
00179 }


Member Data Documentation

std::string Message::m_message [protected]

The message.

Definition at line 91 of file Message.h.

Referenced by decodeFormat(), getMessage(), operator<(), operator==(), and setMessage().

std::string Message::m_source [protected]

The source.

Definition at line 94 of file Message.h.

Referenced by decodeFormat(), getSource(), operator<(), operator==(), and setSource().

std::string Message::m_format [mutable, protected]

The format string.

Definition at line 97 of file Message.h.

Referenced by getFormat(), operator<<(), and setFormat().

std::string Message::m_time_format [mutable, protected]

Time format string.

Definition at line 100 of file Message.h.

Referenced by decodeFormat(), getTimeFormat(), and setTimeFormat().

int Message::m_type [protected]

The type.

Definition at line 103 of file Message.h.

Referenced by decodeFormat(), getType(), operator<(), operator==(), and setType().

std::string Message::m_formatted_msg [mutable, protected]

Formatted message.

Definition at line 106 of file Message.h.

Referenced by decodeFormat(), makeFormattedMsg(), operator<<(), and sizeField().

char Message::m_fill [mutable, protected]

The current fill character.

Definition at line 109 of file Message.h.

Referenced by decodeFormat(), and sizeField().

int Message::m_width [mutable, protected]

The current field width.

Definition at line 112 of file Message.h.

Referenced by setWidth(), and sizeField().

bool Message::m_left [mutable, protected]

Justification.

Definition at line 115 of file Message.h.

Referenced by decodeFormat(), and sizeField().

const char Message::FORMAT_PREFIX [static, protected]

The character used to prefix formatting commands.

Definition at line 118 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::JUSTIFY_LEFT [static, protected]

The character used to indicate start of left text justification.

Definition at line 121 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::JUSTIFY_RIGHT [static, protected]

The character used to indicate start of right text justification.

Definition at line 124 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::MESSAGE [static, protected]

The character used to indicate that the message should be printed.

Definition at line 127 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::TYPE [static, protected]

The character used to indicate that the message type should be printed.

Definition at line 130 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::TIME [static, protected]

The character used to indicate that the message timestamp should be printed.

Definition at line 133 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::UTIME [static, protected]

The character used to indicate that the message timestamp should be printed in UTC time.

Definition at line 137 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::SOURCE [static, protected]

The character used to indicate that the message source should be printed.

Definition at line 140 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::FILL [static, protected]

The character used to indicate that the previous character is to be used for padding out fields if the text is not long enough.

Definition at line 145 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char Message::WIDTH [static, protected]

The character used to indicate that the previous decimal characters should be taken as the field width.

Definition at line 150 of file Message.h.

Referenced by decodeFormat(), and makeFormattedMsg().

const char * Message::DEFAULT_FORMAT [static, protected]

The default message format.

Definition at line 153 of file Message.h.

Referenced by getDefaultFormat(), invalidFormat(), and setFormat().

const char * Message::DEFAULT_TIME_FORMAT [static, protected]

The default time format.

Definition at line 156 of file Message.h.

Referenced by getDefaultTimeFormat(), and setTimeFormat().


The documentation for this class was generated from the following files:
Generated at Fri Jul 18 12:08:36 2008 for Gaudi Framework, version v20r2 by Doxygen version 1.5.1 written by Dimitri van Heesch, © 1997-2004