Loading...
Searching...
No Matches
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"
25#include "pxr/base/tf/api.h"
26
27#if defined(__cplusplus) || defined (doxygen)
28
29#include "pxr/base/arch/hints.h"
30#include "pxr/base/tf/diagnosticHelper.h"
31
32#include <stddef.h>
33#include <stdarg.h>
34#include <string>
35
36PXR_NAMESPACE_OPEN_SCOPE
37
38// Note: diagnosticLite.h defines the various macros, but we'll document
39// them here. The following block is only for doxygen, not seen by a real
40// compile. To see the actual macro definition, look in diagnosticLite.h.
41
42#if defined(doxygen)
43
46
55#define TF_ERROR(...)
56
69#define TF_CODING_ERROR(fmt, args)
70
84#define TF_RUNTIME_ERROR(fmt, args)
85
92#define TF_FATAL_ERROR(fmt, args)
93
133#define TF_WARN(...)
134
174#define TF_STATUS(...)
175
194#define TF_AXIOM(cond)
195
206#define TF_DEV_AXIOM(cond)
207
267#define TF_VERIFY(cond [, format, ...])
268
269#endif /* defined(doxygen) */
270
271//
272// The rest of this is seen by a regular compile (or doxygen).
273//
274
275#if defined(__cplusplus) || defined(doxygen)
276
293#define TF_FUNC_NAME() \
294 ([](char const *f, char const *pf) -> TfEternalString { \
295 static TfEternalString const n = \
296 TfEternalString::Immortalize(ArchGetPrettierFunctionName(f, pf)); \
297 return n; \
298 }(__ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__))
299
300
301void Tf_TerminateHandler();
302
303#if !defined(doxygen)
304
305// Redefine these macros from DiagnosticLite to versions that will accept
306// either string or printf-like args.
307
308#ifdef TF_CODING_ERROR
309#undef TF_CODING_ERROR
310#endif
311#define TF_CODING_ERROR(...) \
312 Tf_PostErrorHelper(TF_CALL_CONTEXT, \
313 TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
314
315#ifdef TF_FATAL_CODING_ERROR
316#undef TF_FATAL_CODING_ERROR
317#endif
318#define TF_FATAL_CODING_ERROR \
319 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
320 TF_DIAGNOSTIC_CODING_ERROR_TYPE).IssueFatalError
321
322
323#ifdef TF_CODING_WARNING
324#undef TF_CODING_WARNING
325#endif
326#define TF_CODING_WARNING(...) \
327 Tf_PostWarningHelper(TF_CALL_CONTEXT, \
328 TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
329
330#ifdef TF_DIAGNOSTIC_WARNING
331#undef TF_DIAGNOSTIC_WARNING
332#endif
333#define TF_DIAGNOSTIC_WARNING \
334 Tf_DiagnosticHelper(TF_CALL_CONTEXT.Hide(), \
335 TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
336
337#ifdef TF_RUNTIME_ERROR
338#undef TF_RUNTIME_ERROR
339#endif // TF_RUNTIME_ERROR
340#define TF_RUNTIME_ERROR(...) \
341 Tf_PostErrorHelper(TF_CALL_CONTEXT, \
342 TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE, __VA_ARGS__)
343
344#ifdef TF_FATAL_ERROR
345#undef TF_FATAL_ERROR
346#endif // TF_FATAL_ERROR
347#define TF_FATAL_ERROR \
348 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
349 TF_DIAGNOSTIC_FATAL_ERROR_TYPE).IssueFatalError
350
351#ifdef TF_DIAGNOSTIC_FATAL_ERROR
352#undef TF_DIAGNOSTIC_FATAL_ERROR
353#endif // TF_DIAGNOSTIC_FATAL_ERROR
354#define TF_DIAGNOSTIC_FATAL_ERROR \
355 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
356 TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE).IssueFatalError
357
358#ifdef TF_DIAGNOSTIC_NONFATAL_ERROR
359#undef TF_DIAGNOSTIC_NONFATAL_ERROR
360#endif // TF_DIAGNOSTIC_NONFATAL_ERROR
361#define TF_DIAGNOSTIC_NONFATAL_ERROR \
362 Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
363 TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
364
365// Redefine the following three macros from DiagnosticLite to versions that will
366// accept the following sets of arguments:
367// * MACRO(const char *, ...)
368// * MACRO(const std::string &msg)
369// * MACRO(ENUM, const char *, ...)
370// * MACRO(ENUM, const std::string *msg)
371// * MACRO(TfDiagnosticInfo, ENUM, const char *, ...)
372// * MACRO(TfDiagnosticInfo, ENUM, const std::string *msg)
373
374#ifdef TF_WARN
375#undef TF_WARN
376#endif // TF_WARN
377#define TF_WARN(...) \
378 Tf_PostWarningHelper(TF_CALL_CONTEXT, __VA_ARGS__)
379
380#ifdef TF_STATUS
381#undef TF_STATUS
382#endif // TF_STATUS
383#define TF_STATUS(...) \
384 Tf_PostStatusHelper(TF_CALL_CONTEXT, __VA_ARGS__)
385
386#ifdef TF_ERROR
387#undef TF_ERROR
388#endif // TF_ERROR
389#define TF_ERROR(...) \
390 Tf_PostErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
391
392#ifdef TF_QUIET_ERROR
393#undef TF_QUIET_ERROR
394#endif // TF_ERROR
395#define TF_QUIET_ERROR(...) \
396 Tf_PostQuietlyErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
397
398// See documentation above.
399#define TF_VERIFY(cond, ...) \
400 (ARCH_LIKELY(cond) ? true : \
401 Tf_FailedVerifyHelper(TF_CALL_CONTEXT, # cond, \
402 Tf_VerifyStringFormat(__VA_ARGS__)))
403
404// Helpers for TF_VERIFY.
405TF_API bool
406Tf_FailedVerifyHelper(TfCallContext const &context,
407 char const *condition, char const *msg);
408
409// Helpers for TF_VERIFY.
410inline char const *
411Tf_VerifyStringFormat() { return nullptr; }
412TF_API char const *
413Tf_VerifyStringFormat(const char *format, ...) ARCH_PRINTF_FUNCTION(1, 2);
414
415#endif // !doxygen
416
417#endif // __cplusplus || doxygen
418
422void TfSetProgramNameForErrors(std::string const& programName);
423
428
430struct Tf_DiagnosticHelper {
431 Tf_DiagnosticHelper(TfCallContext const &context,
432 TfDiagnosticType type) :
433 _context(context),
434 _type(type)
435 {
436 }
437
438 TfCallContext const &GetContext() const { return _context; }
439 TfDiagnosticType GetType() const { return _type; }
440
441 TF_API void IssueError(std::string const &msg) const;
442 TF_API void IssueError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
443 TF_API void IssueFatalError(std::string const &msg) const;
444 TF_API void IssueFatalError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
445 TF_API void IssueWarning(std::string const &msg) const;
446 TF_API void IssueWarning(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
447 TF_API void IssueStatus(std::string const &msg) const;
448 TF_API void IssueStatus(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
449
450 private:
451 TfCallContext _context;
452 TfDiagnosticType _type;
453};
454
455#endif
456
465TF_API
467
469
470PXR_NAMESPACE_CLOSE_SCOPE
471
472#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:36
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.