All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
diagnostic.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_DIAGNOSTIC_H
25 #define PXR_BASE_TF_DIAGNOSTIC_H
26 
37 
38 #include "pxr/pxr.h"
39 #include "pxr/base/arch/function.h"
41 #include "pxr/base/tf/api.h"
42 
43 #if defined(__cplusplus) || defined (doxygen)
44 
45 #include "pxr/base/arch/hints.h"
46 #include "pxr/base/tf/diagnosticHelper.h"
47 
48 #include <stddef.h>
49 #include <stdarg.h>
50 #include <string>
51 
52 PXR_NAMESPACE_OPEN_SCOPE
53 
54 // Note: diagnosticLite.h defines the various macros, but we'll document
55 // them here. The following block is only for doxygen, not seen by a real
56 // compile. To see the actual macro definition, look in diagnosticLite.h.
57 
58 #if defined(doxygen)
59 
62 
71 #define TF_ERROR(...)
72 
85 #define TF_CODING_ERROR(fmt, args)
86 
100 #define TF_RUNTIME_ERROR(fmt, args)
101 
108 #define TF_FATAL_ERROR(fmt, args)
109 
149 #define TF_WARN(...)
150 
190 #define TF_STATUS(...)
191 
210 #define TF_AXIOM(cond)
211 
222 #define TF_DEV_AXIOM(cond)
223 
283 #define TF_VERIFY(cond [, format, ...])
284 
285 #endif /* defined(doxygen) */
286 
287 //
288 // The rest of this is seen by a regular compile (or doxygen).
289 //
290 
291 #if defined(__cplusplus) || defined(doxygen)
292 
309 #define TF_FUNC_NAME() \
310  ArchGetPrettierFunctionName(__ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__)
311 
312 void Tf_TerminateHandler();
313 
314 #if !defined(doxygen)
315 
316 // Redefine these macros from DiagnosticLite to versions that will accept
317 // either string or printf-like args.
318 
319 #ifdef TF_CODING_ERROR
320 #undef TF_CODING_ERROR
321 #endif
322 #define TF_CODING_ERROR(...) \
323  Tf_PostErrorHelper(TF_CALL_CONTEXT, \
324  TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
325 
326 #ifdef TF_FATAL_CODING_ERROR
327 #undef TF_FATAL_CODING_ERROR
328 #endif
329 #define TF_FATAL_CODING_ERROR \
330  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
331  TF_DIAGNOSTIC_CODING_ERROR_TYPE).IssueFatalError
332 
333 
334 #ifdef TF_CODING_WARNING
335 #undef TF_CODING_WARNING
336 #endif
337 #define TF_CODING_WARNING(...) \
338  Tf_PostWarningHelper(TF_CALL_CONTEXT, \
339  TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
340 
341 #ifdef TF_DIAGNOSTIC_WARNING
342 #undef TF_DIAGNOSTIC_WARNING
343 #endif
344 #define TF_DIAGNOSTIC_WARNING \
345  Tf_DiagnosticHelper(TF_CALL_CONTEXT.Hide(), \
346  TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
347 
348 #ifdef TF_RUNTIME_ERROR
349 #undef TF_RUNTIME_ERROR
350 #endif // TF_RUNTIME_ERROR
351 #define TF_RUNTIME_ERROR(...) \
352  Tf_PostErrorHelper(TF_CALL_CONTEXT, \
353  TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE, __VA_ARGS__)
354 
355 #ifdef TF_FATAL_ERROR
356 #undef TF_FATAL_ERROR
357 #endif // TF_FATAL_ERROR
358 #define TF_FATAL_ERROR \
359  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
360  TF_DIAGNOSTIC_FATAL_ERROR_TYPE).IssueFatalError
361 
362 #ifdef TF_DIAGNOSTIC_FATAL_ERROR
363 #undef TF_DIAGNOSTIC_FATAL_ERROR
364 #endif // TF_DIAGNOSTIC_FATAL_ERROR
365 #define TF_DIAGNOSTIC_FATAL_ERROR \
366  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
367  TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE).IssueFatalError
368 
369 #ifdef TF_DIAGNOSTIC_NONFATAL_ERROR
370 #undef TF_DIAGNOSTIC_NONFATAL_ERROR
371 #endif // TF_DIAGNOSTIC_NONFATAL_ERROR
372 #define TF_DIAGNOSTIC_NONFATAL_ERROR \
373  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
374  TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
375 
376 // Redefine the following three macros from DiagnosticLite to versions that will
377 // accept the following sets of arguments:
378 // * MACRO(const char *, ...)
379 // * MACRO(const std::string &msg)
380 // * MACRO(ENUM, const char *, ...)
381 // * MACRO(ENUM, const std::string *msg)
382 // * MACRO(TfDiagnosticInfo, ENUM, const char *, ...)
383 // * MACRO(TfDiagnosticInfo, ENUM, const std::string *msg)
384 
385 #ifdef TF_WARN
386 #undef TF_WARN
387 #endif // TF_WARN
388 #define TF_WARN(...) \
389  Tf_PostWarningHelper(TF_CALL_CONTEXT, __VA_ARGS__)
390 
391 #ifdef TF_STATUS
392 #undef TF_STATUS
393 #endif // TF_STATUS
394 #define TF_STATUS(...) \
395  Tf_PostStatusHelper(TF_CALL_CONTEXT, __VA_ARGS__)
396 
397 #ifdef TF_ERROR
398 #undef TF_ERROR
399 #endif // TF_ERROR
400 #define TF_ERROR(...) \
401  Tf_PostErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
402 
403 #ifdef TF_QUIET_ERROR
404 #undef TF_QUIET_ERROR
405 #endif // TF_ERROR
406 #define TF_QUIET_ERROR(...) \
407  Tf_PostQuietlyErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
408 
409 // See documentation above.
410 #define TF_VERIFY(cond, ...) \
411  (ARCH_LIKELY(cond) ? true : \
412  Tf_FailedVerifyHelper(TF_CALL_CONTEXT, # cond, \
413  Tf_VerifyStringFormat(__VA_ARGS__)))
414 
415 // Helpers for TF_VERIFY.
416 TF_API bool
417 Tf_FailedVerifyHelper(TfCallContext const &context,
418  char const *condition, char const *msg);
419 
420 // Helpers for TF_VERIFY.
421 inline char const *
422 Tf_VerifyStringFormat() { return nullptr; }
423 TF_API char const *
424 Tf_VerifyStringFormat(const char *format, ...) ARCH_PRINTF_FUNCTION(1, 2);
425 
426 #endif // !doxygen
427 
428 #endif // __cplusplus || doxygen
429 
433 void TfSetProgramNameForErrors(std::string const& programName);
434 
438 std::string TfGetProgramNameForErrors();
439 
441 struct Tf_DiagnosticHelper {
442  Tf_DiagnosticHelper(TfCallContext const &context,
443  TfDiagnosticType type) :
444  _context(context),
445  _type(type)
446  {
447  }
448 
449  TfCallContext const &GetContext() const { return _context; }
450  TfDiagnosticType GetType() const { return _type; }
451 
452  TF_API void IssueError(std::string const &msg) const;
453  TF_API void IssueError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
454  TF_API void IssueFatalError(std::string const &msg) const;
455  TF_API void IssueFatalError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
456  TF_API void IssueWarning(std::string const &msg) const;
457  TF_API void IssueWarning(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
458  TF_API void IssueStatus(std::string const &msg) const;
459  TF_API void IssueStatus(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
460 
461  private:
462  TfCallContext _context;
463  TfDiagnosticType _type;
464 };
465 
466 #endif
467 
476 TF_API
478 
480 
481 PXR_NAMESPACE_CLOSE_SCOPE
482 
483 #endif // PXR_BASE_TF_DIAGNOSTIC_H
Define preprocessor function name macros.
Compiler hints.
TF_API void TfInstallTerminateAndCrashHandlers()
(Re)install Tf&#39;s crash handler.
void TfSetProgramNameForErrors(std::string const &programName)
Sets program name for reporting errors.
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
Definition: attributes.h:51
TfDiagnosticType
Enum describing various diagnostic conditions.
std::string TfGetProgramNameForErrors()
Returns currently set program info.
Stripped down version of diagnostic.h that doesn&#39;t define std::string.