ATLAS Offline Software
Loading...
Searching...
No Matches
errorcheck.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
11
15#include "GaudiKernel/MsgStream.h"
16#include "GaudiKernel/INamedInterface.h"
17#include <ctype.h>
18
19
20namespace errorcheck {
21
22
24std::atomic<bool> ReportMessage::s_hide_error_locus;
25
26
29
30
38std::string munge_filename (const std::string& file, const std::string& pkg)
39{
40 // Extract package name in case there is a version (MyPackage-00-00-00)
41 const std::string p = pkg.substr(0, pkg.find('-'));
42 if (!p.empty()) {
43 // Find package name in path and remove any leading entries
44 std::string::size_type ipos = file.find("/"+p+"/");
45 if (ipos != std::string::npos) {
46 return file.substr(ipos+1, std::string::npos);
47 }
48 }
49 return file;
50}
51
52
65 int line,
66 const char* file,
67 const char* func,
68 const char* pkg,
69 const std::string& context,
70 StatusCode sc)
71 : MsgStream (Athena::getMessageSvc(), context)
72{
73 // The common part.
74 format_common (level, line, file, func, pkg);
75
76 // The status code.
77 *this << ": code " << sc;
78
79 // Remember the end of the header.
80 m_pos = stream().str().size();
81}
82
83
95 int line,
96 const char* file,
97 const char* func,
98 const char* pkg,
99 const std::string& context)
100 : MsgStream (Athena::getMessageSvc(), context)
101{
102 // The common part.
103 format_common (level, line, file, func, pkg);
104
105 // Remember the end of the header.
106 m_pos = stream().str().size();
107}
108
109
118void ReportMessage::format_common (MSG::Level level,
119 int line,
120 const char* file,
121 const char* func,
122 const char* pkg)
123{
124 // Logging level.
125 *this << level;
126
127 // Write the source file/line.
129 *this << "FILE:LINE";
130 else {
131 if (pkg && pkg[0] != '\0')
132 *this << munge_filename(file, pkg) << ":" << line;
133 else
134 *this << file << ":" << line;
135 }
136
137 // Include the function name if available.
139 *this << " (FUNC)";
140 else {
141 if (func && func[0] != '\0') {
142 *this << " (" << CxxUtils::normalizeFunctionName(func) << ")";
143 }
144 }
145}
146
147
153{
154 // Don't do anything if the message has already been emitted
155 // (due to using << endmsg, for example).
156 if (!stream().str().empty())
157 *this << endmsg;
158}
159
160
167{
168 // The deal here is this. We want
169 // REPORT_MESSAGE(MSG::INFO) << "foo";
170 // to get a `: ' before the `foo'.
171 // But we don't want
172 // REPORT_MESSAGE(MSG::INFO);
173 // to have a trailing `: '.
174 // So, after we generate the common header part, we remember
175 // where we are in the string. When the message is emitted, we look
176 // to see if additional text has been added. If not, then we don't
177 // need to do anything. But if so, we insert a `: ' after the header.
178 if (m_pos != stream().str().size()) {
179 std::string tmp1 = stream().str();
180 std::string tmp2 = tmp1.substr(0, m_pos);
181 tmp2 += ": ";
182 tmp2.append( tmp1, m_pos);
183 stream().str (tmp2);
184 }
185 return MsgStream::doOutput();
186}
187
188
197void ReportMessage::hideErrorLocus (bool flag /*= true*/)
198{
199 s_hide_error_locus = flag;
200}
201
202
211void ReportMessage::hideFunctionNames (bool flag /*= true*/)
212{
214}
215
216
217} // namespace errorcheck
218
219
220namespace errorcheck {
221
222
230std::string context_name (const INamedInterface* context)
231{
232 return context->name();
233}
234
235
244std::string context_name (const void* /*context*/)
245{
246 return "";
247}
248
249
250} // namespace errorcheck
#define endmsg
Helpers for checking error return status codes and reporting errors.
static Double_t sc
static const Attributes_t empty
std::string::size_type m_pos
The position in the output message after the standard header.
static std::atomic< bool > s_hide_function_names
If true, hide the function names in output messages.
virtual MsgStream & doOutput() override
Emit the message.
static std::atomic< bool > s_hide_error_locus
If true, hide the source file and line number in output messages.
static void hideFunctionNames(bool flag=true)
If set to true, hide function names in the output.
static void hideErrorLocus(bool flag=true)
If set to true, hide the source file and line number in the output.
ReportMessage(MSG::Level level, int line, const char *file, const char *func, const char *pkg, const std::string &context, StatusCode sc)
Constructor.
void format_common(MSG::Level level, int line, const char *file, const char *func, const char *pkg)
Generate the common header for messages.
singleton-like access to IMessageSvc via open function and helper
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
std::string normalizeFunctionName(const std::string &fname)
Normalize a pretty-printed C++ function name.
std::string munge_filename(const std::string &file, const std::string &pkg)
Shorten filename.
std::string context_name(const INamedInterface *context)
Return the context name from a context (this) pointer.
Normalize a pretty-printed C++ function name,.
TFile * file