Gaudi Framework, version v23r0

Home   Generated: Mon Jan 30 2012
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends

Message Class Reference

The Message class. More...

#include <GaudiKernel/Message.h>

Collaboration diagram for Message:
Collaboration graph
[legend]

List of all members.

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 = 'L'
 The character used to indicate start of left text justification.
static const char JUSTIFY_RIGHT = 'R'
 The character used to indicate start of right text justification.
static const char MESSAGE = 'M'
 The character used to indicate that the message should be printed.
static const char TYPE = 'T'
 The character used to indicate that the message type should be printed.
static const char TIME = 't'
 The character used to indicate that the message timestamp should be printed.
static const char UTIME = 'u'
 The character used to indicate that the message timestamp should be printed in UTC time.
static const char SOURCE = 'S'
 The character used to indicate that the message source should be printed.
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.
static const char WIDTH = 'W'
 The character used to indicate that the previous decimal characters should be taken as the field width.
static const char * DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M"
 The default message format.
static const char * DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f"
 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.

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.

                 :
  m_message( "" ), m_source( "UNKNOWN" ), m_format( DEFAULT_FORMAT ),
  m_time_format(DEFAULT_TIME_FORMAT), m_type( NIL ),
  m_fill( ' ' ), m_width( 0 ), m_left( true )
{
}
Message::Message ( const char *  src,
int  type,
const char *  msg 
)

Constructor.

Definition at line 58 of file Message.cpp.

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

Constructor.

Definition at line 71 of file Message.cpp.

Message::~Message (  ) [inline]

Default destructor.

Definition at line 27 of file Message.h.

{}

Member Function Documentation

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

Decode format.

Definition at line 305 of file Message.cpp.

{
  if ( ! format.empty() ) {
    const char FORMAT_TYPE = format[ format.length() - 1 ];
    const std::string FORMAT_PARAM = format.substr( 0, format.length() - 1 );

    // Now test the format.
    std::string level;
    switch( FORMAT_TYPE ) {
    case FILL:
      if ( FORMAT_PARAM.length() == 1 ) {
        m_fill = FORMAT_PARAM[0];
      }
      else
        invalidFormat();
      break;

    case TIME:
      {
        const std::string& timeStr = formattedTime ( m_time_format ) ;
        sizeField( timeStr );
      }
      break;

    case UTIME:
      {
        const std::string& timeStr = formattedTime ( m_time_format, true ) ;
        sizeField( timeStr );
      }
      break;

    case MESSAGE:
      sizeField( m_message );
      break;

    case SOURCE:
      sizeField( m_source );
      break;

    case TYPE:
      switch ( m_type )    {
#define SET(x)  case x:  level=#x;  break
        SET( NIL );
        SET( VERBOSE );
        SET( DEBUG );
        SET( INFO );
        SET( WARNING );
        SET( ERROR );
        SET( FATAL );
        case ALWAYS:       level="SUCCESS"; break;
        default:
              level = "UNKNOWN";
              break;
#undef SET
      }
      sizeField( level );
      break;

    case FORMAT_PREFIX: m_formatted_msg += FORMAT_PREFIX; break;
    case JUSTIFY_RIGHT: m_left = false; break;
    case JUSTIFY_LEFT: m_left = true; break;
    case WIDTH: setWidth( FORMAT_PARAM ); break;
    default: invalidFormat(); break;
    }
  }
  else
    invalidFormat();
}
const std::string Message::getDefaultFormat (  ) [static]

Get the default format string.

Definition at line 200 of file Message.cpp.

{
  return DEFAULT_FORMAT;
}
const std::string Message::getDefaultTimeFormat (  ) [static]

Get the default time format string.

Definition at line 238 of file Message.cpp.

{
  return DEFAULT_TIME_FORMAT ;
}
const std::string & Message::getFormat (  ) const

Get the format string.

Definition at line 189 of file Message.cpp.

{
  return m_format;
}
const std::string & Message::getMessage (  ) const

Get the message string.

Definition at line 84 of file Message.cpp.

{
  return m_message;
}
const std::string & Message::getSource (  ) const

Get the message source.

Definition at line 128 of file Message.cpp.

{
  return m_source;
}
const std::string & Message::getTimeFormat (  ) const

Get the time format string.

Definition at line 227 of file Message.cpp.

{
  return m_time_format;
}
int Message::getType (  ) const

Get the message type.

Definition at line 106 of file Message.cpp.

{
  return m_type;
}
void Message::invalidFormat (  ) const [protected]

Called when an invalid format string is encountered.

Definition at line 381 of file Message.cpp.

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

Format the message.

Definition at line 265 of file Message.cpp.

{
  m_formatted_msg = "";
  std::string::const_iterator i = format.begin();
  while( i != format.end() ) {

    // Output format string until format statement found.
    while(  i != format.end() && *i != FORMAT_PREFIX )
      m_formatted_msg += *i++;

    // Test for end of format string.
    if ( i == format.end() ) break;
    i++;

    // Find type of formatting.
    std::string this_format = "";
    while( i != format.end() && *i != FORMAT_PREFIX &&
           *i != MESSAGE && *i != TYPE && *i != SOURCE &&
           *i != FILL && *i != WIDTH && *i != TIME && *i != UTIME &&
           *i != JUSTIFY_LEFT && *i != JUSTIFY_RIGHT ) {
      this_format += *i++;
    }

    // Reached end of string with improper format.
    if ( i == format.end() ) {
      invalidFormat();
      break;
    }

    this_format += *i++;
    decodeFormat( this_format );
  }
}
bool Message::operator< ( const Message test )

Needed to build maps.

Definition at line 163 of file Message.cpp.

{
  return m_type   < b.m_type ||
         m_source < b.m_source ||
         m_message < b.m_message;
}
void Message::setFormat ( const std::string msg ) const

Set the format string.

Definition at line 213 of file Message.cpp.

{
  if ( format.empty() )
    m_format = DEFAULT_FORMAT;
  else
    m_format = format;
}
void Message::setMessage ( const std::string msg )

Set the message string.

Definition at line 95 of file Message.cpp.

{
  m_message = msg;
}
void Message::setSource ( const std::string src )

Set the message source.

Definition at line 139 of file Message.cpp.

{
  m_source = src;
}
void Message::setTimeFormat ( const std::string timeFormat ) const

Set the time format string.

Definition at line 251 of file Message.cpp.

{
  if ( timeFormat.empty() )
    m_time_format = DEFAULT_TIME_FORMAT;
  else
    m_time_format = timeFormat;
}
void Message::setType ( int  msg_type )

Set the message type.

Definition at line 117 of file Message.cpp.

{
  m_type = msg_type;
}
void Message::setWidth ( const std::string formatArg ) const [protected]

Set the width of a stream.

Definition at line 393 of file Message.cpp.

{
  // Check that the parameters are only digits.
  bool only_digits = true;
  for( std::string::const_iterator i = formatArg.begin();
       i != formatArg.end(); i++ ) {

    if ( ! isdigit( *i ) ) {
      only_digits = false;
      invalidFormat();
      break;
    }
  }

  // Convert string to int.
  if ( only_digits ) {
#ifdef __GNUG__
    m_width = atoi( formatArg.c_str() );
#else
    m_width = atoi( formatArg.data() );
#endif
  }
}
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.

{
  std::string newText;
  if ( m_width == 0 || m_width == static_cast<int>( text.length() ) ) {
    newText = text;
  }
  else {

    // Truncate the text if it is too long.
    if ( m_width < static_cast<int>( text.length() ) ) {
      newText = text.substr( 0, m_width );
      for ( int i = 0, j = newText.length()-1; i < 3 && j >= 0; i++, j-- )
        newText[ j ] = '.';
    }

    // Pad the text.
    else {
      newText = std::string( m_width, m_fill );
      if ( m_left )
        newText.replace( newText.begin(), newText.begin() + text.length(),
                         text.begin(), text.end() );
      else
        newText.replace( newText.end() - text.length(), newText.end(),
                         text.begin(), text.end() );
    }
  }

  m_formatted_msg += newText;
}

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.

{
  msg.makeFormattedMsg( msg.m_format );
  stream << msg.m_formatted_msg;
  return stream;
}
bool operator== ( const Message a,
const Message b 
) [friend]

Insert the message into a stream.

Definition at line 176 of file Message.cpp.

{
  return a.m_source == b.m_source &&
    a.m_type == b.m_type &&
    a.m_message == b.m_message;
}

Member Data Documentation

const char * Message::DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M" [static, protected]

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" [static, protected]

The default time format.

Definition at line 156 of file Message.h.

const char Message::FILL = 'F' [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.

const char Message::FORMAT_PREFIX = '%' [static, protected]

The character used to prefix formatting commands.

Definition at line 118 of file Message.h.

const char Message::JUSTIFY_LEFT = 'L' [static, protected]

The character used to indicate start of left text justification.

Definition at line 121 of file Message.h.

const char Message::JUSTIFY_RIGHT = 'R' [static, protected]

The character used to indicate start of right text justification.

Definition at line 124 of file Message.h.

char Message::m_fill [mutable, protected]

The current fill character.

Definition at line 109 of file Message.h.

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

The format string.

Definition at line 97 of file Message.h.

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

Formatted message.

Definition at line 106 of file Message.h.

bool Message::m_left [mutable, protected]

Justification.

Definition at line 115 of file Message.h.

The message.

Definition at line 91 of file Message.h.

The source.

Definition at line 94 of file Message.h.

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

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 [mutable, protected]

The current field width.

Definition at line 112 of file Message.h.

const char Message::MESSAGE = 'M' [static, protected]

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

Definition at line 127 of file Message.h.

const char Message::SOURCE = 'S' [static, protected]

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' [static, protected]

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' [static, protected]

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' [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.

const char Message::WIDTH = 'W' [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.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Jan 30 2012 13:53:18 for Gaudi Framework, version v23r0 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004