HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ErrorLog.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_ErrorLog.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_ErrorLog__
12 #define __UT_ErrorLog__
13 
14 #include "UT_API.h"
15 #include "UT_Function.h"
16 #include "UT_Lock.h"
17 #include "UT_String.h"
18 #include "UT_StringHolder.h"
19 #include "UT_SymbolTable.h"
20 #include "UT_WorkBuffer.h"
21 #include <SYS/SYS_Types.h>
22 
23 #include <time.h>
24 
25 class UT_Error;
26 
28 {
29 public:
30  UT_ErrorLog();
31  ~UT_ErrorLog();
32 
34 
35  /// Functor to handle errors printed out using the mantra* methods.
37  {
38  public:
40  /// The destructor automatically de-registers the callback
41  virtual ~ErrorCallback();
42 
43  ErrorCallback(const ErrorCallback &) = delete;
44  ErrorCallback &operator=(const ErrorCallback &) = delete;
45 
46  /// If the method returns @c true, the error will not be printed out
47  /// by the built-in error handler. If it returns @c false, processing
48  /// continues in the normal way.
49  /// The @c level is the verbosity level of the error.
50  /// The built-in process will print out:
51  /// ("%s%s", prefix, message).
52  virtual bool filterError(int verbosity,
53  const char *message,
54  const char *prefix) = 0;
55 
56  /// This method takes the verbosity *and* the error level. Sometimes,
57  /// programs will print a message with verbosity 1, which is not a
58  /// warning. `error_level` will be 0 (error), 1 (warning), or 2
59  /// (message).
60  virtual bool filterMessage(int verbosity, int error_level,
61  const char *message,
62  const char *prefix)
63  {
64  return filterError(verbosity, message, prefix);
65  }
66  };
67  /// If you want to un-register the callback, just delete the object.
68  static void registerCallback(ErrorCallback *err);
69  /// Get the universal logging source name for this log source.
70  static const UT_StringHolder &universalLogSourceName();
71 
72  // Log the error's description to the error file if its severity is greater
73  // than or equal to our minimum level
74  void logError(const UT_Error *);
75 
76  // We only worry about locking when we set strings; we assume integers
77  // and booleans are atomic
78  const UT_String &getLogFileName() const
79  { return myLogFileName; }
81  {
82  myLock.lock();
83  myLogFileName.harden(name);
84  myLogFileName.expandVariables();
85  myLock.unlock();
86  }
87 
88  int getMinLevel() const
89  { return myMinLevel; }
90  void setMinLevel(const int level)
91  {
92  UT_Lock::Scope lock(myLock);
93  myMinLevel = level;
94  myMinConsoleLevel = level;
95  }
96  int getMinConsoleLevel() const
97  { return myMinConsoleLevel; }
98  void setMinConsoleLevel(const int level)
99  {
100  UT_Lock::Scope lock(myLock);
101  myMinConsoleLevel = level;
102  }
103 
104  bool getDoLogging() const
105  { return myDoLogging; }
106  void setDoLogging(bool log)
107  {
108  UT_Lock::Scope lock(myLock);
109  myDoLogging = log; }
110  bool getLogToConsole() const
111  { return myLogToConsole; }
112  void setLogToConsole(bool log)
113  {
114  UT_Lock::Scope lock(myLock);
115  myLogToConsole = log;
116  }
117  bool getForceConsoleColor() const
118  { return myForceConsoleColor; }
120  {
121  UT_Lock::Scope lock(myLock);
122  myForceConsoleColor = log; }
123  bool getLogToSystem() const
124  { return myLogToSystem; }
125  void setLogToSystem(bool log)
126  {
127  UT_Lock::Scope lock(myLock);
128  myLogToSystem = log;
129  }
130  const UT_StringHolder& getPrefix() const
131  { return myPrefix; }
132  void setPrefix(const UT_StringHolder& prefix)
133  {
134  UT_Lock::Scope lock(myLock);
135  myPrefix = prefix;
136  }
137  bool getAppendDate() const
138  {
139  return myAppendDate;
140  }
141  void setAppendDate(bool append_date)
142  {
143  UT_Lock::Scope lock(myLock);
144  myAppendDate = append_date;
145  }
146 
147  void resetState()
148  {
149  myLock.lock();
150  myErrorLogTimes.clear();
151  myLock.unlock();
152  }
153 
155  {
156  UT_Lock::Scope lock(myLock);
157  myMaxLogSize = size;
158  }
160  {
161  UT_Lock::Scope lock(myLock);
162  myHeaderCallback = callback;
163  }
164  // Callback function to check whether mantra verbosity is enabled for a
165  // particular callback level.
166  typedef int (*MantraVerboseCallback)();
167  typedef int (*MantraColorCallback)();
168  typedef bool (*MantraTimestampCallback)();
169 
170  // Set the verbosity level for mantra error messages. For any
171  // printouts to occur, it is necessary to first call this method with a
172  // non-negative value - otherwise calls to mantraPrintf, mantraWarning,
173  // and mantraError will be ignored. As an alternative, you can provide
174  // a callback that will dynamically update the verbosity level.
175  static void setMantraVerboseLevel(int level);
176  static int getMantraVerboseLevel();
177  static bool isMantraVerbose(int level);
178  static void setMantraElapsedTime(bool enable);
179  static bool mantraElapsedTime();
180 
181  // This method will also cause errors and warnings to use a "mantra: "
182  // prefix.
183  static void setMantraVerboseCallback(MantraVerboseCallback cb);
184  static void setMantraColorCallback(MantraColorCallback cb);
185  static void setMantraTimestampCallback(MantraTimestampCallback cb);
186 
187  static int mantraPrintf(int level, const char *msg, ...)
189  static int mantraWarning(const char *msg, ...)
191  static int mantraError(const char *msg, ...)
192  SYS_PRINTF_CHECK_ATTRIBUTE(1,2);
193 
194  /// @{
195  /// Use UTformat to format messages
196  template <typename... Args>
197  static size_t format(int lev, const char *fmt, const Args &...args)
198  {
199  if (!isMantraVerbose(lev))
200  return 0;
202  buf.format(fmt, args...);
203  return mantraPrintf(lev, "%s", buf.buffer());
204  }
205  template <typename... Args>
206  static size_t warning(const char *fmt, const Args &...args)
207  {
208  if (!isMantraVerbose(1))
209  return 0;
211  buf.format(fmt, args...);
212  return mantraWarning("%s", buf.buffer());
213  }
214  template <typename... Args>
215  static size_t error(const char *fmt, const Args &...args)
216  {
218  buf.format(fmt, args...);
219  return mantraError("%s", buf.buffer());
220  }
221 
222  template <typename... Args>
223  static size_t formatOnce(int lev, const char *fmt,
224  const Args &...args)
225  {
226  if (!isMantraVerbose(lev))
227  return 0;
229  buf.format(fmt, args...);
230  return mantraPrintfOnce(lev, "%s", buf.buffer());
231  }
232  template <typename... Args>
233  static size_t warningOnce(const char *fmt, const Args &...args)
234  {
235  if (!isMantraVerbose(1))
236  return 0;
238  buf.format(fmt, args...);
239  return mantraWarningOnce("%s", buf.buffer());
240  }
241  template <typename... Args>
242  static size_t errorOnce(const char *fmt, const Args &...args)
243  {
245  buf.format(fmt, args...);
246  return mantraErrorOnce("%s", buf.buffer());
247  }
248  /// @}
249 
250  // Print the error message only if it has not been printed before
251  static int mantraPrintfOnce(int level, const char *msg, ...)
253  static int mantraWarningOnce(const char *msg, ...)
255  static int mantraErrorOnce(const char *msg, ...)
256  SYS_PRINTF_CHECK_ATTRIBUTE(1,2);
257 
258  // Clear tables of messages that have been printed before so that if
259  // an error recurs, it will be printed again.
260  static void clearMantraOnceErrors();
261 
262  static void printErrorToConsole(const char *msg,
263  const char *prefix = "",
264  bool enable_color = false);
265  static void printWarningToConsole(const char *msg,
266  const char *prefix = "",
267  bool enable_color = false);
268  static void printMessageToConsole(const char *msg,
269  const char *prefix = "",
270  bool enable_color = false);
271 protected:
272 
273 private:
274  int myMinLevel;
275  int myMinConsoleLevel;
276  UT_String myLogFileName;
277  bool myDoLogging;
278  bool myLogToConsole;
279  bool myLogToSystem;
280  bool myForceConsoleColor;
281  bool myAppendDate;
282  // Set to <= 0 to disable maximum file size. Default is no max size
283  int64 myMaxLogSize;
284  HeaderCallback myHeaderCallback;
285  UT_StringHolder myPrefix;
286  UT_SymbolMap<time_t> myErrorLogTimes;
287  time_t myLastErrorTime;
288  UT_Lock myLock;
289 };
290 
292 
293 #endif
294 
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
virtual bool filterMessage(int verbosity, int error_level, const char *message, const char *prefix)
Definition: UT_ErrorLog.h:60
void setMinLevel(const int level)
Definition: UT_ErrorLog.h:90
void setForceConsoleColor(bool log)
Definition: UT_ErrorLog.h:119
GLint level
Definition: glcorearb.h:108
void setLogToConsole(bool log)
Definition: UT_ErrorLog.h:112
SYS_FORCE_INLINE const char * buffer() const
UT_API UT_ErrorLog * UTgetErrorLog()
#define UT_API
Definition: UT_API.h:14
static size_t error(const char *fmt, const Args &...args)
Definition: UT_ErrorLog.h:215
void resetState()
Definition: UT_ErrorLog.h:147
int getMinLevel() const
Definition: UT_ErrorLog.h:88
void setLogToSystem(bool log)
Definition: UT_ErrorLog.h:125
OutGridT const XformOp bool bool
void setDoLogging(bool log)
Definition: UT_ErrorLog.h:106
static size_t formatOnce(int lev, const char *fmt, const Args &...args)
Definition: UT_ErrorLog.h:223
void setAppendDate(bool append_date)
Definition: UT_ErrorLog.h:141
int getMinConsoleLevel() const
Definition: UT_ErrorLog.h:96
static size_t warningOnce(const char *fmt, const Args &...args)
Definition: UT_ErrorLog.h:233
const UT_String & getLogFileName() const
Definition: UT_ErrorLog.h:78
void setLogFileName(const UT_String &name)
Definition: UT_ErrorLog.h:80
static size_t warning(const char *fmt, const Args &...args)
Definition: UT_ErrorLog.h:206
static size_t errorOnce(const char *fmt, const Args &...args)
Definition: UT_ErrorLog.h:242
#define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check)
Definition: SYS_Types.h:453
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
void setMaxLogSize(int64 size)
Definition: UT_ErrorLog.h:154
long long int64
Definition: SYS_Types.h:116
bool getAppendDate() const
Definition: UT_ErrorLog.h:137
void setPrefix(const UT_StringHolder &prefix)
Definition: UT_ErrorLog.h:132
GLuint const GLchar * name
Definition: glcorearb.h:786
std::function< T > UT_Function
Definition: UT_Function.h:37
bool getLogToConsole() const
Definition: UT_ErrorLog.h:110
GLsizeiptr size
Definition: glcorearb.h:664
size_t format(const char *fmt, const Args &...args)
LeafData & operator=(const LeafData &)=delete
bool getForceConsoleColor() const
Definition: UT_ErrorLog.h:117
void setHeaderCallback(HeaderCallback callback)
Definition: UT_ErrorLog.h:159
const UT_StringHolder & getPrefix() const
Definition: UT_ErrorLog.h:130
bool getLogToSystem() const
Definition: UT_ErrorLog.h:123
**If you just want to fire and args
Definition: thread.h:618
void setMinConsoleLevel(const int level)
Definition: UT_ErrorLog.h:98
Functor to handle errors printed out using the mantra* methods.
Definition: UT_ErrorLog.h:36
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7905
bool getDoLogging() const
Definition: UT_ErrorLog.h:104
UT_Function< void(UT_WorkBuffer &)> HeaderCallback
Definition: UT_ErrorLog.h:33