All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
diagnostic.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_DIAGNOSTIC_H
8#define PXR_BASE_TF_DIAGNOSTIC_H
9
20
21#include "pxr/pxr.h"
24#include "pxr/base/tf/api.h"
25
26#if defined(__cplusplus) || defined (doxygen)
27
28#include "pxr/base/arch/hints.h"
29#include "pxr/base/tf/diagnosticHelper.h"
30
31#include <stddef.h>
32#include <stdarg.h>
33#include <string>
34
35PXR_NAMESPACE_OPEN_SCOPE
36
37// Note: diagnosticLite.h defines the various macros, but we'll document
38// them here. The following block is only for doxygen, not seen by a real
39// compile. To see the actual macro definition, look in diagnosticLite.h.
40
41#if defined(doxygen)
42
45
54#define TF_ERROR(...)
55
68#define TF_CODING_ERROR(fmt, args)
69
83#define TF_RUNTIME_ERROR(fmt, args)
84
91#define TF_FATAL_ERROR(fmt, args)
92
132#define TF_WARN(...)
133
173#define TF_STATUS(...)
174
193#define TF_AXIOM(cond)
194
205#define TF_DEV_AXIOM(cond)
206
266#define TF_VERIFY(cond [, format, ...])
267
268#endif /* defined(doxygen) */
269
270//
271// The rest of this is seen by a regular compile (or doxygen).
272//
273
274#if defined(__cplusplus) || defined(doxygen)
275
292#define TF_FUNC_NAME() \
293 ArchGetPrettierFunctionName(__ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__)
294
295void Tf_TerminateHandler();
296
297#if !defined(doxygen)
298
299// Redefine these macros from DiagnosticLite to versions that will accept
300// either string or printf-like args.
301
302#ifdef TF_CODING_ERROR
303#undef TF_CODING_ERROR
304#endif
305#define TF_CODING_ERROR(...) \
306 Tf_PostErrorHelper(TF_CALL_CONTEXT, \
307 TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
308
309#ifdef TF_FATAL_CODING_ERROR
310#undef TF_FATAL_CODING_ERROR
311#endif
312#define TF_FATAL_CODING_ERROR \
313 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
314 TF_DIAGNOSTIC_CODING_ERROR_TYPE).IssueFatalError
315
316
317#ifdef TF_CODING_WARNING
318#undef TF_CODING_WARNING
319#endif
320#define TF_CODING_WARNING(...) \
321 Tf_PostWarningHelper(TF_CALL_CONTEXT, \
322 TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
323
324#ifdef TF_DIAGNOSTIC_WARNING
325#undef TF_DIAGNOSTIC_WARNING
326#endif
327#define TF_DIAGNOSTIC_WARNING \
328 Tf_DiagnosticHelper(TF_CALL_CONTEXT.Hide(), \
329 TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
330
331#ifdef TF_RUNTIME_ERROR
332#undef TF_RUNTIME_ERROR
333#endif // TF_RUNTIME_ERROR
334#define TF_RUNTIME_ERROR(...) \
335 Tf_PostErrorHelper(TF_CALL_CONTEXT, \
336 TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE, __VA_ARGS__)
337
338#ifdef TF_FATAL_ERROR
339#undef TF_FATAL_ERROR
340#endif // TF_FATAL_ERROR
341#define TF_FATAL_ERROR \
342 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
343 TF_DIAGNOSTIC_FATAL_ERROR_TYPE).IssueFatalError
344
345#ifdef TF_DIAGNOSTIC_FATAL_ERROR
346#undef TF_DIAGNOSTIC_FATAL_ERROR
347#endif // TF_DIAGNOSTIC_FATAL_ERROR
348#define TF_DIAGNOSTIC_FATAL_ERROR \
349 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
350 TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE).IssueFatalError
351
352#ifdef TF_DIAGNOSTIC_NONFATAL_ERROR
353#undef TF_DIAGNOSTIC_NONFATAL_ERROR
354#endif // TF_DIAGNOSTIC_NONFATAL_ERROR
355#define TF_DIAGNOSTIC_NONFATAL_ERROR \
356 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
357 TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
358
359// Redefine the following three macros from DiagnosticLite to versions that will
360// accept the following sets of arguments:
361// * MACRO(const char *, ...)
362// * MACRO(const std::string &msg)
363// * MACRO(ENUM, const char *, ...)
364// * MACRO(ENUM, const std::string *msg)
365// * MACRO(TfDiagnosticInfo, ENUM, const char *, ...)
366// * MACRO(TfDiagnosticInfo, ENUM, const std::string *msg)
367
368#ifdef TF_WARN
369#undef TF_WARN
370#endif // TF_WARN
371#define TF_WARN(...) \
372 Tf_PostWarningHelper(TF_CALL_CONTEXT, __VA_ARGS__)
373
374#ifdef TF_STATUS
375#undef TF_STATUS
376#endif // TF_STATUS
377#define TF_STATUS(...) \
378 Tf_PostStatusHelper(TF_CALL_CONTEXT, __VA_ARGS__)
379
380#ifdef TF_ERROR
381#undef TF_ERROR
382#endif // TF_ERROR
383#define TF_ERROR(...) \
384 Tf_PostErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
385
386#ifdef TF_QUIET_ERROR
387#undef TF_QUIET_ERROR
388#endif // TF_ERROR
389#define TF_QUIET_ERROR(...) \
390 Tf_PostQuietlyErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
391
392// See documentation above.
393#define TF_VERIFY(cond, ...) \
394 (ARCH_LIKELY(cond) ? true : \
395 Tf_FailedVerifyHelper(TF_CALL_CONTEXT, # cond, \
396 Tf_VerifyStringFormat(__VA_ARGS__)))
397
398// Helpers for TF_VERIFY.
399TF_API bool
400Tf_FailedVerifyHelper(TfCallContext const &context,
401 char const *condition, char const *msg);
402
403// Helpers for TF_VERIFY.
404inline char const *
405Tf_VerifyStringFormat() { return nullptr; }
406TF_API char const *
407Tf_VerifyStringFormat(const char *format, ...) ARCH_PRINTF_FUNCTION(1, 2);
408
409#endif // !doxygen
410
411#endif // __cplusplus || doxygen
412
416void TfSetProgramNameForErrors(std::string const& programName);
417
422
424struct Tf_DiagnosticHelper {
425 Tf_DiagnosticHelper(TfCallContext const &context,
426 TfDiagnosticType type) :
427 _context(context),
428 _type(type)
429 {
430 }
431
432 TfCallContext const &GetContext() const { return _context; }
433 TfDiagnosticType GetType() const { return _type; }
434
435 TF_API void IssueError(std::string const &msg) const;
436 TF_API void IssueError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
437 TF_API void IssueFatalError(std::string const &msg) const;
438 TF_API void IssueFatalError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
439 TF_API void IssueWarning(std::string const &msg) const;
440 TF_API void IssueWarning(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
441 TF_API void IssueStatus(std::string const &msg) const;
442 TF_API void IssueStatus(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
443
444 private:
445 TfCallContext _context;
446 TfDiagnosticType _type;
447};
448
449#endif
450
459TF_API
461
463
464PXR_NAMESPACE_CLOSE_SCOPE
465
466#endif // PXR_BASE_TF_DIAGNOSTIC_H
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
Definition: attributes.h:34
Stripped down version of diagnostic.h that doesn't define std::string.
TfDiagnosticType
Enum describing various diagnostic conditions.
Define preprocessor function name macros.
void TfSetProgramNameForErrors(std::string const &programName)
Sets program name for reporting errors.
std::string TfGetProgramNameForErrors()
Returns currently set program info.
TF_API void TfInstallTerminateAndCrashHandlers()
(Re)install Tf's crash handler.
Compiler hints.
STL namespace.