The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
MsgStream Class Reference

Definition of the MsgStream class used to transmit messages. More...

#include <GaudiKernel/MsgStream.h>

Collaboration diagram for MsgStream:

Public Member Functions

GAUDI_API MsgStream (IMessageSvc *svc, int buffer_length=128)
 Standard constructor: Connect to message service for output.
 
GAUDI_API MsgStream (IMessageSvc *svc, std::string source, int buffer_length=128)
 Standard constructor: Connect to message service for output.
 
 MsgStream (const MsgStream &msg)
 Copy constructor.
 
virtual GAUDI_API ~MsgStream ()=default
 Standard destructor.
 
MsgStreamreport (int lvl)
 Initialize report of new message: activate if print level is sufficient.
 
virtual GAUDI_API MsgStreamdoOutput ()
 Output method.
 
const std::string & buffer () const
 Access string buffer.
 
std::ostringstream & stream ()
 Access string MsgStream.
 
void setMsgSvc (IMessageSvc *svc)
 Update IMessageSvc pointer.
 
void setLevel (int level)
 Update outputlevel.
 
MSG::Level level () const
 Retrieve output level.
 
MSG::Level currentLevel () const
 Retrieve current stream output level.
 
void activate ()
 Activate MsgStream.
 
void deactivate ()
 Deactivate MsgStream.
 
bool isActive () const
 Accessor: is MsgStream active.
 
MsgStreamflush ()
 
MsgStreamwrite (const char *buff, int len)
 
MsgStreamoperator<< (MsgStream &(*_f)(MsgStream &))
 Accept MsgStream modifiers.
 
MsgStreamoperator<< (std::ostream &(*_f)(std::ostream &))
 Accept oMsgStream modifiers.
 
MsgStreamoperator<< (std::ios &(*_f)(std::ios &))
 Accept ios modifiers.
 
MsgStreamoperator<< (MSG::Level level)
 Accept MsgStream activation using MsgStreamer operator.
 
MsgStreamoperator<< (long long arg)
 
MsgStreamoperator<< (std::ios_base &(*_f)(std::ios_base &))
 Accept ios base class modifiers.
 
std::streamsize width () const
 IOS emulation.
 
std::streamsize width (std::streamsize v)
 
std::streamsize precision () const
 
std::streamsize precision (int v)
 
long flags () const
 
long flags (FLAG_TYPE v)
 
long setf (FLAG_TYPE v)
 
char fill () const
 
char fill (char v)
 
int rdstate () const
 
bool good () const
 
bool eof () const
 
bool bad () const
 
int setf (FLAG_TYPE _f, FLAG_TYPE _m)
 
void unsetf (FLAG_TYPE _l)
 
void clear (STATE_TYPE _i=std::ios_base::failbit)
 
GAUDI_API void setColor (MSG::Color col)
 Set the text color.
 
GAUDI_API void setColor (MSG::Color fg, MSG::Color bg)
 Set the foreground and background colors.
 
GAUDI_API void resetColor ()
 Reset the colors to defaults.
 

Static Public Member Functions

static GAUDI_API bool enableCountInactive (bool value=true)
 Enable/disable the count of inactive messages.
 
static GAUDI_API bool countInactive ()
 Returns the state of the counting of inactive messages (enabled/disabled).
 

Protected Attributes

IMessageSvcm_service
 Pointer to message service if buffer has send.
 
std::string m_buffer
 Use standard string for information buffering.
 
std::string m_source
 Use std::string for source information to be passed to the message service.
 
std::ostringstream m_stream
 String MsgStream associated to buffer.
 
bool m_active = false
 Flag set to true if formatting engine is active.
 
MSG::Level m_level
 Debug level of the message service.
 
MSG::Level m_currLevel
 Current debug level.
 
bool m_useColors
 use colors
 
IInactiveMessageCounterm_inactCounter = nullptr
 Pointer to service counting messages prepared but not printed because of wrong level.
 

Static Protected Attributes

static bool m_countInactive = false
 Flag to state if the inactive messages has to be counted.
 

Private Types

typedef std::ios_base::fmtflags FLAG_TYPE
 Error return code in case ios modification is requested for inactive streams.
 
typedef std::ios_base::iostate STATE_TYPE
 

Detailed Description

Definition of the MsgStream class used to transmit messages.

This class is intended to ease the use of error logging to the message service

Author
M.Frank
Sebastien Ponce

Definition at line 29 of file MsgStream.h.

Member Typedef Documentation

◆ FLAG_TYPE

typedef std::ios_base::fmtflags MsgStream::FLAG_TYPE
private

Error return code in case ios modification is requested for inactive streams.

Definition at line 34 of file MsgStream.h.

◆ STATE_TYPE

typedef std::ios_base::iostate MsgStream::STATE_TYPE
private

Definition at line 35 of file MsgStream.h.

Constructor & Destructor Documentation

◆ MsgStream() [1/3]

MsgStream::MsgStream ( IMessageSvc * svc,
int buffer_length = 128 )

Standard constructor: Connect to message service for output.

Definition at line 29 of file MsgStream.cpp.

29 : m_service( svc ) {
30 setLevel( svc ? svc->outputLevel() : MSG::INFO );
32 m_useColors = ( svc ? svc->useColor() : false );
33#ifndef NDEBUG
35#endif
36}
virtual int outputLevel() const =0
Retrieve the current output level threshold.
virtual bool useColor() const =0
Show whether colors are used.
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition MsgStream.h:56
bool m_useColors
use colors
Definition MsgStream.h:53
MSG::Level m_level
Debug level of the message service.
Definition MsgStream.h:49
MSG::Level m_currLevel
Current debug level.
Definition MsgStream.h:51
void setLevel(int level)
Update outputlevel.
Definition MsgStream.h:103
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition MsgStream.h:39
TARGET * Cast(IInterface *i)
Definition IInterface.h:333
@ INFO
Definition IMessageSvc.h:22

◆ MsgStream() [2/3]

MsgStream::MsgStream ( IMessageSvc * svc,
std::string source,
int buffer_length = 128 )

Standard constructor: Connect to message service for output.

Definition at line 38 of file MsgStream.cpp.

38 : m_service( svc ), m_source( std::move( source ) ) {
39 setLevel( svc ? svc->outputLevel( m_source ) : MSG::INFO );
41 m_useColors = ( svc && svc->useColor() );
42#ifndef NDEBUG
44#endif
45}
std::string m_source
Use std::string for source information to be passed to the message service.
Definition MsgStream.h:43

◆ MsgStream() [3/3]

MsgStream::MsgStream ( const MsgStream & msg)
inline

Copy constructor.

Definition at line 66 of file MsgStream.h.

67 : m_service( msg.m_service )
68 , m_active( msg.m_active )
69 , m_level( msg.m_level )
70 , m_currLevel( msg.m_currLevel )
71 , m_useColors( msg.m_useColors )
72 , m_inactCounter( msg.m_inactCounter ) {
73 try { // ignore exception if we cannot copy the string
74 m_source = msg.m_source;
75 } catch ( ... ) {}
76 }
bool m_active
Flag set to true if formatting engine is active.
Definition MsgStream.h:47

◆ ~MsgStream()

virtual GAUDI_API MsgStream::~MsgStream ( )
virtualdefault

Standard destructor.

Member Function Documentation

◆ activate()

void MsgStream::activate ( )
inline

Activate MsgStream.

Definition at line 112 of file MsgStream.h.

112{ m_active = true; }

◆ bad()

bool MsgStream::bad ( ) const
inline

Definition at line 172 of file MsgStream.h.

172{ return isActive() && m_stream.bad(); }
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition MsgStream.h:45
bool isActive() const
Accessor: is MsgStream active.
Definition MsgStream.h:116

◆ buffer()

const std::string & MsgStream::buffer ( ) const
inline

Access string buffer.

Definition at line 97 of file MsgStream.h.

97{ return m_buffer; }
std::string m_buffer
Use standard string for information buffering.
Definition MsgStream.h:41

◆ clear()

void MsgStream::clear ( STATE_TYPE _i = std::ios_base::failbit)
inline

Definition at line 177 of file MsgStream.h.

177 {
178 if ( isActive() ) m_stream.clear( _i );
179 }

◆ countInactive()

bool MsgStream::countInactive ( )
static

Returns the state of the counting of inactive messages (enabled/disabled).

Definition at line 27 of file MsgStream.cpp.

27{ return m_countInactive; }
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition MsgStream.h:58

◆ currentLevel()

MSG::Level MsgStream::currentLevel ( ) const
inline

Retrieve current stream output level.

Definition at line 110 of file MsgStream.h.

110{ return m_currLevel; }

◆ deactivate()

void MsgStream::deactivate ( )
inline

Deactivate MsgStream.

Definition at line 114 of file MsgStream.h.

114{ m_active = false; }

◆ doOutput()

MsgStream & MsgStream::doOutput ( )
virtual

Output method.

Definition at line 47 of file MsgStream.cpp.

47 {
48 try {
49 // This piece of code may throw and we cannot afford it when we print a message
50 // in the middle of a catch block.
51 if ( isActive() ) {
52 const Message msg( m_source, m_currLevel, m_stream.str() );
53 if ( m_service ) {
54 m_service->reportMessage( msg, m_currLevel );
55 } else {
56 std::cout << msg << std::endl;
57 }
58 }
59 m_stream.str( "" );
60 } catch ( ... ) {}
61 return *this;
62}

◆ enableCountInactive()

bool MsgStream::enableCountInactive ( bool value = true)
static

Enable/disable the count of inactive messages.

Returns the previous state.

Definition at line 21 of file MsgStream.cpp.

21 {
22 bool old = m_countInactive;
23 m_countInactive = value;
24 return old;
25}

◆ eof()

bool MsgStream::eof ( ) const
inline

Definition at line 171 of file MsgStream.h.

171{ return isActive() && m_stream.eof(); }

◆ fill() [1/2]

char MsgStream::fill ( ) const
inline

Definition at line 167 of file MsgStream.h.

167{ return isActive() ? m_stream.fill() : (char)-1; }

◆ fill() [2/2]

char MsgStream::fill ( char v)
inline

Definition at line 168 of file MsgStream.h.

168{ return isActive() ? m_stream.fill( v ) : (char)-1; }

◆ flags() [1/2]

long MsgStream::flags ( ) const
inline

Definition at line 164 of file MsgStream.h.

164{ return isActive() ? m_stream.flags() : 0; }

◆ flags() [2/2]

long MsgStream::flags ( FLAG_TYPE v)
inline

Definition at line 165 of file MsgStream.h.

165{ return isActive() ? m_stream.flags( v ) : 0; }

◆ flush()

MsgStream & MsgStream::flush ( )
inline

Definition at line 118 of file MsgStream.h.

118 {
119 if ( isActive() ) m_stream.flush();
120 return *this;
121 }

◆ good()

bool MsgStream::good ( ) const
inline

Definition at line 170 of file MsgStream.h.

170{ return isActive() && m_stream.good(); }

◆ isActive()

bool MsgStream::isActive ( ) const
inline

Accessor: is MsgStream active.

Definition at line 116 of file MsgStream.h.

116{ return m_active; }

◆ level()

MSG::Level MsgStream::level ( ) const
inline

Retrieve output level.

Definition at line 108 of file MsgStream.h.

108{ return m_level; }

◆ operator<<() [1/6]

MsgStream & MsgStream::operator<< ( long long arg)
inline

Definition at line 144 of file MsgStream.h.

144 {
145 try {
146 // this may throw, and we cannot afford it if the stream is used in a catch block
147 if ( isActive() ) { m_stream << arg; }
148 } catch ( ... ) {}
149 return *this;
150 }

◆ operator<<() [2/6]

MsgStream & MsgStream::operator<< ( MSG::Level level)
inline

Accept MsgStream activation using MsgStreamer operator.

Definition at line 143 of file MsgStream.h.

143{ return report( level ); }
MSG::Level level() const
Retrieve output level.
Definition MsgStream.h:108
MsgStream & report(int lvl)
Initialize report of new message: activate if print level is sufficient.
Definition MsgStream.h:80

◆ operator<<() [3/6]

MsgStream & MsgStream::operator<< ( MsgStream &(* _f )(MsgStream &))
inline

Accept MsgStream modifiers.

Definition at line 128 of file MsgStream.h.

128 {
129 if ( isActive() ) _f( *this );
130 return *this;
131 }

◆ operator<<() [4/6]

MsgStream & MsgStream::operator<< ( std::ios &(* _f )(std::ios &))
inline

Accept ios modifiers.

Definition at line 138 of file MsgStream.h.

138 {
139 if ( isActive() ) _f( m_stream );
140 return *this;
141 }

◆ operator<<() [5/6]

MsgStream & MsgStream::operator<< ( std::ios_base &(* _f )(std::ios_base &))
inline

Accept ios base class modifiers.

Definition at line 153 of file MsgStream.h.

153 {
154 if ( isActive() ) _f( m_stream );
155 return *this;
156 }

◆ operator<<() [6/6]

MsgStream & MsgStream::operator<< ( std::ostream &(* _f )(std::ostream &))
inline

Accept oMsgStream modifiers.

Definition at line 133 of file MsgStream.h.

133 {
134 if ( isActive() ) _f( m_stream );
135 return *this;
136 }

◆ precision() [1/2]

std::streamsize MsgStream::precision ( ) const
inline

Definition at line 161 of file MsgStream.h.

161{ return isActive() ? m_stream.precision() : 0; }

◆ precision() [2/2]

std::streamsize MsgStream::precision ( int v)
inline

Definition at line 162 of file MsgStream.h.

162{ return isActive() ? m_stream.precision( v ) : 0; }

◆ rdstate()

int MsgStream::rdstate ( ) const
inline

Definition at line 169 of file MsgStream.h.

169{ return isActive() ? m_stream.rdstate() : std::ios_base::failbit; }

◆ report()

MsgStream & MsgStream::report ( int lvl)
inline

Initialize report of new message: activate if print level is sufficient.

Definition at line 80 of file MsgStream.h.

80 {
81 lvl = ( lvl >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( lvl < MSG::NIL ) ? MSG::NIL : lvl;
82 if ( ( m_currLevel = MSG::Level( lvl ) ) >= level() ) {
83 activate();
84 } else {
85 deactivate();
86#ifndef NDEBUG
88 m_inactCounter->incrInactiveCount( MSG::Level( lvl ), m_source );
89 }
90#endif
91 }
92 return *this;
93 }
void deactivate()
Deactivate MsgStream.
Definition MsgStream.h:114
void activate()
Activate MsgStream.
Definition MsgStream.h:112
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition MsgStream.cpp:27
@ NIL
Definition IMessageSvc.h:22
@ NUM_LEVELS
Definition IMessageSvc.h:22
@ ALWAYS
Definition IMessageSvc.h:22

◆ resetColor()

void MsgStream::resetColor ( )

Reset the colors to defaults.

Definition at line 85 of file MsgStream.cpp.

85 {
86 if ( m_useColors ) {
87 try { // this may throw and we must not do it
88 m_stream << "\x1b[m" << m_service->getLogColor( m_currLevel );
89 } catch ( ... ) {}
90 }
91}

◆ setColor() [1/2]

void MsgStream::setColor ( MSG::Color col)

Set the text color.

Definition at line 64 of file MsgStream.cpp.

64 {
65 if ( m_useColors ) {
66 int fc = 90 + col;
67 try { // this may throw and we must not do it
68 m_stream << "\x1b[" << fc << ";1m";
69 } catch ( ... ) {}
70 }
71}

◆ setColor() [2/2]

void MsgStream::setColor ( MSG::Color fg,
MSG::Color bg )

Set the foreground and background colors.

Definition at line 73 of file MsgStream.cpp.

73 {
74 if ( m_useColors ) {
75 try { // this may throw and we must not do it
76 int fc = 90 + fg;
77 m_stream << "\x1b[" << fc;
78 int bc = 100 + bg;
79 m_stream << ";" << bc;
80 m_stream << ";1m";
81 } catch ( ... ) {}
82 }
83}

◆ setf() [1/2]

int MsgStream::setf ( FLAG_TYPE _f,
FLAG_TYPE _m )
inline

Definition at line 173 of file MsgStream.h.

173{ return isActive() ? m_stream.setf( _f, _m ) : 0; }

◆ setf() [2/2]

long MsgStream::setf ( FLAG_TYPE v)
inline

Definition at line 166 of file MsgStream.h.

166{ return isActive() ? m_stream.setf( v ) : 0; }

◆ setLevel()

void MsgStream::setLevel ( int level)
inline

Update outputlevel.

Definition at line 103 of file MsgStream.h.

103 {
104 level = ( level >= MSG::NUM_LEVELS ) ? MSG::ALWAYS : ( level < MSG::NIL ) ? MSG::NIL : level;
106 }

◆ setMsgSvc()

void MsgStream::setMsgSvc ( IMessageSvc * svc)
inline

Update IMessageSvc pointer.

Definition at line 101 of file MsgStream.h.

101{ m_service = svc; }

◆ stream()

std::ostringstream & MsgStream::stream ( )
inline

Access string MsgStream.

Definition at line 99 of file MsgStream.h.

99{ return m_stream; }

◆ unsetf()

void MsgStream::unsetf ( FLAG_TYPE _l)
inline

Definition at line 174 of file MsgStream.h.

174 {
175 if ( isActive() ) m_stream.unsetf( _l );
176 }

◆ width() [1/2]

std::streamsize MsgStream::width ( ) const
inline

IOS emulation.

Definition at line 159 of file MsgStream.h.

159{ return isActive() ? m_stream.width() : 0; }

◆ width() [2/2]

std::streamsize MsgStream::width ( std::streamsize v)
inline

Definition at line 160 of file MsgStream.h.

160{ return isActive() ? m_stream.width( v ) : 0; }

◆ write()

MsgStream & MsgStream::write ( const char * buff,
int len )
inline

Definition at line 123 of file MsgStream.h.

123 {
124 if ( isActive() ) m_stream.write( buff, len );
125 return *this;
126 }

Member Data Documentation

◆ m_active

bool MsgStream::m_active = false
protected

Flag set to true if formatting engine is active.

Definition at line 47 of file MsgStream.h.

◆ m_buffer

std::string MsgStream::m_buffer
protected

Use standard string for information buffering.

Definition at line 41 of file MsgStream.h.

◆ m_countInactive

bool MsgStream::m_countInactive = false
staticprotected

Flag to state if the inactive messages has to be counted.

Definition at line 58 of file MsgStream.h.

◆ m_currLevel

MSG::Level MsgStream::m_currLevel
protected

Current debug level.

Definition at line 51 of file MsgStream.h.

◆ m_inactCounter

IInactiveMessageCounter* MsgStream::m_inactCounter = nullptr
protected

Pointer to service counting messages prepared but not printed because of wrong level.

Definition at line 56 of file MsgStream.h.

◆ m_level

MSG::Level MsgStream::m_level
protected

Debug level of the message service.

Definition at line 49 of file MsgStream.h.

◆ m_service

IMessageSvc* MsgStream::m_service
protected

Pointer to message service if buffer has send.

Definition at line 39 of file MsgStream.h.

◆ m_source

std::string MsgStream::m_source
protected

Use std::string for source information to be passed to the message service.

Definition at line 43 of file MsgStream.h.

◆ m_stream

std::ostringstream MsgStream::m_stream
protected

String MsgStream associated to buffer.

Definition at line 45 of file MsgStream.h.

◆ m_useColors

bool MsgStream::m_useColors
protected

use colors

Definition at line 53 of file MsgStream.h.


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