ATLAS Offline Software
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 
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/INamedInterface.h"
17 #include <ctype.h>
18 
19 
20 namespace errorcheck {
21 
22 
24 std::atomic<bool> ReportMessage::s_hide_error_locus;
25 
26 
28 std::atomic<bool> ReportMessage::s_hide_function_names;
29 
30 
38 std::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 
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.
128  if (s_hide_error_locus)
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 
197 void ReportMessage::hideErrorLocus (bool flag /*= true*/)
198 {
200 }
201 
202 
212 {
214 }
215 
216 
217 } // namespace errorcheck
218 
219 
220 namespace errorcheck {
221 
222 
230 std::string context_name (const INamedInterface* context)
231 {
232  return context->name();
233 }
234 
235 
244 std::string context_name (const void* /*context*/)
245 {
246  return "";
247 }
248 
249 
250 } // namespace errorcheck
checkFileSG.line
line
Definition: checkFileSG.py:75
DeMoUpdate.tmp2
string tmp2
Definition: DeMoUpdate.py:1168
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
errorcheck::ReportMessage::~ReportMessage
~ReportMessage()
Destructor.
Definition: errorcheck.cxx:152
errorcheck::ReportMessage::format_common
void format_common(MSG::Level level, int line, const char *file, const char *func, const char *pkg)
Generate the common header for messages.
Definition: errorcheck.cxx:118
errorcheck::munge_filename
std::string munge_filename(const std::string &file, const std::string &pkg)
Shorten filename.
Definition: errorcheck.cxx:38
errorcheck::ReportMessage::s_hide_error_locus
static std::atomic< bool > s_hide_error_locus
If true, hide the source file and line number in output messages.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:295
errorcheck::context_name
std::string context_name(const INamedInterface *context)
Return the context name from a context (this) pointer.
Definition: errorcheck.cxx:230
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
errorcheck::ReportMessage::s_hide_function_names
static std::atomic< bool > s_hide_function_names
If true, hide the function names in output messages.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:298
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
errorcheck
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:138
errorcheck::ReportMessage::doOutput
virtual MsgStream & doOutput()
Emit the message.
Definition: errorcheck.cxx:166
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
master.flag
bool flag
Definition: master.py:29
errorcheck::ReportMessage::m_pos
std::string::size_type m_pos
The position in the output message after the standard header.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:292
file
TFile * file
Definition: tile_monitor.h:29
errorcheck::ReportMessage::hideFunctionNames
static void hideFunctionNames(bool flag=true)
If set to true, hide function names in the output.
Definition: errorcheck.cxx:211
errorcheck::ReportMessage::hideErrorLocus
static void hideErrorLocus(bool flag=true)
If set to true, hide the source file and line number in the output.
Definition: errorcheck.cxx:197
errorcheck.h
Helpers for checking error return status codes and reporting errors.
errorcheck::ReportMessage::ReportMessage
ReportMessage(MSG::Level level, int line, const char *file, const char *func, const char *pkg, const std::string &context, StatusCode sc)
Constructor.
Definition: errorcheck.cxx:64
normalizeFunctionName.h
Normalize a pretty-printed C++ function name,.
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
CxxUtils::normalizeFunctionName
std::string normalizeFunctionName(const std::string &fname)
Normalize a pretty-printed C++ function name.
Definition: normalizeFunctionName.cxx:135