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