The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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>
15#include <cstdarg>
16#include <cstdio>
17#include <iostream>
18
20
22 bool old = m_countInactive;
23 m_countInactive = value;
24 return old;
25}
26
28
30 setLevel( svc ? svc->outputLevel() : MSG::INFO );
32 m_useColors = ( svc ? svc->useColor() : false );
33#ifndef NDEBUG
35#endif
36}
37
38MsgStream::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
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 ) {
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}
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
93std::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}
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
Define general base for Gaudi exception.
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
virtual int outputLevel() const =0
Retrieve the current output level threshold.
virtual bool useColor() const =0
Show whether colors are used.
The Message class.
Definition Message.h:25
std::ostringstream m_stream
String MsgStream associated to buffer.
Definition MsgStream.h:45
std::string m_source
Use std::string for source information to be passed to the message service.
Definition MsgStream.h:43
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition MsgStream.h:58
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition MsgStream.h:56
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition MsgStream.cpp:21
bool m_useColors
use colors
Definition MsgStream.h:53
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition MsgStream.cpp:27
MSG::Level m_level
Debug level of the message service.
Definition MsgStream.h:49
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition MsgStream.cpp:47
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition MsgStream.cpp:85
MSG::Level m_currLevel
Current debug level.
Definition MsgStream.h:51
GAUDI_API MsgStream(IMessageSvc *svc, int buffer_length=128)
Standard constructor: Connect to message service for output.
Definition MsgStream.cpp:29
bool isActive() const
Accessor: is MsgStream active.
Definition MsgStream.h:116
void setLevel(int level)
Update outputlevel.
Definition MsgStream.h:103
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition MsgStream.cpp:64
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition MsgStream.h:39
constexpr static const auto FAILURE
Definition StatusCode.h:100
TARGET * Cast(IInterface *i)
Definition IInterface.h:333
@ INFO
Definition IMessageSvc.h:22
STL namespace.