Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  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 }
72 
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 }
84 
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 }
92 
93 std::string format( const char* fmt, ... ) {
94  const int buffsize = 2048;
95  static char buffer[buffsize];
96  va_list arguments;
97  va_start( arguments, fmt );
98  if ( vsnprintf( buffer, buffsize, fmt, arguments ) >= buffsize ) {
99  va_end( arguments );
100  throw GaudiException( "Insufficient buffer size (" + std::to_string( buffsize ) + ") when formatting message",
101  "MsgStream", StatusCode::FAILURE );
102  }
103  va_end( arguments );
104  return std::string( buffer );
105 }
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:93
IMessageSvc
Definition: IMessageSvc.h:34
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:85
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