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