The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
Message Class Referencefinal

The Message class. More...

#include <GaudiKernel/Message.h>

Collaboration diagram for Message:

Public Member Functions

 Message ()=default
 Default constructor.
 
 Message (const char *src, int type, const char *msg)
 Constructor.
 
 Message (std::string src, int type, std::string msg)
 Constructor.
 
 ~Message ()=default
 Default destructor.
 
const std::string & getMessage () const
 Get the message string.
 
void setMessage (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::string & getSource () const
 Get the message source.
 
void setSource (std::string_view src)
 Set the message source.
 

Private Attributes

Event identifiers
EventContext::ContextID_t m_ecSlot { EventContext::INVALID_CONTEXT_ID }
 Event slot.
 
EventContext::ContextEvt_t m_ecEvt { EventContext::INVALID_CONTEXT_EVT }
 Event number.
 
EventIDBase m_ecEvtId
 Full event ID.
 
pthread_t m_ecThrd {}
 Thread ID.
 

Static Private Attributes

Formatting characters
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 COMP = 'C'
 The character used to indicate that the message source should be printed, focus on the component.
 
static const char SLOT = 's'
 The character used to indicate that the slot number should be printed.
 
static const char EVTNUM = 'e'
 The character used to indicate that the event number should be printed.
 
static const char THREAD = 'X'
 The character used to indicate that the thread ID should be printed.
 
static const char EVENTID = 'E'
 The character used to indicate that the full event ID should be printed.
 
static const char FILL = 'F'
 The character used to indicate that the previous character is used to pad 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 constexpr const char * DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M"
 The default message format.
 
static constexpr const char * DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f"
 The default time format (accepts strftime formatters plus %f for milliseconds).
 

Event identifiers of message

std::string m_message
 The message text.
 
std::string m_source { "UNKNOWN" }
 The message source.
 
int m_type { 0 }
 The message type/level.
 
std::string m_format { DEFAULT_FORMAT }
 The format string.
 
std::string m_time_format { DEFAULT_TIME_FORMAT }
 Time format string.
 
std::string m_formatted_msg
 Formatted message.
 
char m_fill { ' ' }
 Current fill character.
 
int m_width { 0 }
 Current field width.
 
bool m_left { true }
 The message alignment.
 
EventContext::ContextID_t getEventSlot () const
 
EventContext::ContextEvt_t getEventNumber () const
 
const EventIDBasegetEventID () const
 
const std::string & getFormat () const
 Get the format string.
 
void setFormat (std::string msg) const
 Set the format string.
 
const std::string & getTimeFormat () const
 Get the time format string.
 
void setTimeFormat (std::string timeFormat) const
 Set the time format string.
 
static const std::string getDefaultFormat ()
 Get the default format string.
 
static const std::string getDefaultTimeFormat ()
 Get the default time format string.
 
bool operator< (const Message &lhs, const Message &rhs)
 Needed to build maps.
 
std::ostream & operator<< (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.
 
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, bool middle=false) const
 Truncate or pad the output string to the field width. If middle is true, cut the central part, otherwise the end.
 
void setWidth (const std::string &formatArg) const
 Set the width of 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 25 of file Message.h.

Constructor & Destructor Documentation

◆ Message() [1/3]

Message::Message ( )
default

Default constructor.

◆ Message() [2/3]

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

Constructor.

Definition at line 39 of file Message.cpp.

40 : Message( std::string( src ), type, std::string( msg ) ) {}
Message()=default
Default constructor.

◆ Message() [3/3]

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

Constructor.

Definition at line 48 of file Message.cpp.

49 : m_message( std::move( msg ) ), m_source( std::move( src ) ), m_type( type ) {
50
51 const EventContext& ctx = Gaudi::Hive::currentContext();
52 m_ecSlot = ctx.slot();
53 m_ecEvt = ctx.evt();
54 m_ecEvtId = ctx.eventID();
55 m_ecThrd = pthread_self();
56}
std::string m_message
The message text.
Definition Message.h:107
EventIDBase m_ecEvtId
Full event ID.
Definition Message.h:121
EventContext::ContextID_t m_ecSlot
Event slot.
Definition Message.h:119
int m_type
The message type/level.
Definition Message.h:109
std::string m_source
The message source.
Definition Message.h:108
pthread_t m_ecThrd
Thread ID.
Definition Message.h:122
EventContext::ContextEvt_t m_ecEvt
Event number.
Definition Message.h:120
GAUDI_API const EventContext & currentContext()

◆ ~Message()

Message::~Message ( )
default

Default destructor.

Member Function Documentation

◆ decodeFormat()

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

Decode format.

Definition at line 159 of file Message.cpp.

159 {
160 if ( !format.empty() ) {
161 const char FORMAT_TYPE = format[format.length() - 1];
162 const std::string FORMAT_PARAM = format.substr( 0, format.length() - 1 );
163
164 // Now test the format.
165 std::string level;
166 switch ( FORMAT_TYPE ) {
167 case FILL:
168 if ( FORMAT_PARAM.length() == 1 ) {
169 m_fill = FORMAT_PARAM[0];
170 } else
172 break;
173
174 case TIME: {
175 sizeField( formattedTime( m_time_format ) );
176 } break;
177
178 case UTIME: {
179 sizeField( formattedTime( m_time_format, true ) );
180 } break;
181
182 case THREAD: {
183 std::ostringstream ost;
184 // ost << "0x" << std::hex << pthread_self();
185 ost << "0x" << std::hex << m_ecThrd;
186 const std::string& thrStr( ost.str() );
187 sizeField( thrStr );
188 } break;
189
190 case SLOT: {
191 std::ostringstream ost;
193 sizeField( ost.str() );
194 } break;
195
196 case EVTNUM: {
197 std::ostringstream ost;
199 sizeField( ost.str() );
200 } break;
201
202 case EVENTID: {
203 std::ostringstream ost;
204 if ( m_ecEvtId.isValid() ) { ost << m_ecEvtId; }
205 sizeField( ost.str() );
206 } break;
207
208 case MESSAGE:
210 break;
211
212 case SOURCE:
214 break;
215
216 case COMP:
217 sizeField( m_source, true );
218 break;
219
220 case TYPE:
221 switch ( m_type ) {
222#define SET( x ) \
223 case x: \
224 level = #x; \
225 break
226 SET( NIL );
227 SET( VERBOSE );
228 SET( DEBUG );
229 SET( INFO );
230 SET( WARNING );
231 SET( ERROR );
232 SET( FATAL );
233 case ALWAYS:
234 level = "SUCCESS";
235 break;
236 default:
237 level = "UNKNOWN";
238 break;
239#undef SET
240 }
241 sizeField( level );
242 break;
243
244 case FORMAT_PREFIX:
246 break;
247 case JUSTIFY_RIGHT:
248 m_left = false;
249 break;
250 case JUSTIFY_LEFT:
251 m_left = true;
252 break;
253 case WIDTH:
254 setWidth( FORMAT_PARAM );
255 break;
256 default:
258 break;
259 }
260 } else
262}
#define SET(x)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
static constexpr ContextEvt_t INVALID_CONTEXT_EVT
static constexpr ContextID_t INVALID_CONTEXT_ID
void sizeField(const std::string &text, bool middle=false) const
Truncate or pad the output string to the field width. If middle is true, cut the central part,...
Definition Message.cpp:305
static const char SLOT
The character used to indicate that the slot number should be printed.
Definition Message.h:155
std::string m_formatted_msg
Formatted message.
Definition Message.h:112
bool m_left
The message alignment.
Definition Message.h:115
void invalidFormat() const
Called when an invalid format string is encountered.
Definition Message.cpp:271
static const char THREAD
The character used to indicate that the thread ID should be printed.
Definition Message.h:161
static const char UTIME
The character used to indicate that the message timestamp should be printed in UTC time.
Definition Message.h:146
static const char EVTNUM
The character used to indicate that the event number should be printed.
Definition Message.h:158
static const char SOURCE
The character used to indicate that the message source should be printed.
Definition Message.h:149
static const char COMP
The character used to indicate that the message source should be printed, focus on the component.
Definition Message.h:152
static const char TIME
The character used to indicate that the message timestamp should be printed.
Definition Message.h:143
static const char JUSTIFY_LEFT
The character used to indicate start of left text justification.
Definition Message.h:131
static const char EVENTID
The character used to indicate that the full event ID should be printed.
Definition Message.h:164
static const char FORMAT_PREFIX
The character used to prefix formatting commands.
Definition Message.h: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:170
static const char TYPE
The character used to indicate that the message type should be printed.
Definition Message.h:140
std::string m_time_format
Time format string.
Definition Message.h:111
static const char JUSTIFY_RIGHT
The character used to indicate start of right text justification.
Definition Message.h:134
char m_fill
Current fill character.
Definition Message.h:113
static const char FILL
The character used to indicate that the previous character is used to pad fields if the text is not l...
Definition Message.h:167
void setWidth(const std::string &formatArg) const
Set the width of a stream.
Definition Message.cpp:290
static const char MESSAGE
The character used to indicate that the message should be printed.
Definition Message.h:137
@ NIL
Definition IMessageSvc.h:22
@ WARNING
Definition IMessageSvc.h:22
@ FATAL
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
@ ALWAYS
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22

◆ getDefaultFormat()

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

Get the default format string.

Definition at line 68 of file Message.h.

68{ return DEFAULT_FORMAT; }
static constexpr const char * DEFAULT_FORMAT
The default message format.
Definition Message.h:174

◆ getDefaultTimeFormat()

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

Get the default time format string.

Definition at line 77 of file Message.h.

77{ return DEFAULT_TIME_FORMAT; }
static constexpr const char * DEFAULT_TIME_FORMAT
The default time format (accepts strftime formatters plus %f for milliseconds).
Definition Message.h:177

◆ getEventID()

const EventIDBase & Message::getEventID ( ) const
inline

Definition at line 61 of file Message.h.

61{ return m_ecEvtId; }

◆ getEventNumber()

EventContext::ContextEvt_t Message::getEventNumber ( ) const
inline

Definition at line 60 of file Message.h.

60{ return m_ecEvt; }

◆ getEventSlot()

EventContext::ContextID_t Message::getEventSlot ( ) const
inline

Definition at line 59 of file Message.h.

59{ return m_ecSlot; }

◆ getFormat()

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

Get the format string.

Definition at line 65 of file Message.h.

65{ return m_format; }
std::string m_format
The format string.
Definition Message.h:110

◆ getMessage()

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

Get the message string.

Definition at line 40 of file Message.h.

40{ return m_message; }

◆ getSource()

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

Get the message source.

Definition at line 52 of file Message.h.

52{ return m_source; }

◆ getTimeFormat()

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

Get the time format string.

Definition at line 74 of file Message.h.

74{ return m_time_format; }

◆ getType()

int Message::getType ( ) const
inline

Get the message type.

Definition at line 46 of file Message.h.

46{ return m_type; }

◆ invalidFormat()

void Message::invalidFormat ( ) const
private

Called when an invalid format string is encountered.

Definition at line 271 of file Message.cpp.

void makeFormattedMsg(const std::string &format) const
Format the message.
Definition Message.cpp:122

◆ makeFormattedMsg()

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

Format the message.

Definition at line 122 of file Message.cpp.

122 {
123 m_formatted_msg.clear();
124 auto i = format.begin();
125 while ( i != format.end() ) {
126
127 // Output format string until format statement found.
128 while ( i != format.end() && *i != FORMAT_PREFIX ) m_formatted_msg += *i++;
129
130 // Test for end of format string.
131 if ( i == format.end() ) break;
132 i++;
133
134 // Find type of formatting.
135 std::string this_format;
136 while ( i != format.end() && *i != FORMAT_PREFIX && *i != MESSAGE && *i != TYPE && *i != SOURCE && *i != COMP &&
137 *i != FILL && *i != WIDTH && *i != TIME && *i != UTIME && *i != SLOT && *i != EVTNUM && *i != THREAD &&
138 *i != EVENTID && *i != JUSTIFY_LEFT && *i != JUSTIFY_RIGHT ) {
139 this_format += *i++;
140 }
141
142 // Reached end of string with improper format.
143 if ( i == format.end() ) {
145 break;
146 }
147
148 this_format += *i++;
149 decodeFormat( this_format );
150 }
151}
void decodeFormat(const std::string &format) const
Decode format.
Definition Message.cpp:159

◆ setFormat()

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

Set the format string.

Definition at line 97 of file Message.cpp.

97 {
98 if ( !format.empty() ) {
99 m_format = std::move( format );
100 } else {
102 }
103}

◆ setMessage()

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

Set the message string.

Definition at line 43 of file Message.h.

43{ m_message = std::move( msg ); }

◆ setSource()

void Message::setSource ( std::string_view src)
inline

Set the message source.

Definition at line 55 of file Message.h.

55{ m_source = src; }

◆ setTimeFormat()

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

Set the time format string.

Definition at line 112 of file Message.cpp.

112 {
113 m_time_format = ( timeFormat.empty() ? DEFAULT_TIME_FORMAT : std::move( timeFormat ) );
114}

◆ setType()

void Message::setType ( int msg_type)
inline

Set the message type.

Definition at line 49 of file Message.h.

49{ m_type = msg_type; }

◆ setWidth()

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

Set the width of a stream.

Definition at line 290 of file Message.cpp.

290 {
291 // Convert string to int, if string contains digits only...
292 if ( all_digits( formatArg ) )
293 m_width = std::stoi( formatArg );
294 else
296}
int m_width
Current field width.
Definition Message.h:114

◆ sizeField()

void Message::sizeField ( const std::string & text,
bool middle = false ) const
private

Truncate or pad the output string to the field width. If middle is true, cut the central part, otherwise the end.

Definition at line 305 of file Message.cpp.

305 {
306 std::string newText;
307 if ( m_width == 0 || m_width == static_cast<int>( text.length() ) ) {
308 newText = text;
309 } else {
310 const size_t width = static_cast<size_t>( m_width );
311 // Truncate the text if it is too long.
312 if ( width < text.length() ) {
313 if ( middle && width > 4 ) { // truncate text in the middle
314 size_t iTrunc;
315 // If the text is a component-name-chain, try showing the last component
316 const size_t iDot = text.rfind( '.' );
317 if ( iDot != std::string::npos && width > text.length() - iDot + 3 ) {
318 iTrunc = iDot + 1;
319 } else {
320 // No dot or text after dot too long
321 iTrunc = text.length() - width / 2;
322 }
323 const size_t taillength = text.length() - iTrunc;
324 const size_t frontlength = width - taillength - 3;
325 newText.reserve( width );
326 newText.append( text, 0, frontlength );
327 newText.append( 3, '.' );
328 newText.append( text, iTrunc );
329 } // else if middle
330 else { // truncate text at the end
331 newText = text.substr( 0, m_width );
332 for ( int i = 0, j = newText.length() - 1; i < 3 && j >= 0; ++i, --j ) newText[j] = '.';
333 }
334 }
335 // Pad the text.
336 else {
337 newText = std::string( m_width, m_fill );
338 if ( m_left )
339 newText.replace( newText.begin(), newText.begin() + text.length(), text.begin(), text.end() );
340 else
341 newText.replace( newText.end() - text.length(), newText.end(), text.begin(), text.end() );
342 }
343 }
344
345 m_formatted_msg += newText;
346}

Friends And Related Symbol Documentation

◆ operator<

bool operator< ( const Message & lhs,
const Message & rhs )
friend

Needed to build maps.

Definition at line 76 of file Message.cpp.

76 {
77 return lhs.m_type < rhs.m_type || lhs.m_source < rhs.m_source || lhs.m_message < rhs.m_message;
78}

◆ operator<<

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

Insert the message into a stream.

Definition at line 64 of file Message.cpp.

64 {
65 msg.makeFormattedMsg( msg.m_format );
66 stream << msg.m_formatted_msg;
67 return stream;
68}
stream
Definition Write.py:32

◆ operator==

bool operator== ( const Message & a,
const Message & b )
friend

Insert the message into a stream.

Definition at line 86 of file Message.cpp.

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

Member Data Documentation

◆ COMP

const char Message::COMP = 'C'
staticprivate

The character used to indicate that the message source should be printed, focus on the component.

Definition at line 152 of file Message.h.

◆ DEFAULT_FORMAT

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

The default message format.

Definition at line 174 of file Message.h.

◆ DEFAULT_TIME_FORMAT

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

The default time format (accepts strftime formatters plus %f for milliseconds).

Definition at line 177 of file Message.h.

◆ EVENTID

const char Message::EVENTID = 'E'
staticprivate

The character used to indicate that the full event ID should be printed.

Definition at line 164 of file Message.h.

◆ EVTNUM

const char Message::EVTNUM = 'e'
staticprivate

The character used to indicate that the event number should be printed.

Definition at line 158 of file Message.h.

◆ FILL

const char Message::FILL = 'F'
staticprivate

The character used to indicate that the previous character is used to pad fields if the text is not long enough.

Definition at line 167 of file Message.h.

◆ FORMAT_PREFIX

const char Message::FORMAT_PREFIX = '%'
staticprivate

The character used to prefix formatting commands.

Definition at line 128 of file Message.h.

◆ JUSTIFY_LEFT

const char Message::JUSTIFY_LEFT = 'L'
staticprivate

The character used to indicate start of left text justification.

Definition at line 131 of file Message.h.

◆ JUSTIFY_RIGHT

const char Message::JUSTIFY_RIGHT = 'R'
staticprivate

The character used to indicate start of right text justification.

Definition at line 134 of file Message.h.

◆ m_ecEvt

Event number.

Definition at line 120 of file Message.h.

◆ m_ecEvtId

EventIDBase Message::m_ecEvtId
private

Full event ID.

Definition at line 121 of file Message.h.

◆ m_ecSlot

Event slot.

Definition at line 119 of file Message.h.

◆ m_ecThrd

pthread_t Message::m_ecThrd {}
private

Thread ID.

Definition at line 122 of file Message.h.

122{};

◆ m_fill

char Message::m_fill { ' ' }
mutableprivate

Current fill character.

Definition at line 113 of file Message.h.

113{ ' ' };

◆ m_format

std::string Message::m_format { DEFAULT_FORMAT }
mutableprivate

The format string.

Definition at line 110 of file Message.h.

110{ DEFAULT_FORMAT };

◆ m_formatted_msg

std::string Message::m_formatted_msg
mutableprivate

Formatted message.

Definition at line 112 of file Message.h.

◆ m_left

bool Message::m_left { true }
mutableprivate

The message alignment.

Definition at line 115 of file Message.h.

115{ true };

◆ m_message

std::string Message::m_message
private

The message text.

Definition at line 107 of file Message.h.

◆ m_source

std::string Message::m_source { "UNKNOWN" }
private

The message source.

Definition at line 108 of file Message.h.

108{ "UNKNOWN" };

◆ m_time_format

std::string Message::m_time_format { DEFAULT_TIME_FORMAT }
mutableprivate

Time format string.

Definition at line 111 of file Message.h.

◆ m_type

int Message::m_type { 0 }
private

The message type/level.

Definition at line 109 of file Message.h.

109{ 0 };

◆ m_width

int Message::m_width { 0 }
mutableprivate

Current field width.

Definition at line 114 of file Message.h.

114{ 0 };

◆ MESSAGE

const char Message::MESSAGE = 'M'
staticprivate

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

Definition at line 137 of file Message.h.

◆ SLOT

const char Message::SLOT = 's'
staticprivate

The character used to indicate that the slot number should be printed.

Definition at line 155 of file Message.h.

◆ SOURCE

const char Message::SOURCE = 'S'
staticprivate

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

Definition at line 149 of file Message.h.

◆ THREAD

const char Message::THREAD = 'X'
staticprivate

The character used to indicate that the thread ID should be printed.

Definition at line 161 of file Message.h.

◆ TIME

const char Message::TIME = 't'
staticprivate

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

Definition at line 143 of file Message.h.

◆ TYPE

const char Message::TYPE = 'T'
staticprivate

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

Definition at line 140 of file Message.h.

◆ UTIME

const char Message::UTIME = 'u'
staticprivate

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

Definition at line 146 of file Message.h.

◆ WIDTH

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


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