The Gaudi Framework  v40r0 (475e45c1)
MsgStream.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 \***********************************************************************************/
13 #include <GaudiKernel/Message.h>
14 #include <GaudiKernel/MsgStream.h>
15 #include <cstdarg>
16 #include <cstdio>
17 #include <iostream>
18 
19 bool MsgStream::m_countInactive = false;
20 
21 bool MsgStream::enableCountInactive( bool value ) {
22  bool old = m_countInactive;
23  m_countInactive = value;
24  return old;
25 }
26 
28 
29 MsgStream::MsgStream( IMessageSvc* svc, int ) : m_service( svc ) {
30  setLevel( svc ? svc->outputLevel() : MSG::INFO );
32  m_useColors = ( svc ? svc->useColor() : false );
33 #ifndef NDEBUG
34  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
35 #endif
36 }
37 
38 MsgStream::MsgStream( IMessageSvc* svc, std::string source, int ) : 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
43  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>( svc ) : 0;
44 #endif
45 }
46 
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 ) {
55  } else {
56  std::cout << msg << std::endl;
57  }
58  }
59  m_stream.str( "" );
60  } catch ( ... ) {}
61  return *this;
62 }
63 
65 #ifndef _WIN32
66  if ( m_useColors ) {
67  int fc = 90 + col;
68  try { // this may throw and we must not do it
69  m_stream << "\x1b[" << fc << ";1m";
70  } catch ( ... ) {}
71  }
72 #endif
73 }
74 
76 #ifndef _WIN32
77  if ( m_useColors ) {
78  try { // this may throw and we must not do it
79  int fc = 90 + fg;
80  m_stream << "\x1b[" << fc;
81  int bc = 100 + bg;
82  m_stream << ";" << bc;
83  m_stream << ";1m";
84  } catch ( ... ) {}
85  }
86 #endif
87 }
88 
90 #ifndef _WIN32
91  if ( m_useColors ) {
92  try { // this may throw and we must not do it
93  m_stream << "\x1b[m" << m_service->getLogColor( m_currLevel );
94  } catch ( ... ) {}
95  }
96 #endif
97 }
98 
99 std::string format( const char* fmt, ... ) {
100  const int buffsize = 2048;
101  static char buffer[buffsize];
102  va_list arguments;
103  va_start( arguments, fmt );
104  if ( vsnprintf( buffer, buffsize, fmt, arguments ) >= buffsize ) {
105  va_end( arguments );
106  throw GaudiException( "Insufficient buffer size (" + std::to_string( buffsize ) + ") when formatting message",
107  "MsgStream", StatusCode::FAILURE );
108  }
109  va_end( arguments );
110  return std::string( buffer );
111 }
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:99
IMessageSvc
Definition: IMessageSvc.h:44
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition: MsgStream.cpp:21
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition: MsgStream.h:39
MSG::Color
Color
Definition: IMessageSvc.h:23
MSG::INFO
@ INFO
Definition: IMessageSvc.h:22
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
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:53
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition: MsgStream.cpp:47
GaudiException
Definition: GaudiException.h:29
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition: MsgStream.cpp:64
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition: MsgStream.h:49
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:25
MsgStream::m_stream
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition: MsgStream.h:45
MsgStream::m_source
std::string m_source
Use std::string for source information to be passed to the message service.
Definition: MsgStream.h:43
MsgStream
Definition: MsgStream.h:29
MsgStream::m_currLevel
MSG::Level m_currLevel
Current debug level.
Definition: MsgStream.h:51
MsgStream::MsgStream
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition: MsgStream.cpp:29
fmt
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:103
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition: MsgStream.h:56
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition: MsgStream.h:116
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition: MsgStream.cpp:89
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:58
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition: MsgStream.cpp:27