The Gaudi Framework
master (9f3f8895)
Toggle main menu visibility
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
\***********************************************************************************/
11
#include <
GaudiKernel/GaudiException.h
>
12
#include <
GaudiKernel/IMessageSvc.h
>
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
27
bool
MsgStream::countInactive
() {
return
m_countInactive
; }
28
29
MsgStream::MsgStream
(
IMessageSvc
* svc,
int
) :
m_service
( svc ) {
30
setLevel
( svc ? svc->
outputLevel
() :
MSG::INFO
);
31
m_currLevel
=
m_level
;
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
);
40
m_currLevel
=
m_level
;
41
m_useColors
= ( svc && svc->
useColor
() );
42
#ifndef NDEBUG
43
m_inactCounter
= svc ?
Gaudi::Cast<IInactiveMessageCounter>
( svc ) : 0;
44
#endif
45
}
46
47
MsgStream
&
MsgStream::doOutput
() {
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
64
void
MsgStream::setColor
(
MSG::Color
col ) {
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
73
void
MsgStream::setColor
(
MSG::Color
fg,
MSG::Color
bg ) {
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
85
void
MsgStream::resetColor
() {
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
}
GaudiException.h
IMessageSvc.h
Message.h
format
std::string format(const char *fmt,...)
MsgStream format utility "a la sprintf(...)".
Definition
MsgStream.cpp:93
MsgStream.h
GaudiException
Define general base for Gaudi exception.
Definition
GaudiException.h:29
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition
IMessageSvc.h:34
IMessageSvc::outputLevel
virtual int outputLevel() const =0
Retrieve the current output level threshold.
IMessageSvc::useColor
virtual bool useColor() const =0
Show whether colors are used.
Message
The Message class.
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::m_countInactive
static bool m_countInactive
Flag to state if the inactive messages has to be counted.
Definition
MsgStream.h:58
MsgStream::m_inactCounter
IInactiveMessageCounter * m_inactCounter
Pointer to service counting messages prepared but not printed because of wrong level.
Definition
MsgStream.h:56
MsgStream::enableCountInactive
static GAUDI_API bool enableCountInactive(bool value=true)
Enable/disable the count of inactive messages.
Definition
MsgStream.cpp:21
MsgStream::m_useColors
bool m_useColors
use colors
Definition
MsgStream.h:53
MsgStream::countInactive
static GAUDI_API bool countInactive()
Returns the state of the counting of inactive messages (enabled/disabled).
Definition
MsgStream.cpp:27
MsgStream::m_level
MSG::Level m_level
Debug level of the message service.
Definition
MsgStream.h:49
MsgStream::doOutput
virtual GAUDI_API MsgStream & doOutput()
Output method.
Definition
MsgStream.cpp:47
MsgStream::resetColor
GAUDI_API void resetColor()
Reset the colors to defaults.
Definition
MsgStream.cpp:85
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
MsgStream::isActive
bool isActive() const
Accessor: is MsgStream active.
Definition
MsgStream.h:116
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition
MsgStream.h:103
MsgStream::setColor
GAUDI_API void setColor(MSG::Color col)
Set the text color.
Definition
MsgStream.cpp:64
MsgStream::m_service
IMessageSvc * m_service
Pointer to message service if buffer has send.
Definition
MsgStream.h:39
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition
StatusCode.h:100
Gaudi::Cast
TARGET * Cast(IInterface *i)
Definition
IInterface.h:340
MSG::Color
Color
Definition
IMessageSvc.h:23
MSG::INFO
@ INFO
Definition
IMessageSvc.h:22
std
STL namespace.
GaudiKernel
src
Lib
MsgStream.cpp
Generated on
for The Gaudi Framework by
1.17.0