Message Class Referencefinal

The Message class. More...

#include <GaudiKernel/Message.h>

Collaboration diagram for Message:

Public Member Functions

 Message ()=default
 Default constructor. More...
 
 Message (const char *src, int type, const char *msg)
 Constructor. More...
 
 Message (std::string src, int type, std::string msg)
 Constructor. More...
 
 ~Message ()=default
 Default destructor. More...
 
const std::string & getMessage () const
 Get the message string. More...
 
void setMessage (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 (std::string src)
 Set the message source. More...
 
const std::string & getFormat () const
 Get the format string. More...
 
void setFormat (std::string msg) const
 Set the format string. More...
 
const std::string & getTimeFormat () const
 Get the time format string. More...
 
void setTimeFormat (std::string timeFormat) const
 Set the time format string. More...
 
bool operator< (const Message &test)
 Needed to build maps. More...
 
 Message ()=default
 Default constructor. More...
 
 Message (const char *src, int type, const char *msg)
 Constructor. More...
 
 Message (std::string src, int type, std::string msg)
 Constructor. More...
 
 ~Message ()=default
 Default destructor. More...
 
const std::string & getMessage () const
 Get the message string. More...
 
void setMessage (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 (std::string src)
 Set the message source. More...
 
const std::string & getFormat () const
 Get the format string. More...
 
void setFormat (std::string msg) const
 Set the format string. More...
 
const std::string & getTimeFormat () const
 Get the time format string. More...
 
void setTimeFormat (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...
 
static const std::string getDefaultFormat ()
 Get the default format string. More...
 
static const std::string getDefaultTimeFormat ()
 Get the default time format string. More...
 

Private 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...
 
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...
 

Private Attributes

std::string m_message
 The message. More...
 
std::string m_source = "UNKNOWN"
 The source. More...
 
std::string m_format = DEFAULT_FORMAT
 The format string. More...
 
std::string m_time_format = DEFAULT_TIME_FORMAT
 Time format string. More...
 
int m_type = 0
 The type. More...
 
std::string m_formatted_msg
 Formatted message. More...
 
char m_fill = ' '
 The current fill character. More...
 
int m_width = 0
 The current field width. More...
 
bool m_left = true
 Justification. More...
 

Static Private Attributes

static const char FORMAT_PREFIX = '%'
 Formatting string characters. 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 constexpr const char * DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M"
 The default message format. More...
 
static constexpr 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...
 
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 14 of file Message.h.

Constructor & Destructor Documentation

Message::Message ( )
default

Default constructor.

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

Constructor.

Definition at line 29 of file Message.cpp.

29  :
30  m_message( msg ), m_source( src ), m_type( type )
31 {
32 }
std::string m_message
The message.
Definition: Message.h:90
std::string m_source
The source.
Definition: Message.h:93
string type
Definition: gaudirun.py:151
int m_type
The type.
Definition: Message.h:102
Message::Message ( std::string  src,
int  type,
std::string  msg 
)

Constructor.

Definition at line 40 of file Message.cpp.

40  :
41  m_message( std::move(msg) ), m_source( std::move(src) ), m_type( type )
42 {
43 }
std::string m_message
The message.
Definition: Message.h:90
std::string m_source
The source.
Definition: Message.h:93
string type
Definition: gaudirun.py:151
int m_type
The type.
Definition: Message.h:102
Message::~Message ( )
default

Default destructor.

Message::Message ( )
default

Default constructor.

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

Constructor.

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

Constructor.

Message::~Message ( )
default

Default destructor.

Member Function Documentation

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

Decode format.

Definition at line 271 of file Message.cpp.

272 {
273  if ( ! format.empty() ) {
274  const char FORMAT_TYPE = format[ format.length() - 1 ];
275  const std::string FORMAT_PARAM = format.substr( 0, format.length() - 1 );
276 
277  // Now test the format.
278  std::string level;
279  switch( FORMAT_TYPE ) {
280  case FILL:
281  if ( FORMAT_PARAM.length() == 1 ) {
282  m_fill = FORMAT_PARAM[0];
283  }
284  else
285  invalidFormat();
286  break;
287 
288  case TIME:
289  {
290  sizeField( formattedTime ( m_time_format ) );
291  }
292  break;
293 
294  case UTIME:
295  {
296  sizeField( formattedTime ( m_time_format, true ) );
297  }
298  break;
299 
300  case MESSAGE:
301  sizeField( m_message );
302  break;
303 
304  case SOURCE:
305  sizeField( m_source );
306  break;
307 
308  case TYPE:
309  switch ( m_type ) {
310 #define SET(x) case x: level=#x; break
311  SET( NIL );
312  SET( VERBOSE );
313  SET( DEBUG );
314  SET( INFO );
315  SET( WARNING );
316  SET( ERROR );
317  SET( FATAL );
318  case ALWAYS: level="SUCCESS"; break;
319  default:
320  level = "UNKNOWN";
321  break;
322 #undef SET
323  }
324  sizeField( level );
325  break;
326 
328  case JUSTIFY_RIGHT: m_left = false; break;
329  case JUSTIFY_LEFT: m_left = true; break;
330  case WIDTH: setWidth( FORMAT_PARAM ); break;
331  default: invalidFormat(); break;
332  }
333  }
334  else
335  invalidFormat();
336 }
bool m_left
Justification.
Definition: Message.h:114
char m_fill
The current fill character.
Definition: Message.h:108
static const char TYPE
The character used to indicate that the message type should be printed.
Definition: Message.h:131
std::string m_formatted_msg
Formatted message.
Definition: Message.h:105
std::string m_time_format
Time format string.
Definition: Message.h:99
void setWidth(const std::string &formatArg) const
Set the width of a stream.
Definition: Message.cpp:370
static const char JUSTIFY_LEFT
The character used to indicate start of left text justification.
Definition: Message.h:122
std::string m_message
The message.
Definition: Message.h:90
#define SET(x)
static const char UTIME
The character used to indicate that the message timestamp should be printed in UTC time...
Definition: Message.h:138
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:345
static const char FORMAT_PREFIX
Formatting string characters.
Definition: Message.h:119
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:146
static const char WIDTH
The character used to indicate that the previous decimal characters should be taken as the field widt...
Definition: Message.h:151
static const char MESSAGE
The character used to indicate that the message should be printed.
Definition: Message.h:128
static const char TIME
The character used to indicate that the message timestamp should be printed.
Definition: Message.h:134
static const char JUSTIFY_RIGHT
The character used to indicate start of right text justification.
Definition: Message.h:125
void sizeField(const std::string &text) const
Truncate or pad the output string to the field width as necessary.
Definition: Message.cpp:384
static const char SOURCE
The character used to indicate that the message source should be printed.
Definition: Message.h:141
std::string m_source
The source.
Definition: Message.h:93
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
int m_type
The type.
Definition: Message.h:102
void Message::decodeFormat ( const std::string &  format) const
private

Decode format.

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

Get the default format string.

Definition at line 167 of file Message.cpp.

168 {
169  return DEFAULT_FORMAT;
170 }
static constexpr const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:155
static const std::string Message::getDefaultFormat ( )
static

Get the default format string.

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

Get the default time format string.

Definition at line 206 of file Message.cpp.

207 {
208  return DEFAULT_TIME_FORMAT ;
209 }
static constexpr const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:159
static const std::string Message::getDefaultTimeFormat ( )
static

Get the default time format string.

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

Get the format string.

Definition at line 156 of file Message.cpp.

157 {
158  return m_format;
159 }
std::string m_format
The format string.
Definition: Message.h:96
const std::string& Message::getFormat ( ) const

Get the format string.

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

Get the message string.

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

Get the message string.

Definition at line 51 of file Message.cpp.

52 {
53  return m_message;
54 }
std::string m_message
The message.
Definition: Message.h:90
const std::string & Message::getSource ( ) const

Get the message source.

Definition at line 95 of file Message.cpp.

96 {
97  return m_source;
98 }
std::string m_source
The source.
Definition: Message.h:93
const std::string& Message::getSource ( ) const

Get the message source.

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

Get the time format string.

Definition at line 195 of file Message.cpp.

196 {
197  return m_time_format;
198 }
std::string m_time_format
Time format string.
Definition: Message.h:99
const std::string& Message::getTimeFormat ( ) const

Get the time format string.

int Message::getType ( ) const

Get the message type.

int Message::getType ( ) const

Get the message type.

Definition at line 73 of file Message.cpp.

74 {
75  return m_type;
76 }
int m_type
The type.
Definition: Message.h:102
void Message::invalidFormat ( ) const
private

Called when an invalid format string is encountered.

void Message::invalidFormat ( ) const
private

Called when an invalid format string is encountered.

Definition at line 345 of file Message.cpp.

346 {
348 }
static constexpr const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:155
void makeFormattedMsg(const std::string &format) const
Format the message.
Definition: Message.cpp:231
void Message::makeFormattedMsg ( const std::string &  format) const
private

Format the message.

void Message::makeFormattedMsg ( const std::string &  format) const
private

Format the message.

Definition at line 231 of file Message.cpp.

232 {
233  m_formatted_msg.clear();
234  auto i = format.begin();
235  while( i != format.end() ) {
236 
237  // Output format string until format statement found.
238  while( i != format.end() && *i != FORMAT_PREFIX )
239  m_formatted_msg += *i++;
240 
241  // Test for end of format string.
242  if ( i == format.end() ) break;
243  i++;
244 
245  // Find type of formatting.
246  std::string this_format;
247  while( i != format.end() && *i != FORMAT_PREFIX &&
248  *i != MESSAGE && *i != TYPE && *i != SOURCE &&
249  *i != FILL && *i != WIDTH && *i != TIME && *i != UTIME &&
250  *i != JUSTIFY_LEFT && *i != JUSTIFY_RIGHT ) {
251  this_format += *i++;
252  }
253 
254  // Reached end of string with improper format.
255  if ( i == format.end() ) {
256  invalidFormat();
257  break;
258  }
259 
260  this_format += *i++;
261  decodeFormat( this_format );
262  }
263 }
static const char TYPE
The character used to indicate that the message type should be printed.
Definition: Message.h:131
std::string m_formatted_msg
Formatted message.
Definition: Message.h:105
void decodeFormat(const std::string &format) const
Decode format.
Definition: Message.cpp:271
static const char JUSTIFY_LEFT
The character used to indicate start of left text justification.
Definition: Message.h:122
static const char UTIME
The character used to indicate that the message timestamp should be printed in UTC time...
Definition: Message.h:138
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:345
static const char FORMAT_PREFIX
Formatting string characters.
Definition: Message.h:119
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:146
static const char WIDTH
The character used to indicate that the previous decimal characters should be taken as the field widt...
Definition: Message.h:151
static const char MESSAGE
The character used to indicate that the message should be printed.
Definition: Message.h:128
static const char TIME
The character used to indicate that the message timestamp should be printed.
Definition: Message.h:134
static const char JUSTIFY_RIGHT
The character used to indicate start of right text justification.
Definition: Message.h:125
static const char SOURCE
The character used to indicate that the message source should be printed.
Definition: Message.h:141
list i
Definition: ana.py:128
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
bool Message::operator< ( const Message test)

Needed to build maps.

bool Message::operator< ( const Message test)

Needed to build maps.

Definition at line 130 of file Message.cpp.

131 {
132  return m_type < b.m_type ||
133  m_source < b.m_source ||
134  m_message < b.m_message;
135 }
std::string m_message
The message.
Definition: Message.h:90
std::string m_source
The source.
Definition: Message.h:93
int m_type
The type.
Definition: Message.h:102
void Message::setFormat ( std::string  msg) const

Set the format string.

Definition at line 180 of file Message.cpp.

181 {
182  if (LIKELY(!format.empty())) {
183  m_format = std::move(format);
184  } else {
186  }
187 }
static constexpr const char * DEFAULT_FORMAT
The default message format.
Definition: Message.h:155
std::string m_format
The format string.
Definition: Message.h:96
#define LIKELY(x)
Definition: Kernel.h:125
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
void Message::setFormat ( std::string  msg) const

Set the format string.

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

Set the message string.

Definition at line 62 of file Message.cpp.

63 {
64  m_message = std::move(msg);
65 }
std::string m_message
The message.
Definition: Message.h:90
void Message::setMessage ( std::string  msg)

Set the message string.

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

Set the message source.

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

Set the message source.

Definition at line 106 of file Message.cpp.

107 {
108  m_source = std::move(src);
109 }
std::string m_source
The source.
Definition: Message.h:93
void Message::setTimeFormat ( std::string  timeFormat) const

Set the time format string.

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

Set the time format string.

Definition at line 219 of file Message.cpp.

220 {
221  m_time_format = ( timeFormat.empty() ? DEFAULT_TIME_FORMAT
222  : std::move(timeFormat) );
223 }
std::string m_time_format
Time format string.
Definition: Message.h:99
static constexpr const char * DEFAULT_TIME_FORMAT
The default time format.
Definition: Message.h:159
void Message::setType ( int  msg_type)

Set the message type.

Definition at line 84 of file Message.cpp.

85 {
86  m_type = msg_type;
87 }
int m_type
The type.
Definition: Message.h:102
void Message::setType ( int  msg_type)

Set the message type.

void Message::setWidth ( const std::string &  formatArg) const
private

Set the width of a stream.

Definition at line 370 of file Message.cpp.

371 {
372  // Convert string to int, if string contains digits only...
373  if ( all_digits(formatArg) ) m_width = std::stoi(formatArg);
374  else invalidFormat();
375 }
void invalidFormat() const
Called when an invalid format string is encountered.
Definition: Message.cpp:345
int m_width
The current field width.
Definition: Message.h:111
void Message::setWidth ( const std::string &  formatArg) const
private

Set the width of a stream.

void Message::sizeField ( const std::string &  text) const
private

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

Definition at line 384 of file Message.cpp.

385 {
386  std::string newText;
387  if ( m_width == 0 || m_width == static_cast<int>( text.length() ) ) {
388  newText = text;
389  }
390  else {
391 
392  // Truncate the text if it is too long.
393  if ( m_width < static_cast<int>( text.length() ) ) {
394  newText = text.substr( 0, m_width );
395  for ( int i = 0, j = newText.length()-1; i < 3 && j >= 0; ++i, --j )
396  newText[ j ] = '.';
397  }
398 
399  // Pad the text.
400  else {
401  newText = std::string( m_width, m_fill );
402  if ( m_left )
403  newText.replace( newText.begin(), newText.begin() + text.length(),
404  text.begin(), text.end() );
405  else
406  newText.replace( newText.end() - text.length(), newText.end(),
407  text.begin(), text.end() );
408  }
409  }
410 
411  m_formatted_msg += newText;
412 }
bool m_left
Justification.
Definition: Message.h:114
char m_fill
The current fill character.
Definition: Message.h:108
std::string m_formatted_msg
Formatted message.
Definition: Message.h:105
int m_width
The current field width.
Definition: Message.h:111
list i
Definition: ana.py:128
void Message::sizeField ( const std::string &  text) const
private

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

Friends And Related Function Documentation

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

Insert the message into a stream.

Definition at line 117 of file Message.cpp.

118 {
119  msg.makeFormattedMsg( msg.m_format );
120  stream << msg.m_formatted_msg;
121  return stream;
122 }
std::string m_formatted_msg
Formatted message.
Definition: Message.h:105
std::string m_format
The format string.
Definition: Message.h:96
void makeFormattedMsg(const std::string &format) const
Format the message.
Definition: Message.cpp:231
std::ostream& operator<< ( std::ostream &  stream,
const Message msg 
)
friend

Insert the message into a stream.

Definition at line 117 of file Message.cpp.

118 {
119  msg.makeFormattedMsg( msg.m_format );
120  stream << msg.m_formatted_msg;
121  return stream;
122 }
std::string m_formatted_msg
Formatted message.
Definition: Message.h:105
std::string m_format
The format string.
Definition: Message.h:96
void makeFormattedMsg(const std::string &format) const
Format the message.
Definition: Message.cpp:231
bool operator== ( const Message a,
const Message b 
)
friend

Insert the message into a stream.

Definition at line 143 of file Message.cpp.

144 {
145  return a.m_source == b.m_source &&
146  a.m_type == b.m_type &&
147  a.m_message == b.m_message;
148 }
std::string m_message
The message.
Definition: Message.h:90
std::string m_source
The source.
Definition: Message.h:93
int m_type
The type.
Definition: Message.h:102
bool operator== ( const Message a,
const Message b 
)
friend

Insert the message into a stream.

Definition at line 143 of file Message.cpp.

144 {
145  return a.m_source == b.m_source &&
146  a.m_type == b.m_type &&
147  a.m_message == b.m_message;
148 }
std::string m_message
The message.
Definition: Message.h:90
std::string m_source
The source.
Definition: Message.h:93
int m_type
The type.
Definition: Message.h:102

Member Data Documentation

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

The default message format.

Definition at line 155 of file Message.h.

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

The default time format.

Definition at line 159 of file Message.h.

static const char Message::FILL = 'F'
staticprivate

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 146 of file Message.h.

static const char Message::FORMAT_PREFIX = '%'
staticprivate

Formatting string characters.

The character used to prefix formatting commands.

Definition at line 119 of file Message.h.

static const char Message::JUSTIFY_LEFT = 'L'
staticprivate

The character used to indicate start of left text justification.

Definition at line 122 of file Message.h.

static const char Message::JUSTIFY_RIGHT = 'R'
staticprivate

The character used to indicate start of right text justification.

Definition at line 125 of file Message.h.

char Message::m_fill = ' '
mutableprivate

The current fill character.

Definition at line 108 of file Message.h.

std::string Message::m_format = DEFAULT_FORMAT
mutableprivate

The format string.

Definition at line 96 of file Message.h.

std::string Message::m_formatted_msg
mutableprivate

Formatted message.

Definition at line 105 of file Message.h.

bool Message::m_left = true
mutableprivate

Justification.

Definition at line 114 of file Message.h.

std::string Message::m_message
private

The message.

Definition at line 90 of file Message.h.

std::string Message::m_source = "UNKNOWN"
private

The source.

Definition at line 93 of file Message.h.

std::string Message::m_time_format = DEFAULT_TIME_FORMAT
mutableprivate

Time format string.

Definition at line 99 of file Message.h.

int Message::m_type = 0
private

The type.

Definition at line 102 of file Message.h.

int Message::m_width = 0
mutableprivate

The current field width.

Definition at line 111 of file Message.h.

static const char Message::MESSAGE = 'M'
staticprivate

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

Definition at line 128 of file Message.h.

static const char Message::SOURCE = 'S'
staticprivate

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

Definition at line 141 of file Message.h.

static const char Message::TIME = 't'
staticprivate

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

Definition at line 134 of file Message.h.

static const char Message::TYPE = 'T'
staticprivate

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

Definition at line 131 of file Message.h.

static const char Message::UTIME = 'u'
staticprivate

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

Definition at line 138 of file Message.h.

static const char Message::WIDTH = 'W'
staticprivate

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

Definition at line 151 of file Message.h.


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