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  return _context.GetFile();
59  }
60 
61  /// Return the source line number that the diagnostic message was posted
62  /// from.
63  size_t GetSourceLineNumber() const {
64  return _context.GetLine();
65  }
66 
67  /// Return the commentary string describing this diagnostic message.
68  std::string const &GetCommentary() const {
69  return _commentary;
70  }
71 
72  /// Return the source function that the diagnostic message was posted from.
73  std::string GetSourceFunction() const {
76  }
77 
78  /// Add to the commentary string describing this diagnostic message.
79  ///
80  /// Note: each string added to the commentary is separated from
81  /// the previous one with a newline. This means that
82  /// you the string \c s should \e not end with a newline. Thus,
83  /// \code
84  /// cout << e.GetCommentary() << "\n";
85  /// \endcode
86  /// always prints the entire commentary string as a newline
87  /// separated sequence of messages.
88  void AugmentCommentary(const std::string& s) {
89  if (_commentary.empty())
90  _commentary = s;
91  else {
92  _commentary += "\n";
93  _commentary += s;
94  }
95  }
96 
97  /// Return the diagnostic code posted.
99  return _code;
100  }
101 
102 
103  /// Return the diagnostic code posted as a string.
104  ///
105  /// If the enum value posted with the message has been registered
106  /// with \c TF_ADD_ENUM_NAME(), then \c GetDiagnosticCodeAsString() will
107  /// return the symbolic name of the enum.
108  ///
109  /// If the enum has not been registered, then code of the form
110  /// \code
111  /// TF_ERROR(PUCE).Post("is an ugly color");
112  /// \endcode
113  /// will still result in \c GetDiagnosticCodeAsString() returning the string
114  /// "PUCE"; however, code of the form
115  /// \code
116  /// MyErrorCode c = PUCE;
117  /// TF_ERROR(c).Post("is still ugly");
118  /// \endcode
119  /// will result in \c GetDiagnosticCodeAsString() returning the
120  /// (uninformative) string "c".
121  const std::string& GetDiagnosticCodeAsString() const {
122  return _codeString;
123  }
124 
125  /// Return a (possibly NULL) const pointer to the info object associated
126  /// with this message.
127  ///
128  /// If this message was posted without supplying an \c info argument to
129  /// Post(), e.g.
130  /// \code
131  /// TF_ERROR(SOME_CODE).Post("something went wrong");
132  /// \endcode
133  ///
134  /// then \c GetInfo() returns NULL. Otherwise, when info is supplied,
135  /// e.g.
136  /// \code
137  /// T myInfo = ...
138  /// TF_ERROR(SOME_CODE).Post("something went wrong")->SetInfo(myInfo);
139  /// \endcode
140  ///
141  /// then a const pointer to a copy of myInfo in the above example is
142  /// returned by GetInfo<T>(). If the type T doesn't match the held type
143  /// then GetInfo() returns NULL.
144  template <typename T>
145  const T* GetInfo() const {
146  return std::any_cast<T>(&_info);
147  }
148 
149  /// Set the info object associated with this diagnostic message.
150  /// \see GetInfo()
152  _info = any;
153  }
154 
155  /// Return true if the message was posted via \c PostQuietly().
156  ///
157  /// Notices sent from \c PostQuietly() are indicating that an immediate
158  /// printout of the error is not desirable, because someone higher up on
159  /// the stack may actually handle this error. This is rare, but it does
160  /// happen on occasion.
161  bool GetQuiet() const {
162  return _quiet;
163  }
164 
165  /// Return true if this diagnostic's code is a fatal code.
166  TF_API
167  bool IsFatal() const;
168 
169  /// Return true if this diagnostic's code is either a fatal or nonfatal
170  /// coding error.
171  TF_API
172  bool IsCodingError() const;
173 
174  /// Construct an instance.
175  TF_API
176  TfDiagnosticBase(TfEnum code, char const *codeString,
177  TfCallContext const &context,
178  const std::string& commentary,
179  TfDiagnosticInfo info, bool quiet);
180 
181 protected:
183 
184  std::string _commentary;
186  std::string _codeString;
188  size_t _serial = 0;
189  bool _quiet = false;
190 
191  friend class TfDiagnosticMgr;
192  friend class TfErrorTransport;
193  friend class TfErrorMark;
194 };
195 
197 
198 #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
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
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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.