HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
diagnosticBase.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_BASE_H
8 #define PXR_BASE_TF_DIAGNOSTIC_BASE_H
9 
10 /// \file tf/diagnosticBase.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/tf/api.h"
15 #include "pxr/base/tf/enum.h"
16 #include "pxr/base/tf/refBase.h"
18 #include "pxr/base/tf/weakPtr.h"
19 
20 #include "pxr/base/arch/inttypes.h"
22 #include "pxr/base/arch/function.h"
23 
24 #include <any>
25 #include <cstdarg>
26 #include <string>
27 
29 
31 
32 class TfDiagnosticMgr;
33 
34 /// \class TfDiagnosticBase
35 /// \ingroup group_tf_TfError
36 ///
37 /// Represents the base class of an object representing a diagnostic message.
38 ///
39 /// This forms the base class for specific types associated with errors,
40 /// warnings and status messages. It associates a diagnostic code (which is an
41 /// enum value) with the message. It can also hold on to arbitrary information
42 /// associated with the message in a TfDiagnosticInfo.
43 ///
44 /// See \ref page_tf_TfError in the C++ API reference for a detailed example.
45 /// For a description of how to post an error, warning or a status message see
46 /// \c TF_ERROR(), \c TF_WARN and \c TF_STATUS also in the C++ API reference.
47 ///
49 public:
50 
51  /// Return the call context where the message was issued.
52  const TfCallContext &GetContext() const {
53  return _context;
54  }
55 
56  /// Return the source file name that the diagnostic message was posted from.
57  std::string GetSourceFileName() const {
58  if (_context.GetFile()) {
59  return _context.GetFile();
60  }
61  return std::string();
62  }
63 
64  /// Return the source line number that the diagnostic message was posted
65  /// from.
66  size_t GetSourceLineNumber() const {
67  return _context.GetLine();
68  }
69 
70  /// Return the commentary string describing this diagnostic message.
71  std::string const &GetCommentary() const {
72  return _commentary;
73  }
74 
75  /// Return the source function that the diagnostic message was posted from.
76  std::string GetSourceFunction() const {
80  }
81  return std::string();
82  }
83 
84  /// Add to the commentary string describing this diagnostic message.
85  ///
86  /// Note: each string added to the commentary is separated from
87  /// the previous one with a newline. This means that
88  /// you the string \c s should \e not end with a newline. Thus,
89  /// \code
90  /// cout << e.GetCommentary() << "\n";
91  /// \endcode
92  /// always prints the entire commentary string as a newline
93  /// separated sequence of messages.
94  void AugmentCommentary(const std::string& s) {
95  if (_commentary.empty())
96  _commentary = s;
97  else {
98  _commentary += "\n";
99  _commentary += s;
100  }
101  }
102 
103  /// Return the diagnostic code posted.
105  return _code;
106  }
107 
108 
109  /// Return the diagnostic code posted as a string.
110  ///
111  /// If the enum value posted with the message has been registered
112  /// with \c TF_ADD_ENUM_NAME(), then \c GetDiagnosticCodeAsString() will
113  /// return the symbolic name of the enum.
114  ///
115  /// If the enum has not been registered, then code of the form
116  /// \code
117  /// TF_ERROR(PUCE).Post("is an ugly color");
118  /// \endcode
119  /// will still result in \c GetDiagnosticCodeAsString() returning the string
120  /// "PUCE"; however, code of the form
121  /// \code
122  /// MyErrorCode c = PUCE;
123  /// TF_ERROR(c).Post("is still ugly");
124  /// \endcode
125  /// will result in \c GetDiagnosticCodeAsString() returning the
126  /// (uninformative) string "c".
127  const std::string& GetDiagnosticCodeAsString() const {
128  return _codeString;
129  }
130 
131  /// Return a (possibly NULL) const pointer to the info object associated
132  /// with this message.
133  ///
134  /// If this message was posted without supplying an \c info argument to
135  /// Post(), e.g.
136  /// \code
137  /// TF_ERROR(SOME_CODE).Post("something went wrong");
138  /// \endcode
139  ///
140  /// then \c GetInfo() returns NULL. Otherwise, when info is supplied,
141  /// e.g.
142  /// \code
143  /// T myInfo = ...
144  /// TF_ERROR(SOME_CODE).Post("something went wrong")->SetInfo(myInfo);
145  /// \endcode
146  ///
147  /// then a const pointer to a copy of myInfo in the above example is
148  /// returned by GetInfo<T>(). If the type T doesn't match the held type
149  /// then GetInfo() returns NULL.
150  template <typename T>
151  const T* GetInfo() const {
152  return std::any_cast<T>(&_info);
153  }
154 
155  /// Set the info object associated with this diagnostic message.
156  /// \see GetInfo()
158  _info = any;
159  }
160 
161  /// Return true if the message was posted via \c PostQuietly().
162  ///
163  /// Notices sent from \c PostQuietly() are indicating that an immediate
164  /// printout of the error is not desirable, because someone higher up on
165  /// the stack may actually handle this error. This is rare, but it does
166  /// happen on occasion.
167  bool GetQuiet() const {
168  return _quiet;
169  }
170 
171  /// Return true if this diagnostic's code is a fatal code.
172  TF_API
173  bool IsFatal() const;
174 
175  /// Return true if this diagnostic's code is either a fatal or nonfatal
176  /// coding error.
177  TF_API
178  bool IsCodingError() const;
179 
180  /// Construct an instance.
181  TF_API
182  TfDiagnosticBase(TfEnum code, char const *codeString,
183  TfCallContext const &context,
184  const std::string& commentary,
185  TfDiagnosticInfo info, bool quiet);
186 
187 protected:
189 
190  std::string _commentary;
192  std::string _codeString;
194  size_t _serial = 0;
195  bool _quiet = false;
196 
197  friend class TfDiagnosticMgr;
198  friend class TfErrorTransport;
199  friend class TfErrorMark;
200 };
201 
203 
204 #endif // PXR_BASE_TF_DIAGNOSTIC_BASE_H
std::string GetSourceFileName() const
Return the source file name that the diagnostic message was posted from.
TF_API TfDiagnosticBase(TfEnum code, char const *codeString, TfCallContext const &context, const std::string &commentary, TfDiagnosticInfo info, bool quiet)
Construct an instance.
#define TF_API
Definition: api.h:23
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
char const * GetPrettyFunction() const
Definition: callContext.h:67
GLdouble s
Definition: glad.h:3009
const TfCallContext & GetContext() const
Return the call context where the message was issued.
Definition: enum.h:119
PXR_NAMESPACE_OPEN_SCOPE typedef std::any TfDiagnosticInfo
size_t GetLine() const
Definition: callContext.h:63
TfCallContext _context
const std::string & GetDiagnosticCodeAsString() const
const T * GetInfo() const
size_t GetSourceLineNumber() const
TfEnum GetDiagnosticCode() const
Return the diagnostic code posted.
TF_API bool IsCodingError() const
bool any(const vbool4 &v)
Definition: simd.h:3600
char const * GetFile() const
Definition: callContext.h:55
TF_API bool IsFatal() const
Return true if this diagnostic's code is a fatal code.
std::string _codeString
std::string GetSourceFunction() const
Return the source function that the diagnostic message was posted from.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool GetQuiet() const
void AugmentCommentary(const std::string &s)
char const * GetFunction() const
Definition: callContext.h:59
TfDiagnosticInfo _info
PXR_NAMESPACE_OPEN_SCOPE ARCH_API std::string ArchGetPrettierFunctionName(const std::string &function, const std::string &prettyFunction)
void SetInfo(TfDiagnosticInfo any)
std::string _commentary
std::string const & GetCommentary() const
Return the commentary string describing this diagnostic message.