ATLAS Offline Software
Control/AthenaKernel/AthenaKernel/errorcheck.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
5 */
6 
123 #ifndef ATHENAKERNEL_ERRORCHECK_H
124 #define ATHENAKERNEL_ERRORCHECK_H
125 
126 
127 #include "GaudiKernel/StatusCode.h"
128 #include "GaudiKernel/MsgStream.h"
129 #include "GaudiKernel/IMessageSvc.h"
130 #include "boost/preprocessor/facilities/overload.hpp"
131 #include <string>
132 #include <atomic>
133 
134 class INamedInterface;
135 
136 
137 // Internal out-of-line functions go in this namespace.
138 namespace errorcheck {
139 
140 
148 std::string context_name (const INamedInterface* context);
149 
150 
159 std::string context_name (const void* context);
160 
161 
172  : public MsgStream
173 {
174 public:
187  int line,
188  const char* file,
189  const char* func,
190  const char* pkg,
191  const std::string& context,
192  StatusCode sc);
193 
194 
206  int line,
207  const char* file,
208  const char* func,
209  const char* pkg,
210  const std::string& context);
211 
212 
217  ~ReportMessage();
218 
219 
225  virtual MsgStream& doOutput();
226 
251  MsgStream& msgstream() { return *this; }
252 
253 
262  static void hideErrorLocus (bool flag = true);
263 
264 
273  static void hideFunctionNames (bool flag = true);
274 
275 
276 private:
286  int line,
287  const char* file,
288  const char* func,
289  const char* pkg);
290 
292  std::string::size_type m_pos;
293 
295  static std::atomic<bool> s_hide_error_locus;
296 
298  static std::atomic<bool> s_hide_function_names;
299 };
300 
301 
302 } // namespace errorcheck
303 
304 
305 // Macro to get the current function name.
306 // Currently, this relies on a gcc extension, and will just return
307 // an empty string for another compiler. A similar mechanism (__func__)
308 // has been proposed for the next revision of the C++ standard (cf. n1642);
309 // if that makes it in, it can be used instead.
310 #if defined(__GNUC__)
311 # define ERRORCHECK_FNAME __PRETTY_FUNCTION__
312 #else
313 # define ERRORCHECK_FNAME ""
314 #endif
315 
316 // Define empty package name in case there is none defined already
317 #ifndef ATLAS_PACKAGE_NAME
318 # define ATLAS_PACKAGE_NAME nullptr
319 #endif
320 
321 // Collect all the arguments describing the source location:
322 // line number, file name, function name, package.
323 #define ERRORCHECK_ARGS __LINE__, __FILE__, ERRORCHECK_FNAME, ATLAS_PACKAGE_NAME
324 
325 
333 #define REPORT_ERROR_WITH_CONTEXT(SC, CONTEXT_NAME) \
334  errorcheck::ReportMessage (MSG::ERROR, ERRORCHECK_ARGS, CONTEXT_NAME, SC) \
335  . msgstream()
336 
337 
345 #define REPORT_MESSAGE_WITH_CONTEXT(LVL, CONTEXT_NAME) \
346  errorcheck::ReportMessage (LVL, ERRORCHECK_ARGS, CONTEXT_NAME).msgstream()
347 
348 
355 #define REPORT_ERROR(SC) \
356  REPORT_ERROR_WITH_CONTEXT(SC, errorcheck::context_name (this))
357 
358 
365 #define REPORT_MESSAGE(LVL) \
366  REPORT_MESSAGE_WITH_CONTEXT(LVL, errorcheck::context_name (this))
367 
368 
374 #define CHECK_FAILED(EXP, CONTEXT_NAME, SC, RET) do { \
375  errorcheck::ReportMessage (MSG::ERROR, \
376  ERRORCHECK_ARGS, \
377  CONTEXT_NAME, \
378  SC) . msgstream() \
379  << #EXP; \
380  return RET; \
381  } while(0)
382 
383 
395 #ifndef __CPPCHECK__ // The varadic macros here confuse cppcheck.
396 #define CHECK_WITH_CONTEXT(...) \
397  BOOST_PP_OVERLOAD(CHECK_WITH_CONTEXT_, __VA_ARGS__)(__VA_ARGS__)
398 
399 #define CHECK_WITH_CONTEXT_2(EXP, CONTEXT_NAME) do { \
400  StatusCode sc__(EXP); \
401  if (! sc__.isSuccess()) \
402  CHECK_FAILED(EXP, CONTEXT_NAME, sc__, sc__); \
403  } while (0)
404 
405 #define CHECK_WITH_CONTEXT_3(EXP, CONTEXT_NAME, RET) do { \
406  StatusCode sc__(EXP); \
407  if (! sc__.isSuccess()) \
408  CHECK_FAILED(EXP, CONTEXT_NAME, sc__, RET); \
409  } while (0)
410 
411 
422 #define CHECK(...) \
423  BOOST_PP_OVERLOAD(CHECK_, __VA_ARGS__)(__VA_ARGS__)
424 
425 #define CHECK_1(EXP) CHECK_WITH_CONTEXT(EXP, errorcheck::context_name(this))
426 #define CHECK_2(EXP, RET) CHECK_WITH_CONTEXT(EXP, errorcheck::context_name(this), RET)
427 #endif
428 
429 
441 #define CHECK_CODE_WITH_CONTEXT(EXP, CONTEXT_NAME, CODE) do { \
442  StatusCode sc__ = (EXP); \
443  if (! sc__.isSuccess()) { \
444  sc__ = CODE; \
445  CHECK_FAILED(EXP, CONTEXT_NAME, sc__, sc__); \
446  } \
447  } while (0)
448 
449 
460 #define CHECK_CODE(EXP, CODE) \
461  CHECK_CODE_WITH_CONTEXT(EXP, errorcheck::context_name(this), CODE)
462 
463 
474 #define CHECK_RECOVERABLE_WITH_CONTEXT(EXP, CONTEXT_NAME) \
475  CHECK_CODE_WITH_CONTEXT(EXP, CONTEXT_NAME, StatusCode::RECOVERABLE)
476 
477 
487 #define CHECK_RECOVERABLE(EXP) \
488  CHECK_RECOVERABLE_WITH_CONTEXT(EXP, errorcheck::context_name(this))
489 
490 
501 #define CHECK_FATAL_WITH_CONTEXT(EXP, CONTEXT_NAME) \
502  CHECK_CODE_WITH_CONTEXT(EXP, CONTEXT_NAME, StatusCode::FAILURE)
503 
504 
514 #define CHECK_FATAL(EXP) \
515  CHECK_FATAL_WITH_CONTEXT(EXP, errorcheck::context_name(this))
516 
517 
518 #endif // not ATHENAKERNEL_ERRORCODE_H
checkFileSG.line
line
Definition: checkFileSG.py:75
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::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
errorcheck::ReportMessage
Helper class to use to report a message.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:173
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
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
errorcheck
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:138
errorcheck::ReportMessage::doOutput
virtual MsgStream & doOutput()
Emit the message.
Definition: errorcheck.cxx:166
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
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::msgstream
MsgStream & msgstream()
Convert to a MsgStream reference.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:251
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::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