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 TF_DIAGNOSTIC_H
25 #define TF_DIAGNOSTIC_H
26 
37 
38 #include "pxr/pxr.h"
39 #include "pxr/base/arch/function.h"
40 #include "pxr/base/tf/diagnosticLite.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 
72 #define TF_ERROR(...)
73 
87 #define TF_CODING_ERROR(fmt, args)
88 
103 #define TF_RUNTIME_ERROR(fmt, args)
104 
111 #define TF_FATAL_ERROR(fmt, args)
112 
153 #define TF_WARN(...)
154 
195 #define TF_STATUS(...)
196 
215 #define TF_AXIOM(cond)
216 
227 #define TF_DEV_AXIOM(cond)
228 
289 #define TF_VERIFY(cond [, format, ...])
290 
291 #endif /* defined(doxygen) */
292 
293 //
294 // The rest of this is seen by a regular compile (or doxygen).
295 //
296 
297 #if defined(__cplusplus) || defined(doxygen)
298 
315 #define TF_FUNC_NAME() \
316  ArchGetPrettierFunctionName(__ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__)
317 
318 void Tf_TerminateHandler();
319 
320 #if !defined(doxygen)
321 
322 // Redefine these macros from DiagnosticLite to versions that will accept
323 // either string or printf-like args.
324 
325 #ifdef TF_CODING_ERROR
326 #undef TF_CODING_ERROR
327 #endif
328 #define TF_CODING_ERROR(...) \
329  Tf_PostErrorHelper(TF_CALL_CONTEXT, \
330  TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
331 
332 #ifdef TF_FATAL_CODING_ERROR
333 #undef TF_FATAL_CODING_ERROR
334 #endif
335 #define TF_FATAL_CODING_ERROR \
336  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
337  TF_DIAGNOSTIC_CODING_ERROR_TYPE).IssueFatalError
338 
339 
340 #ifdef TF_CODING_WARNING
341 #undef TF_CODING_WARNING
342 #endif
343 #define TF_CODING_WARNING(...) \
344  Tf_PostWarningHelper(TF_CALL_CONTEXT, \
345  TF_DIAGNOSTIC_CODING_ERROR_TYPE, __VA_ARGS__)
346 
347 #ifdef TF_DIAGNOSTIC_WARNING
348 #undef TF_DIAGNOSTIC_WARNING
349 #endif
350 #define TF_DIAGNOSTIC_WARNING \
351  Tf_DiagnosticHelper(TF_CALL_CONTEXT.Hide(), \
352  TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
353 
354 #ifdef TF_RUNTIME_ERROR
355 #undef TF_RUNTIME_ERROR
356 #endif // TF_RUNTIME_ERROR
357 #define TF_RUNTIME_ERROR(...) \
358  Tf_PostErrorHelper(TF_CALL_CONTEXT, \
359  TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE, __VA_ARGS__)
360 
361 #ifdef TF_FATAL_ERROR
362 #undef TF_FATAL_ERROR
363 #endif // TF_FATAL_ERROR
364 #define TF_FATAL_ERROR \
365  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
366  TF_DIAGNOSTIC_FATAL_ERROR_TYPE).IssueFatalError
367 
368 #ifdef TF_DIAGNOSTIC_FATAL_ERROR
369 #undef TF_DIAGNOSTIC_FATAL_ERROR
370 #endif // TF_DIAGNOSTIC_FATAL_ERROR
371 #define TF_DIAGNOSTIC_FATAL_ERROR \
372  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
373  TF_DIAGNOSTIC_RUNTIME_ERROR_TYPE).IssueFatalError
374 
375 #ifdef TF_DIAGNOSTIC_NONFATAL_ERROR
376 #undef TF_DIAGNOSTIC_NONFATAL_ERROR
377 #endif // TF_DIAGNOSTIC_NONFATAL_ERROR
378 #define TF_DIAGNOSTIC_NONFATAL_ERROR \
379  Tf_DiagnosticHelper(TF_CALL_CONTEXT, \
380  TF_DIAGNOSTIC_WARNING_TYPE).IssueWarning
381 
382 // Redefine the following three macros from DiagnosticLite to versions that will
383 // accept the following sets of arguments:
384 // * MACRO(const char *, ...)
385 // * MACRO(const std::string &msg)
386 // * MACRO(ENUM, const char *, ...)
387 // * MACRO(ENUM, const std::string *msg)
388 // * MACRO(TfDiagnosticInfo, ENUM, const char *, ...)
389 // * MACRO(TfDiagnosticInfo, ENUM, const std::string *msg)
390 
391 #ifdef TF_WARN
392 #undef TF_WARN
393 #endif // TF_WARN
394 #define TF_WARN(...) \
395  Tf_PostWarningHelper(TF_CALL_CONTEXT, __VA_ARGS__)
396 
397 #ifdef TF_STATUS
398 #undef TF_STATUS
399 #endif // TF_STATUS
400 #define TF_STATUS(...) \
401  Tf_PostStatusHelper(TF_CALL_CONTEXT, __VA_ARGS__)
402 
403 #ifdef TF_ERROR
404 #undef TF_ERROR
405 #endif // TF_ERROR
406 #define TF_ERROR(...) \
407  Tf_PostErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
408 
409 #ifdef TF_QUIET_ERROR
410 #undef TF_QUIET_ERROR
411 #endif // TF_ERROR
412 #define TF_QUIET_ERROR(...) \
413  Tf_PostQuietlyErrorHelper(TF_CALL_CONTEXT, __VA_ARGS__)
414 
415 // See documentation above.
416 #define TF_VERIFY(cond, ...) \
417  (ARCH_LIKELY(cond) ? true : \
418  Tf_FailedVerifyHelper(TF_CALL_CONTEXT, # cond, \
419  Tf_VerifyStringFormat(__VA_ARGS__)))
420 
421 // Helpers for TF_VERIFY.
422 TF_API bool
423 Tf_FailedVerifyHelper(TfCallContext const &context,
424  char const *condition, char const *msg);
425 
426 // Helpers for TF_VERIFY.
427 inline char const *
428 Tf_VerifyStringFormat() { return nullptr; }
429 TF_API char const *
430 Tf_VerifyStringFormat(const char *format, ...) ARCH_PRINTF_FUNCTION(1, 2);
431 
432 #endif // !doxygen
433 
434 #endif // __cplusplus || doxygen
435 
439 void TfSetProgramNameForErrors(std::string const& programName);
440 
444 std::string TfGetProgramNameForErrors();
445 
447 struct Tf_DiagnosticHelper {
448  Tf_DiagnosticHelper(TfCallContext const &context,
449  TfDiagnosticType type) :
450  _context(context),
451  _type(type)
452  {
453  }
454 
455  TfCallContext const &GetContext() const { return _context; }
456  TfDiagnosticType GetType() const { return _type; }
457 
458  TF_API void IssueError(std::string const &msg) const;
459  TF_API void IssueError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
460  TF_API void IssueFatalError(std::string const &msg) const;
461  TF_API void IssueFatalError(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
462  TF_API void IssueWarning(std::string const &msg) const;
463  TF_API void IssueWarning(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
464  TF_API void IssueStatus(std::string const &msg) const;
465  TF_API void IssueStatus(char const *fmt, ...) const ARCH_PRINTF_FUNCTION(2,3);
466 
467  private:
468  TfCallContext _context;
469  TfDiagnosticType _type;
470 };
471 
472 #endif
473 
482 TF_API
484 
486 
487 PXR_NAMESPACE_CLOSE_SCOPE
488 
489 #endif // TF_DIAGNOSTIC_H
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.