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