ATLAS Offline Software
Loading...
Searching...
No Matches
Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9#ifndef ASG_MESSAGING__MESSAGE_CHECK_H
10#define ASG_MESSAGING__MESSAGE_CHECK_H
11
13
76
77
78
79#include <type_traits>
80
83
86#include <atomic>
87
113#define ANA_MSG_HEADER(NAME) \
114 namespace NAME { \
115 MsgStream& msg (); \
116 MsgStream& msg (MSG::Level level); \
117 bool msgLvl (MSG::Level lvl); \
118 void setMsgLevel (MSG::Level level); }
119
120#ifdef XAOD_STANDALONE
121#define ASG_TOOLS_MSG_STREAM(NAME,TITLE) \
122 static MsgStream& NAME {::asg::MsgHelpers::pkgMsgStream (TITLE)};
123#else
124#define ASG_TOOLS_MSG_STREAM(NAME,TITLE) \
125 static thread_local MsgStream NAME {::asg::MsgHelpers::pkgMsgStream (TITLE)};
126#endif
127
133#define ANA_MSG_SOURCE(NAME,TITLE) \
134 namespace NAME \
135 { \
136 MsgStream& msg () \
137 { \
138 ASG_TOOLS_MSG_STREAM (result, TITLE); \
139 return result; \
140 } \
141 \
142 MsgStream& msg (MSG::Level level) \
143 { \
144 return msg() << level; \
145 } \
146 \
147 bool msgLvl (MSG::Level lvl) \
148 { \
149 if (msg().level() <= lvl) \
150 { \
151 msg() << lvl; \
152 return true; \
153 } else \
154 { \
155 return false; \
156 } \
157 } \
158 \
159 void setMsgLevel (MSG::Level level) \
160 { \
161 ::asg::MsgHelpers::setPkgMsgLevel (TITLE, level); \
162 } \
163 }
164
165namespace asg
166{
167 ANA_MSG_HEADER (msgUserCode)
168}
169
170namespace asg
171{
174 template<typename T> struct CheckHelper;
175
176 template<> struct CheckHelper<StatusCode>
177 {
179 static inline bool isSuccess (const StatusCode& sc) {
180 return sc.isSuccess(); }
181
183 static constexpr inline auto successCode () {
184 return StatusCode::SUCCESS;}
185
187 static constexpr inline auto failureCode () {
188 return StatusCode::FAILURE;}
189 };
190
191 template<> struct CheckHelper<int>
192 {
194 static inline int successCode () {
195 return 0;}
196
203 static inline int failureCode () {
204 return 220;}
205 };
206
207 template<> struct CheckHelper<bool>
208 {
210 static inline bool isSuccess (const bool& sc) {
211 return sc; }
212
214 static inline bool successCode () {
215 return true;}
216
218 static inline bool failureCode () {
219 return false;}
220 };
221
222 template<typename T> struct CheckHelper<T*>
223 {
225 static inline bool isSuccess (const T *sc) {
226 return sc; }
227
229 static inline T *failureCode () {
230 return nullptr;}
231 };
232
233 namespace detail
234 {
239 [[noreturn]] void throw_check_fail (const std::string& str);
240 }
241}
242
243
244// This is a GCC extension for getting the name of the current function.
245#if defined( __GNUC__ )
246# define MSGSTREAM_FNAME CxxUtils::normalizeFunctionName(__PRETTY_FUNCTION__)
247#else
248# define MSGSTREAM_FNAME ""
249#endif
250
251
252// This is a GCC extension for getting the name of the current function.
253#if defined( __GNUC__ )
254# define ASG_TOOLS_MSGSTREAM_FNAME CxxUtils::normalizeFunctionName(__PRETTY_FUNCTION__)
255#else
256# define ASG_TOOLS_MSGSTREAM_FNAME ""
257#endif
258
266#define ASG_TOOLS_MSGSTREAM_PREFIX \
267 __FILE__ << ":" << __LINE__ << " (" << ASG_TOOLS_MSGSTREAM_FNAME << "): "
268
270#define ANA_MSG_LVL_SERIOUS( lvl, xmsg ) \
271 msg( lvl ) << ASG_TOOLS_MSGSTREAM_PREFIX << xmsg << endmsg
272
274#define ANA_MSG_LVL_NOCHK( lvl, xmsg ) \
275 msg( lvl ) << xmsg << endmsg
276
278#define ANA_MSG_LVL( lvl, xmsg ) \
279 do { \
280 if( msg().level() <= lvl ) { \
281 ANA_MSG_LVL_NOCHK( lvl, xmsg ); \
282 } \
283 } while( 0 )
284
286#define ANA_MSG_VERBOSE( xmsg ) ANA_MSG_LVL( MSG::VERBOSE, xmsg )
288#define ANA_MSG_DEBUG( xmsg ) ANA_MSG_LVL( MSG::DEBUG, xmsg )
290#define ANA_MSG_INFO( xmsg ) ANA_MSG_LVL_NOCHK( MSG::INFO, xmsg )
292#define ANA_MSG_WARNING( xmsg ) ANA_MSG_LVL_NOCHK( MSG::WARNING, xmsg )
294#define ANA_MSG_ERROR( xmsg ) ANA_MSG_LVL_SERIOUS( MSG::ERROR, xmsg )
296#define ANA_MSG_FATAL( xmsg ) ANA_MSG_LVL_SERIOUS( MSG::FATAL, xmsg )
298#define ANA_MSG_ALWAYS( xmsg ) ANA_MSG_LVL_NOCHK( MSG::ALWAYS, xmsg )
299
300
301
306typedef StatusCode AsgToolsCheckResultType;
307
308
314#define ANA_CHECK_SET_TYPE(TYPE) \
315 typedef TYPE AsgToolsCheckResultType;
316
317
324#define ANA_CHECK(EXP) \
325 { const auto sc__ = EXP; \
326 typedef typename std::decay<decltype(sc__)>::type scType__; \
327 if (ATH_UNLIKELY(!::asg::CheckHelper<scType__>::isSuccess (sc__))) { \
328 ANA_MSG_ERROR ("Failed to call \"" << #EXP << "\""); \
329 return ::asg::CheckHelper<AsgToolsCheckResultType>::failureCode(); \
330 } }
331
332
339#define ANA_CHECK_THROW(EXP) \
340 { const auto sc__ = EXP; \
341 typedef typename std::decay<decltype(sc__)>::type scType__; \
342 if (ATH_UNLIKELY(!::asg::CheckHelper<scType__>::isSuccess (sc__))) { \
343 std::ostringstream str; \
344 str << #EXP; \
345 ANA_MSG_ERROR ("Failed to call \"" << str.str() << "\", throwing exception"); \
346 ::asg::detail::throw_check_fail (str.str()); \
347 } }
348
349#endif
StatusCode AsgToolsCheckResultType
the return type used by ANA_CHECK
#define ANA_MSG_HEADER(NAME)
for standalone code this creates a new message category
static Double_t sc
Define macros for attributes used to control the static checker.
void throw_check_fail(const std::string &str)
throw an error for a failed check
Normalize a pretty-printed C++ function name,.
static bool isSuccess(const StatusCode &sc)
whether the status code reports a success
static constexpr auto successCode()
produce a status code to report success
static constexpr auto failureCode()
produce a status code to report failure
static bool isSuccess(const T *sc)
whether the status code reports a success
static T * failureCode()
produce a status code to report failure
static bool successCode()
produce a status code to report success
static bool isSuccess(const bool &sc)
whether the status code reports a success
static bool failureCode()
produce a status code to report failure
static int failureCode()
produce a status code to report failure
static int successCode()
produce a status code to report success
this is an internal traits class for status codes used by the ANA_CHECK* macros