HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ErrorManager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_ErrorManager.h (Utility library)
7  *
8  * COMMENTS:
9  * This class implements an error manager which will
10  * manage a list of error messages.
11  * NOTE: It assumes ownership of the errors that it is givin.
12  *
13  */
14 
15 #ifndef __UT_ErrorManager_h__
16 #define __UT_ErrorManager_h__
17 
18 #include "UT_API.h"
19 #include "UT_Array.h"
20 #include "UT_Error.h"
21 #include "UT_Function.h"
22 #include "UT_NonCopyable.h"
23 #include <SYS/SYS_Deprecated.h>
24 #include <SYS/SYS_Types.h>
25 
26 class UT_SourceLocation;
27 class UT_String;
28 class UT_StringHolder;
29 
30 
32 
34 {
35  enum {
36  FLAG_ERRORS_READ = 0x01,
37  FLAG_SCAN_NEEDED = 0x02,
38  FLAG_DISABLED = 0x04,
39  FLAG_DEADLOCK = 0x08,
40  FLAG_NOLOG = 0x10
41  };
42 
43 public:
45  ~UT_ErrorManager();
46 
47  UT_ErrorManager(const UT_ErrorManager &) = delete;
48  UT_ErrorManager &operator=(const UT_ErrorManager &) = delete;
49 
50  UT_ErrorSeverity addMessage (const char *type, int code, const char *msg=0,
51  const UT_SourceLocation *loc=0);
52  UT_ErrorSeverity addPrompt (const char *type, int code, const char *msg=0,
53  const UT_SourceLocation *loc=0);
54  UT_ErrorSeverity addWarning (const char *type, int code, const char *msg=0,
55  const UT_SourceLocation *loc=0);
56  UT_ErrorSeverity addError (const char *type, int code, const char *msg=0,
57  const UT_SourceLocation *loc=0);
58  UT_ErrorSeverity addFatal (const char *type, int code, const char *msg=0,
59  const UT_SourceLocation *loc=0);
60 
61  UT_ErrorSeverity sopAddMessage (int code, const char *msg=0,
62  const UT_SourceLocation *loc=0)
63  { return addMessage("SOP", code, msg, loc); }
64  UT_ErrorSeverity sopAddPrompt (int code, const char *msg=0,
65  const UT_SourceLocation *loc=0)
66  { return addPrompt("SOP", code, msg, loc); }
67  UT_ErrorSeverity sopAddWarning (int code, const char *msg=0,
68  const UT_SourceLocation *loc=0)
69  { return addWarning("SOP", code, msg, loc); }
70  UT_ErrorSeverity sopAddError (int code, const char *msg=0,
71  const UT_SourceLocation *loc=0)
72  { return addError("SOP", code, msg, loc); }
73  UT_ErrorSeverity sopAddFatal (int code, const char *msg=0,
74  const UT_SourceLocation *loc=0)
75  { return addFatal("SOP", code, msg, loc); }
76 
77  UT_ErrorSeverity systemError(const char *msg=0);
78  UT_ErrorSeverity systemWarning(const char *msg=0);
79 
80  UT_ErrorSeverity commonWarning(UT_CommonErrorCode what, const char *msg=0);
81  UT_ErrorSeverity commonError(UT_CommonErrorCode what, const char *msg=0);
82  UT_ErrorSeverity commonMessage(UT_CommonErrorCode what, const char *msg=0);
83 
84  UT_ErrorSeverity addDeadlockError();
85 
86  UT_ErrorSeverity addGeneric(const char *type, int code, const char *msg,
87  UT_ErrorSeverity sev,
88  const UT_SourceLocation *loc=0);
89 
90  UT_ErrorSeverity addGeneric(UT_Error *error);
91 
92  void clearAndDestroyErrors();
93 
94  bool isEmpty() const { return myList.entries() <= 0; }
95  int getNumErrors() const { return myList.entries(); }
96  const UT_Error &getError(int index) const { return *myList(index); }
97 
98  int stealErrors(UT_ErrorManager &victim,
99  int rangestart = 0, int rangeend = -1,
101  bool borrow_only = false);
102 
103  int removeError(int index);
104  int findError(const char *type, int code) const;
105 
106 // passing in a severity of NONE will return all errors
107 //
108  int getErrorMessages(UT_String &messages,
110  bool use_headers = true);
111 
112 // Returns the worst error state
113 //
115  {
116  if( getNeedScan() )
117  computeSeverity();
118  return mySeverity;
119  }
120 // True if addError was called
121  bool hasError() { return getSeverity() >= UT_ERROR_ABORT; }
122 
123  // Clamp all errors to the maximum severity specified and recompute
124  // severity
125  void clampSeverity(UT_ErrorSeverity severity);
126 
127 // This is for backward compatibility: use "getSeverity" !!!
128  UT_ErrorSeverity getErrorState() { return getSeverity(); }
129 
130  void pruneDuplicates();
131 
132  bool isDisabled() const
133  {
134  return ((myFlags & FLAG_DISABLED) != 0);
135  }
136  void setDisabled(bool f)
137  {
138  if (f)
139  myFlags |= FLAG_DISABLED;
140  else
141  myFlags &= ~FLAG_DISABLED;
142  }
143  bool isLogging() const
144  {
145  return (myFlags & FLAG_NOLOG) == 0;
146  }
147  // In some rare cases we want to be able to add errors to the manager but
148  // not log them.
149  void setErrorLogging(bool f)
150  {
151  if (!f)
152  myFlags |= FLAG_NOLOG;
153  else
154  myFlags &= ~FLAG_NOLOG;
155  }
156 
158  {
159  if (sev > mySeverity)
160  mySeverity = sev;
161  return mySeverity;
162  }
163 
164  // You typically do not need to call this method unless
165  // you want to restore the error severity back to a
166  // level that matches the errors recorded in the log.
168  {
169  setNeedScan(true);
170  computeSeverity();
171  }
172 
173  bool hasDeadlockError() const
174  {
175  return ((myFlags & FLAG_DEADLOCK) != 0);
176  }
177 
179  UT_Function<UT_StringHolder()> context_fn)
180  { myUniversalLoggingContextFunction = context_fn; }
181 
182  int64 getMemoryUsage(bool inclusive) const
183  {
184  int64 mem = inclusive ? sizeof(*this) : 0;
185  mem += myList.getMemoryUsage(false);
186  for (exint i = 0; i < myList.entries(); ++i)
187  mem += myList(i)->getMemoryUsage(true);
188  return mem;
189  }
190 
191  /// Returns a timestamp that is incremented every time an
192  /// error of ABORT or higher is logged.
193  static int getErrorManagerTimestamp();
194 
195  /// Returns an integer that is incremented every time an error
196  /// is logged.
197  exint getVersion() const { return myVersion; }
198 
199  class Scope;
200 
201  UT_ErrorList::const_iterator begin() const { return myList.begin(); }
202  UT_ErrorList::const_iterator end() const { return myList.end(); }
203 protected:
204  void setReadFlag(bool state);
205  bool getReadFlag() const
206  {
207  return (myFlags & FLAG_ERRORS_READ) != 0;
208  }
209 
210  void setNeedScan(bool needscan);
211  bool getNeedScan() const
212  {
213  return (myFlags & FLAG_SCAN_NEEDED) != 0;
214  }
215 
216  void setDeadlockError(bool onoff)
217  {
218  if (onoff)
219  myFlags |= FLAG_DEADLOCK;
220  else
221  myFlags &= ~FLAG_DEADLOCK;
222  }
223 
224  void incrementVersion() { ++myVersion; }
225 
226 private:
227 
228  static const int MAX_ERRORS_KEPT = 40; // Max when doing regular add
229  static const int MAX_ERRORS_STEAL = 400; // Max errors when stealing
230 
232  int max_errors = MAX_ERRORS_KEPT,
233  bool log = true);
234 
235  void computeSeverity();
236 
237  UT_Function<UT_StringHolder()> myUniversalLoggingContextFunction;
238  UT_ErrorList myList;
239  UT_ErrorSeverity mySeverity;
240  char myFlags;
241  exint myVersion;
242 };
243 
244 /// A global error manager scope
246 {
247 public:
248  struct DelayTag {};
249  static DelayTag Delay;
250 
251  /// Push empty error manager
252  /// @{
253  Scope();
254  explicit Scope(int thread);
255  /// @}
256 
257  /// Push given error manager, must live beyond current scope. This renders
258  /// all accessors on this class unusable.
259  Scope(UT_ErrorManager &errors);
260 
261  /// Construct scope which has not begun yet. Call begin() to start.
263  : myThread(-1) {}
264 
265  /// Ends error scope if needed
266  ~Scope() { end(); }
267 
269 
270  UT_ErrorManager &getErrorManager() { return myErrorManager; }
271  int getThread() const { return myThread; }
272 
273  UT_ErrorSeverity getSeverity() { return myErrorManager.getSeverity(); }
274 
275  /// Disable error accumulation for this scope, note however that the status
276  /// will be maintained.
277  void setDisabled(bool f)
278  { myErrorManager.setDisabled(f); }
279 
280  /// Get error messages
282  UT_String &messages,
284  bool use_headers = true)
285  {
286  return myErrorManager.getErrorMessages(
287  messages, severity, use_headers);
288  }
289 
290  /// Manually begin the scope. Subsequent calls will do nothing until end()
291  /// is called.
292  /// @{
293  void begin();
294  void begin(int thread);
295  /// @}
296 
297  /// Manually end the scope. Subsequent calls will do nothing.
298  void end();
299 
300 private:
301  UT_ErrorManager myErrorManager;
302  int myThread;
303 };
304 
305 /// Alias UT_AutoErrorManager as UT_ErrorManager::Scope
307 
308 /// Creates a thread safe nestable scope around a call to cook one or more
309 /// nodes. When the last scope exits and the last of these objects is
310 /// destructed, we flush the thread-safe cache of errors logged inside the
311 /// scope to any interested UT_UniversalLogSink objects.
313 {
314 public:
317 
320  = delete;
323  = delete;
324 };
325 
328 
332 UT_API void UTpushErrorManager(int thread);
336 UT_API void UTpopErrorManager(int thread);
337 
338 UT_API UT_ErrorSeverity UTaddWarning(const char *type, int code,
339  const char *msg=0,
340  const UT_SourceLocation *loc=0);
341 UT_API UT_ErrorSeverity UTaddPrompt(const char *type, int code,
342  const char *msg=0,
343  const UT_SourceLocation *loc=0);
344 UT_API UT_ErrorSeverity UTaddMessage(const char *type, int code,
345  const char *msg=0,
346  const UT_SourceLocation *loc=0);
347 UT_API UT_ErrorSeverity UTaddError (const char *type, int code,
348  const char *msg=0,
349  const UT_SourceLocation *loc=0);
350 UT_API UT_ErrorSeverity UTaddFatal (const char *type, int code,
351  const char *msg=0,
352  const UT_SourceLocation *loc=0);
353 UT_API UT_ErrorSeverity UTaddSystemError(const char *msg=0);
355  const char *msg=0);
357  const char *msg=0);
359 
360 /// Add a generic error message to the global error manager.
362  const char *type, int code,
363  const char *msg = 0,
364  const UT_SourceLocation *loc = 0);
366 
367 UT_API int UTfindError(const char *type, int code);
368 
370 UT_API bool UThasError();
371 
372 #include <ostream>
373 UT_API int UTcheckOutStream(std::ostream &os, const char *m, const char *path=0);
374 class UT_OStream;
375 UT_API int UTcheckOutStream(UT_OStream &os, const char *m, const char *path=0);
376 
377 #endif
UT_API bool UThasError()
const UT_Error & getError(int index) const
UT_ErrorSeverity getSeverity()
int getErrorMessages(UT_String &messages, UT_ErrorSeverity severity=UT_ERROR_NONE, bool use_headers=true)
Get error messages.
UT_API void UTpopErrorManager()
UT_ErrorSeverity sopAddMessage(int code, const char *msg=0, const UT_SourceLocation *loc=0)
Scope(DelayTag)
Construct scope which has not begun yet. Call begin() to start.
UT_API UT_ErrorSeverity UTaddFatal(const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
UT_ErrorSeverity
Definition: UT_Error.h:25
UT_API UT_ErrorManager * UTgetErrorManager()
#define UT_API
Definition: UT_API.h:14
void setErrorLogging(bool f)
UT_ErrorList::const_iterator end() const
UT_API UT_ErrorSeverity UTaddMessage(const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
bool getNeedScan() const
void setDeadlockError(bool onoff)
UT_API UT_ErrorSeverity UTaddSystemError(const char *msg=0)
void setDisabled(bool f)
< returns > If no error
Definition: snippets.dox:2
UT_API UT_ErrorSeverity UTgetErrorSeverity()
bool getReadFlag() const
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
static DelayTag Delay
GLfloat f
Definition: glcorearb.h:1926
UT_ErrorSeverity updateSeverity(UT_ErrorSeverity sev)
bool hasDeadlockError() const
int getThread() const
exint getVersion() const
UT_API int UTcheckOutStream(std::ostream &os, const char *m, const char *path=0)
UT_API UT_ErrorSeverity UTaddPrompt(const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
void setUniversalLoggingContextFunction(UT_Function< UT_StringHolder()> context_fn)
UT_ErrorList::const_iterator begin() const
GLuint GLuint end
Definition: glcorearb.h:475
bool isLogging() const
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
GLenum GLenum severity
Definition: glcorearb.h:2539
std::function< T > UT_Function
Definition: UT_Function.h:37
UT_ErrorSeverity sopAddWarning(int code, const char *msg=0, const UT_SourceLocation *loc=0)
bool isDisabled() const
UT_API UT_ErrorSeverity UTaddCommonError(UT_CommonErrorCode what, const char *msg=0)
UT_CommonErrorCode
Definition: UT_Error.h:42
UT_ErrorSeverity sopAddError(int code, const char *msg=0, const UT_SourceLocation *loc=0)
int64 getMemoryUsage(bool inclusive) const
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
UT_API int UTfindError(const char *type, int code)
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
UT_API UT_ErrorSeverity UTaddWarning(const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
A global error manager scope.
base_iterator< const UT_Error *, true > const_iterator
Definition: UT_Array.h:999
UT_API UT_ErrorSeverity UTaddDeadlockError()
UT_API UT_ErrorSeverity UTaddCommonWarning(UT_CommonErrorCode what, const char *msg=0)
bool isEmpty() const
UT_ErrorManager::Scope UT_AutoErrorManager
Alias UT_AutoErrorManager as UT_ErrorManager::Scope.
~Scope()
Ends error scope if needed.
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7688
type
Definition: core.h:1059
UT_API UT_ErrorSeverity UTaddGeneric(UT_ErrorSeverity sev, const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
Add a generic error message to the global error manager.
UT_API void UTpushErrorManager()
int getNumErrors() const
UT_ErrorSeverity sopAddFatal(int code, const char *msg=0, const UT_SourceLocation *loc=0)
UT_API UT_ErrorSeverity UTaddError(const char *type, int code, const char *msg=0, const UT_SourceLocation *loc=0)
UT_ErrorSeverity sopAddPrompt(int code, const char *msg=0, const UT_SourceLocation *loc=0)
UT_ErrorSeverity getErrorState()
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558