The Gaudi Framework  master (37c0b60a)
MsgStream.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 //====================================================================
12 // MsgStream.cpp
13 //--------------------------------------------------------------------
14 //
15 // Package : System ( The LHCb Offline System)
16 //
17 // Description: Class to ease error logging to the message service
18 //
19 // Author : M.Frank
20 // History :
21 // +---------+----------------------------------------------+---------
22 // | Date | Comment | Who
23 // +---------+----------------------------------------------+---------
24 // | 29/10/98| Initial version | MF
25 // +---------+----------------------------------------------+---------
26 //
27 //====================================================================
28 #define KERNEL_MSGSTREAM_CPP
29 
32 #include <GaudiKernel/Message.h>
33 #include <GaudiKernel/MsgStream.h>
34 
35 #include <cstdarg>
36 #include <cstdio>
37 #include <iostream>
38 
39 bool MsgStream::m_countInactive = false;
40 
41 bool MsgStream::enableCountInactive( bool value ) {
42  bool old = m_countInactive;
43  m_countInactive = value;
44  return old;
45 }
46 
48 
49 MsgStream::MsgStream( IMessageSvc* svc, int ) : m_service( svc ) {
50  setLevel( svc ? svc->outputLevel() : MSG::INFO );
52  m_useColors = ( svc ? svc->useColor() : false );
53 #ifndef NDEBUG
54  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
55 #endif
56 }
57 
58 MsgStream::MsgStream( IMessageSvc* svc, std::string source, int ) : m_service( svc ), m_source( std::move( source ) ) {
59  setLevel( svc ? svc->outputLevel( m_source ) : MSG::INFO );
61  m_useColors = ( svc && svc->useColor() );
62 #ifndef NDEBUG
63  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
64 #endif
65 }
66 
68  try {
69  // This piece of code may throw and we cannot afford it when we print a message
70  // in the middle of a catch block.
71  if ( isActive() ) {
73  if ( m_service ) {
75  } else {
76  std::cout << msg << std::endl;
77  }
78  }
79  m_stream.str( "" );
80  } catch ( ... ) {}
81  return *this;
82 }
83 
85 #ifndef _WIN32
86  if ( m_useColors ) {
87  int fc = 90 + col;
88  try { // this may throw and we must not do it
89  m_stream << "\x1b[" << fc << ";1m";
90  } catch ( ... ) {}
91  }
92 #endif
93 }
94 
96 #ifndef _WIN32
97  if ( m_useColors ) {
98  try { // this may throw and we must not do it
99  int fc = 90 + fg;
100  m_stream << "\x1b[" << fc;
101  int bc = 100 + bg;
102  m_stream << ";" << bc;
103  m_stream << ";1m";
104  } catch ( ... ) {}
105  }
106 #endif
107 }
108 
110 #ifndef _WIN32
111  if ( m_useColors ) {
112  try { // this may throw and we must not do it
113  m_stream << "\x1b[m" << m_service->getLogColor( m_currLevel );
114  } catch ( ... ) {}
115  }
116 #endif
117 }
118 
119 std::string format( const char* fmt, ... ) {
120  const int buffsize = 2048;
121  static char buffer[buffsize];
122  va_list arguments;
123  va_start( arguments, fmt );
124  if ( vsnprintf( buffer, buffsize, fmt, arguments ) >= buffsize ) {
125  va_end( arguments );
126  throw GaudiException( "Insufficient buffer size (" + std::to_string( buffsize ) + ") when formatting message",
127  "MsgStream", StatusCode::FAILURE );
128  }
129  va_end( arguments );
130  return std::string( buffer );
131 }
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:41
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:43
MSG::Color
Color
Definition: IMessageSvc.h:26
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
IMessageSvc::getLogColor
virtual std::string getLogColor(int logLevel) const =0
Get the color codes for various log levels.
GaudiException.h
MsgStream::m_useColors
bool m_useColors
use colors
Definition: MsgStream.h:57
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:67
GaudiException
Definition: GaudiException.h:31
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:84
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:53
IMessageSvc::useColor
virtual bool useColor() const =0
Show whether colors are used.
IMessageSvc.h
IMessageSvc::reportMessage
virtual void reportMessage(const Message &msg, int outputLevel)=0
Report a message by sending a Message object to the message service.
IMessageSvc::outputLevel
virtual int outputLevel() const =0
Retrieve the current output level threshold.
Message
Definition: Message.h:26
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:49
std::cout
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:47
std::to_string
T to_string(T... args)
MsgStream
Definition: MsgStream.h:33
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:55
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:49
std::endl
T endl(T... args)
std
STL namespace.
fmt
std::ostringstream::str
T str(T... args)
va_list
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:107
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:60
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:120
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:109
MsgStream.h
Message.h
MsgStream::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition: MsgStream.h:62
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:47