ATLAS Offline Software
Loading...
Searching...
No Matches
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-2025 CERN for the benefit of the ATLAS collaboration
5*/
6
121
122
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
134class INamedInterface;
135
136
137// Internal out-of-line functions go in this namespace.
138namespace errorcheck {
139
140
148std::string context_name (const INamedInterface* context);
149
150
159std::string context_name (const void* context);
160
161
172 : public MsgStream
173{
174public:
186 ReportMessage (MSG::Level level,
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
205 ReportMessage (MSG::Level level,
206 int line,
207 const char* file,
208 const char* func,
209 const char* pkg,
210 const std::string& context);
211
212
218
219
225 virtual MsgStream& doOutput() override;
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
276private:
285 void format_common (MSG::Level level,
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
static Double_t sc
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.
MsgStream & msgstream()
Convert to a MsgStream reference.
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.
std::string context_name(const INamedInterface *context)
Return the context name from a context (this) pointer.
TFile * file