All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Message Class Reference

The Message class. More...

#include <GaudiKernel/Message.h>

Collaboration diagram for Message:

Public Member Functions

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

Static Public Member Functions

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

Protected Member Functions

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

Protected Attributes

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

Static Protected Attributes

static const char FORMAT_PREFIX = '%'
 The character used to prefix formatting commands. More...
 
static const char JUSTIFY_LEFT = 'L'
 The character used to indicate start of left text justification. More...
 
static const char JUSTIFY_RIGHT = 'R'
 The character used to indicate start of right text justification. More...
 
static const char MESSAGE = 'M'
 The character used to indicate that the message should be printed. More...
 
static const char TYPE = 'T'
 The character used to indicate that the message type should be printed. More...
 
static const char TIME = 't'
 The character used to indicate that the message timestamp should be printed. More...
 
static const char UTIME = 'u'
 The character used to indicate that the message timestamp should be printed in UTC time. More...
 
static const char SOURCE = 'S'
 The character used to indicate that the message source should be printed. More...
 
static const char FILL = 'F'
 The character used to indicate that the previous character is to be used for padding out fields if the text is not long enough. More...
 
static const char WIDTH = 'W'
 The character used to indicate that the previous decimal characters should be taken as the field width. More...
 
static const char * DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M"
 The default message format. More...
 
static const char * DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f"
 The default time format. More...
 

Friends

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

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.

Constructor & Destructor Documentation

Message::Message ( )

Default constructor.

Definition at line 45 of file Message.cpp.

45  :
46  m_message( "" ), m_source( "UNKNOWN" ), m_format( DEFAULT_FORMAT ),
48  m_fill( ' ' ), m_width( 0 ), m_left( true )
49 {
50 }
bool m_left
Justification.
Definition: Message.h:115
char m_fill
The current fill character.
Definition: Message.h:109
std::string m_time_format
Time format string.
Definition: Message.h:100
std::string m_message
The message.
Definition: Message.h:91
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
static const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:156
int m_width
The current field width.
Definition: Message.h:112
std::string m_format
The format string.
Definition: Message.h:97
std::string m_source
The source.
Definition: Message.h:94
int m_type
The type.
Definition: Message.h:103
Message::Message ( const char *  src,
int  type,
const char *  msg 
)

Constructor.

Definition at line 58 of file Message.cpp.

58  :
61  m_fill( ' ' ), m_width( 0 ), m_left( true )
62 {
63 }
bool m_left
Justification.
Definition: Message.h:115
char m_fill
The current fill character.
Definition: Message.h:109
std::string m_time_format
Time format string.
Definition: Message.h:100
std::string m_message
The message.
Definition: Message.h:91
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
string type
Definition: gaudirun.py:126
static const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:156
int m_width
The current field width.
Definition: Message.h:112
std::string m_format
The format string.
Definition: Message.h:97
std::string m_source
The source.
Definition: Message.h:94
int m_type
The type.
Definition: Message.h:103
Message::Message ( const std::string &  src,
int  type,
const std::string &  msg 
)

Constructor.

Definition at line 71 of file Message.cpp.

71  :
74  m_fill( ' ' ), m_width( 0 ), m_left( true )
75 {
76 }
bool m_left
Justification.
Definition: Message.h:115
char m_fill
The current fill character.
Definition: Message.h:109
std::string m_time_format
Time format string.
Definition: Message.h:100
std::string m_message
The message.
Definition: Message.h:91
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
string type
Definition: gaudirun.py:126
static const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:156
int m_width
The current field width.
Definition: Message.h:112
std::string m_format
The format string.
Definition: Message.h:97
std::string m_source
The source.
Definition: Message.h:94
int m_type
The type.
Definition: Message.h:103
Message::~Message ( )
inline

Default destructor.

Definition at line 27 of file Message.h.

27 {}

Member Function Documentation

void Message::decodeFormat ( const std::string &  format) const
protected

Decode format.

Definition at line 305 of file Message.cpp.

306 {
307  if ( ! format.empty() ) {
308  const char FORMAT_TYPE = format[ format.length() - 1 ];
309  const std::string FORMAT_PARAM = format.substr( 0, format.length() - 1 );
310 
311  // Now test the format.
312  std::string level;
313  switch( FORMAT_TYPE ) {
314  case FILL:
315  if ( FORMAT_PARAM.length() == 1 ) {
316  m_fill = FORMAT_PARAM[0];
317  }
318  else
319  invalidFormat();
320  break;
321 
322  case TIME:
323  {
324  const std::string& timeStr = formattedTime ( m_time_format ) ;
325  sizeField( timeStr );
326  }
327  break;
328 
329  case UTIME:
330  {
331  const std::string& timeStr = formattedTime ( m_time_format, true ) ;
332  sizeField( timeStr );
333  }
334  break;
335 
336  case MESSAGE:
337  sizeField( m_message );
338  break;
339 
340  case SOURCE:
341  sizeField( m_source );
342  break;
343 
344  case TYPE:
345  switch ( m_type ) {
346 #define SET(x) case x: level=#x; break
347  SET( NIL );
348  SET( VERBOSE );
349  SET( DEBUG );
350  SET( INFO );
351  SET( WARNING );
352  SET( ERROR );
353  SET( FATAL );
354  case ALWAYS: level="SUCCESS"; break;
355  default:
356  level = "UNKNOWN";
357  break;
358 #undef SET
359  }
360  sizeField( level );
361  break;
362 
364  case JUSTIFY_RIGHT: m_left = false; break;
365  case JUSTIFY_LEFT: m_left = true; break;
366  case WIDTH: setWidth( FORMAT_PARAM ); break;
367  default: invalidFormat(); break;
368  }
369  }
370  else
371  invalidFormat();
372 }
static const char TYPE
The character used to indicate that the message type should be printed.
Definition: Message.h:130
bool m_left
Justification.
Definition: Message.h:115
static const char MESSAGE
The character used to indicate that the message should be printed.
Definition: Message.h:127
char m_fill
The current fill character.
Definition: Message.h:109
static const char JUSTIFY_LEFT
The character used to indicate start of left text justification.
Definition: Message.h:121
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
std::string m_formatted_msg
Formatted message.
Definition: Message.h:106
std::string m_time_format
Time format string.
Definition: Message.h:100
void setWidth(const std::string &formatArg) const
Set the width of a stream.
Definition: Message.cpp:393
std::string m_message
The message.
Definition: Message.h:91
#define SET(x)
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:381
static const char TIME
The character used to indicate that the message timestamp should be printed.
Definition: Message.h:133
static const char JUSTIFY_RIGHT
The character used to indicate start of right text justification.
Definition: Message.h:124
static const char UTIME
The character used to indicate that the message timestamp should be printed in UTC time...
Definition: Message.h:137
static const char FILL
The character used to indicate that the previous character is to be used for padding out fields if th...
Definition: Message.h:145
void sizeField(const std::string &text) const
Truncate or pad the output string to the field width as necessary.
Definition: Message.cpp:424
static const char FORMAT_PREFIX
The character used to prefix formatting commands.
Definition: Message.h:118
std::string m_source
The source.
Definition: Message.h:94
static const char WIDTH
The character used to indicate that the previous decimal characters should be taken as the field widt...
Definition: Message.h:150
static const char SOURCE
The character used to indicate that the message source should be printed.
Definition: Message.h:140
int m_type
The type.
Definition: Message.h:103
const std::string Message::getDefaultFormat ( )
static

Get the default format string.

Definition at line 200 of file Message.cpp.

201 {
202  return DEFAULT_FORMAT;
203 }
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
const std::string Message::getDefaultTimeFormat ( )
static

Get the default time format string.

Definition at line 238 of file Message.cpp.

239 {
240  return DEFAULT_TIME_FORMAT ;
241 }
static const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:156
const std::string & Message::getFormat ( ) const

Get the format string.

Definition at line 189 of file Message.cpp.

190 {
191  return m_format;
192 }
std::string m_format
The format string.
Definition: Message.h:97
const std::string & Message::getMessage ( ) const

Get the message string.

Definition at line 84 of file Message.cpp.

85 {
86  return m_message;
87 }
std::string m_message
The message.
Definition: Message.h:91
const std::string & Message::getSource ( ) const

Get the message source.

Definition at line 128 of file Message.cpp.

129 {
130  return m_source;
131 }
std::string m_source
The source.
Definition: Message.h:94
const std::string & Message::getTimeFormat ( ) const

Get the time format string.

Definition at line 227 of file Message.cpp.

228 {
229  return m_time_format;
230 }
std::string m_time_format
Time format string.
Definition: Message.h:100
int Message::getType ( ) const

Get the message type.

Definition at line 106 of file Message.cpp.

107 {
108  return m_type;
109 }
int m_type
The type.
Definition: Message.h:103
void Message::invalidFormat ( ) const
protected

Called when an invalid format string is encountered.

Definition at line 381 of file Message.cpp.

382 {
384 }
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
void makeFormattedMsg(const std::string &format) const
Format the message.
Definition: Message.cpp:265
void Message::makeFormattedMsg ( const std::string &  format) const
protected

Format the message.

Definition at line 265 of file Message.cpp.

266 {
267  m_formatted_msg = "";
268  std::string::const_iterator i = format.begin();
269  while( i != format.end() ) {
270 
271  // Output format string until format statement found.
272  while( i != format.end() && *i != FORMAT_PREFIX )
273  m_formatted_msg += *i++;
274 
275  // Test for end of format string.
276  if ( i == format.end() ) break;
277  i++;
278 
279  // Find type of formatting.
280  std::string this_format = "";
281  while( i != format.end() && *i != FORMAT_PREFIX &&
282  *i != MESSAGE && *i != TYPE && *i != SOURCE &&
283  *i != FILL && *i != WIDTH && *i != TIME && *i != UTIME &&
284  *i != JUSTIFY_LEFT && *i != JUSTIFY_RIGHT ) {
285  this_format += *i++;
286  }
287 
288  // Reached end of string with improper format.
289  if ( i == format.end() ) {
290  invalidFormat();
291  break;
292  }
293 
294  this_format += *i++;
295  decodeFormat( this_format );
296  }
297 }
static const char TYPE
The character used to indicate that the message type should be printed.
Definition: Message.h:130
static const char MESSAGE
The character used to indicate that the message should be printed.
Definition: Message.h:127
static const char JUSTIFY_LEFT
The character used to indicate start of left text justification.
Definition: Message.h:121
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
std::string m_formatted_msg
Formatted message.
Definition: Message.h:106
void decodeFormat(const std::string &format) const
Decode format.
Definition: Message.cpp:305
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:381
static const char TIME
The character used to indicate that the message timestamp should be printed.
Definition: Message.h:133
static const char JUSTIFY_RIGHT
The character used to indicate start of right text justification.
Definition: Message.h:124
static const char UTIME
The character used to indicate that the message timestamp should be printed in UTC time...
Definition: Message.h:137
static const char FILL
The character used to indicate that the previous character is to be used for padding out fields if th...
Definition: Message.h:145
static const char FORMAT_PREFIX
The character used to prefix formatting commands.
Definition: Message.h:118
list i
Definition: ana.py:128
static const char WIDTH
The character used to indicate that the previous decimal characters should be taken as the field widt...
Definition: Message.h:150
static const char SOURCE
The character used to indicate that the message source should be printed.
Definition: Message.h:140
bool Message::operator< ( const Message test)

Needed to build maps.

Definition at line 163 of file Message.cpp.

164 {
165  return m_type < b.m_type ||
166  m_source < b.m_source ||
167  m_message < b.m_message;
168 }
std::string m_message
The message.
Definition: Message.h:91
std::string m_source
The source.
Definition: Message.h:94
int m_type
The type.
Definition: Message.h:103
void Message::setFormat ( const std::string &  msg) const

Set the format string.

Definition at line 213 of file Message.cpp.

214 {
215  if ( format.empty() )
217  else
218  m_format = format;
219 }
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
static const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:153
std::string m_format
The format string.
Definition: Message.h:97
void Message::setMessage ( const std::string &  msg)

Set the message string.

Definition at line 95 of file Message.cpp.

96 {
97  m_message = msg;
98 }
std::string m_message
The message.
Definition: Message.h:91
void Message::setSource ( const std::string &  src)

Set the message source.

Definition at line 139 of file Message.cpp.

140 {
141  m_source = src;
142 }
std::string m_source
The source.
Definition: Message.h:94
void Message::setTimeFormat ( const std::string &  timeFormat) const

Set the time format string.

Definition at line 251 of file Message.cpp.

252 {
253  if ( timeFormat.empty() )
255  else
256  m_time_format = timeFormat;
257 }
std::string m_time_format
Time format string.
Definition: Message.h:100
static const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:156
void Message::setType ( int  msg_type)

Set the message type.

Definition at line 117 of file Message.cpp.

118 {
119  m_type = msg_type;
120 }
int m_type
The type.
Definition: Message.h:103
void Message::setWidth ( const std::string &  formatArg) const
protected

Set the width of a stream.

Definition at line 393 of file Message.cpp.

394 {
395  // Check that the parameters are only digits.
396  bool only_digits = true;
397  for( std::string::const_iterator i = formatArg.begin();
398  i != formatArg.end(); i++ ) {
399 
400  if ( ! isdigit( *i ) ) {
401  only_digits = false;
402  invalidFormat();
403  break;
404  }
405  }
406 
407  // Convert string to int.
408  if ( only_digits ) {
409 #ifdef __GNUG__
410  m_width = atoi( formatArg.c_str() );
411 #else
412  m_width = atoi( formatArg.data() );
413 #endif
414  }
415 }
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:381
int m_width
The current field width.
Definition: Message.h:112
list i
Definition: ana.py:128
void Message::sizeField ( const std::string &  text) const
protected

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

Definition at line 424 of file Message.cpp.

425 {
426  std::string newText;
427  if ( m_width == 0 || m_width == static_cast<int>( text.length() ) ) {
428  newText = text;
429  }
430  else {
431 
432  // Truncate the text if it is too long.
433  if ( m_width < static_cast<int>( text.length() ) ) {
434  newText = text.substr( 0, m_width );
435  for ( int i = 0, j = newText.length()-1; i < 3 && j >= 0; i++, j-- )
436  newText[ j ] = '.';
437  }
438 
439  // Pad the text.
440  else {
441  newText = std::string( m_width, m_fill );
442  if ( m_left )
443  newText.replace( newText.begin(), newText.begin() + text.length(),
444  text.begin(), text.end() );
445  else
446  newText.replace( newText.end() - text.length(), newText.end(),
447  text.begin(), text.end() );
448  }
449  }
450 
451  m_formatted_msg += newText;
452 }
bool m_left
Justification.
Definition: Message.h:115
char m_fill
The current fill character.
Definition: Message.h:109
std::string m_formatted_msg
Formatted message.
Definition: Message.h:106
int m_width
The current field width.
Definition: Message.h:112
list i
Definition: ana.py:128

Friends And Related Function Documentation

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

Insert the message into a stream.

Definition at line 150 of file Message.cpp.

151 {
152  msg.makeFormattedMsg( msg.m_format );
153  stream << msg.m_formatted_msg;
154  return stream;
155 }
std::string m_formatted_msg
Formatted message.
Definition: Message.h:106
std::string m_format
The format string.
Definition: Message.h:97
void makeFormattedMsg(const std::string &format) const
Format the message.
Definition: Message.cpp:265
bool operator== ( const Message a,
const Message b 
)
friend

Insert the message into a stream.

Definition at line 176 of file Message.cpp.

177 {
178  return a.m_source == b.m_source &&
179  a.m_type == b.m_type &&
180  a.m_message == b.m_message;
181 }
std::string m_message
The message.
Definition: Message.h:91
std::string m_source
The source.
Definition: Message.h:94
int m_type
The type.
Definition: Message.h:103

Member Data Documentation

const char * Message::DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M"
staticprotected

The default message format.

Definition at line 153 of file Message.h.

const char * Message::DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f"
staticprotected

The default time format.

Definition at line 156 of file Message.h.

const char Message::FILL = 'F'
staticprotected

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.

const char Message::FORMAT_PREFIX = '%'
staticprotected

The character used to prefix formatting commands.

Definition at line 118 of file Message.h.

const char Message::JUSTIFY_LEFT = 'L'
staticprotected

The character used to indicate start of left text justification.

Definition at line 121 of file Message.h.

const char Message::JUSTIFY_RIGHT = 'R'
staticprotected

The character used to indicate start of right text justification.

Definition at line 124 of file Message.h.

char Message::m_fill
mutableprotected

The current fill character.

Definition at line 109 of file Message.h.

std::string Message::m_format
mutableprotected

The format string.

Definition at line 97 of file Message.h.

std::string Message::m_formatted_msg
mutableprotected

Formatted message.

Definition at line 106 of file Message.h.

bool Message::m_left
mutableprotected

Justification.

Definition at line 115 of file Message.h.

std::string Message::m_message
protected

The message.

Definition at line 91 of file Message.h.

std::string Message::m_source
protected

The source.

Definition at line 94 of file Message.h.

std::string Message::m_time_format
mutableprotected

Time format string.

Definition at line 100 of file Message.h.

int Message::m_type
protected

The type.

Definition at line 103 of file Message.h.

int Message::m_width
mutableprotected

The current field width.

Definition at line 112 of file Message.h.

const char Message::MESSAGE = 'M'
staticprotected

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

Definition at line 127 of file Message.h.

const char Message::SOURCE = 'S'
staticprotected

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

Definition at line 140 of file Message.h.

const char Message::TIME = 't'
staticprotected

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

Definition at line 133 of file Message.h.

const char Message::TYPE = 'T'
staticprotected

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

Definition at line 130 of file Message.h.

const char Message::UTIME = 'u'
staticprotected

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

Definition at line 137 of file Message.h.

const char Message::WIDTH = 'W'
staticprotected

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.


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