HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
errorhandler.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio/blob/master/LICENSE.md
4 
5 
6 #pragma once
7 
8 #include <OpenImageIO/export.h>
10 #include <OpenImageIO/strutil.h>
11 
12 
14 
15 /// ErrorHandler is a simple class that accepts error messages
16 /// (classified as errors, severe errors, warnings, info, messages, or
17 /// debug output) and handles them somehow. By default it just prints
18 /// the messages to stdout and/or stderr (and suppresses some based on a
19 /// "verbosity" level).
20 ///
21 /// The basic idea is that your library code has no idea whether some
22 /// application that will use it someday will want errors or other
23 /// output to be sent to the console, go to a log file, be intercepted
24 /// by the calling application, or something else. So you punt, by
25 /// having your library take a pointer to an ErrorHandler, passed in
26 /// from the calling app (and possibly subclassed to have arbitrarily
27 /// different behavior from the default console output) and make all
28 /// error-like output via the ErrorHandler*.
29 ///
31 public:
32  /// Error categories. We use broad categories in the high order bits.
33  /// A library may just use these categories, or may create individual
34  /// error codes as long as they have the right high bits to designate
35  /// their category (file not found = ERROR + 1, etc.).
36  enum ErrCode {
37  EH_NO_ERROR = 0, // never sent to handler
38  EH_MESSAGE = 0 << 16,
39  EH_INFO = 1 << 16,
40  EH_WARNING = 2 << 16,
41  EH_ERROR = 3 << 16,
42  EH_SEVERE = 4 << 16,
43  EH_DEBUG = 5 << 16
44  };
45 
46  /// VerbosityLevel controls how much detail the calling app wants.
47  ///
49  QUIET = 0, ///< Show MESSAGE, SEVERE, ERROR only
50  NORMAL = 1, ///< Show MESSAGE, SEVERE, ERROR, WARNING
51  VERBOSE = 2 ///< Like NORMAL, but also show INFO
52  };
53 
54  ErrorHandler() noexcept
55  : m_verbosity(NORMAL)
56  {
57  }
58  virtual ~ErrorHandler() {}
59 
60  /// Set desired verbosity level.
61  void verbosity(int v) noexcept { m_verbosity = v; }
62 
63  /// Return the current verbosity level.
64  int verbosity() const noexcept { return m_verbosity; }
65 
66  /// The main (or "full detail") method -- takes a code (with high
67  /// bits being an ErrCode) and writes the message, with a prefix
68  /// indicating the error category (no prefix for "MESSAGE") and
69  /// error string.
70  virtual void operator()(int errcode, const std::string& msg);
71 
72  // Base cases -- take a single string
73  void info(const std::string& msg) { (*this)(EH_INFO, msg); }
74  void warning(const std::string& msg) { (*this)(EH_WARNING, msg); }
75  void error(const std::string& msg) { (*this)(EH_ERROR, msg); }
76  void severe(const std::string& msg) { (*this)(EH_SEVERE, msg); }
77  void message(const std::string& msg) { (*this)(EH_MESSAGE, msg); }
78 #ifndef NDEBUG
79  void debug(const std::string& msg) { (*this)(EH_DEBUG, msg); }
80 #else
81  void debug(const std::string&) {}
82 #endif
83 
84  //
85  // Formatted output with the same notation as Strutil::format.
86  /// Use with caution! Some day this will change to be fmt-like rather
87  /// than printf-like.
88  //
89  template<typename... Args>
90  void info(const char* format, const Args&... args)
91  {
92  if (verbosity() >= VERBOSE)
93  info(Strutil::format(format, args...));
94  }
95 
96  /// Warning message with printf-like formatted error message.
97  /// Will not print unless verbosity >= NORMAL (i.e. will suppress
98  /// for QUIET).
99  template<typename... Args>
100  void warning(const char* format, const Args&... args)
101  {
102  if (verbosity() >= NORMAL)
103  warning(Strutil::format(format, args...));
104  }
105 
106  /// Error message with printf-like formatted error message.
107  /// Will print regardless of verbosity.
108  template<typename... Args>
109  void error(const char* format, const Args&... args)
110  {
111  error(Strutil::format(format, args...));
112  }
113 
114  /// Severe error message with printf-like formatted error message.
115  /// Will print regardless of verbosity.
116  template<typename... Args>
117  void severe(const char* format, const Args&... args)
118  {
119  severe(Strutil::format(format, args...));
120  }
121 
122  /// Prefix-less message with printf-like formatted error message.
123  /// Will not print if verbosity is QUIET. Also note that unlike
124  /// the other routines, message() will NOT append a newline.
125  template<typename... Args>
126  void message(const char* format, const Args&... args)
127  {
128  if (verbosity() > QUIET)
129  message(Strutil::format(format, args...));
130  }
131 
132  /// Debugging message with printf-like formatted error message.
133  /// This will not produce any output if not in DEBUG mode, or
134  /// if verbosity is QUIET.
135  template<typename... Args>
136  void debug(const char* format OIIO_MAYBE_UNUSED,
137  const Args&... args OIIO_MAYBE_UNUSED)
138  {
139 #ifndef NDEBUG
141 #endif
142  }
143 
144  //
145  // Formatted output with printf notation. Use these if you specifically
146  // want printf-notation, even after format() changes to python notation
147  // in some future OIIO release.
148  //
149  template<typename... Args>
150  void infof(const char* format, const Args&... args)
151  {
152  if (verbosity() >= VERBOSE)
153  info(Strutil::sprintf(format, args...));
154  }
155 
156  template<typename... Args>
157  void warningf(const char* format, const Args&... args)
158  {
159  if (verbosity() >= NORMAL)
160  warning(Strutil::sprintf(format, args...));
161  }
162 
163  template<typename... Args>
164  void errorf(const char* format, const Args&... args)
165  {
166  error(Strutil::sprintf(format, args...));
167  }
168 
169  template<typename... Args>
170  void severef(const char* format, const Args&... args)
171  {
172  severe(Strutil::sprintf(format, args...));
173  }
174 
175  template<typename... Args>
176  void messagef(const char* format, const Args&... args)
177  {
178  if (verbosity() > QUIET)
179  message(Strutil::sprintf(format, args...));
180  }
181 
182  template<typename... Args>
183  void debugf(const char* format OIIO_MAYBE_UNUSED,
184  const Args&... args OIIO_MAYBE_UNUSED)
185  {
186 #ifndef NDEBUG
188 #endif
189  }
190 
191  //
192  // Formatted output with std::format notation. Use these if you
193  // specifically want std::format-notation, even before format() changes
194  // to the new notation in some future OIIO release.
195  //
196  template<typename... Args>
197  void infofmt(const char* format, const Args&... args)
198  {
199  if (verbosity() >= VERBOSE)
200  info(Strutil::fmt::format(format, args...));
201  }
202 
203  template<typename... Args>
204  void warningfmt(const char* format, const Args&... args)
205  {
206  if (verbosity() >= NORMAL)
207  warning(Strutil::fmt::format(format, args...));
208  }
209 
210  template<typename... Args>
211  void errorfmt(const char* format, const Args&... args)
212  {
213  error(Strutil::fmt::format(format, args...));
214  }
215 
216  template<typename... Args>
217  void severefmt(const char* format, const Args&... args)
218  {
219  severe(Strutil::fmt::format(format, args...));
220  }
221 
222  template<typename... Args>
223  void messagefmt(const char* format, const Args&... args)
224  {
225  if (verbosity() > QUIET)
226  message(Strutil::fmt::format(format, args...));
227  }
228 
229  template<typename... Args>
230  void debugfmt(const char* format, const Args&... args)
231  {
232 #ifndef NDEBUG
233  debug(Strutil::fmt::format(format, args...));
234 #endif
235  }
236 
237  /// One built-in handler that can always be counted on to be present
238  /// and just echoes the error messages to the console (stdout or
239  /// stderr, depending on the error category).
240  static ErrorHandler& default_handler();
241 
242 private:
243  int m_verbosity;
244 };
245 
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2542
std::string sprintf(const char *fmt, const Args &...args)
Definition: strutil.h:90
void debugf(const char *format OIIO_MAYBE_UNUSED, const Args &...args OIIO_MAYBE_UNUSED)
Definition: errorhandler.h:183
void error(const char *format, const Args &...args)
Definition: errorhandler.h:109
void error(const std::string &msg)
Definition: errorhandler.h:75
void messagefmt(const char *format, const Args &...args)
Definition: errorhandler.h:223
void severe(const char *format, const Args &...args)
Definition: errorhandler.h:117
void severef(const char *format, const Args &...args)
Definition: errorhandler.h:170
void warning(const char *format, const Args &...args)
Definition: errorhandler.h:100
void debug(const char *format OIIO_MAYBE_UNUSED, const Args &...args OIIO_MAYBE_UNUSED)
Definition: errorhandler.h:136
virtual ~ErrorHandler()
Definition: errorhandler.h:58
std::string format(const char *fmt, const Args &...args)
Definition: strutil.h:124
void warning(const std::string &msg)
Definition: errorhandler.h:74
void severe(const std::string &msg)
Definition: errorhandler.h:76
String-related utilities, all in namespace Strutil.
void message(const char *format, const Args &...args)
Definition: errorhandler.h:126
void warningf(const char *format, const Args &...args)
Definition: errorhandler.h:157
#define OIIO_MAYBE_UNUSED
Definition: platform.h:396
void debug(const std::string &msg)
Definition: errorhandler.h:79
void debugfmt(const char *format, const Args &...args)
Definition: errorhandler.h:230
const GLdouble * v
Definition: glcorearb.h:836
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
void verbosity(int v) noexcept
Set desired verbosity level.
Definition: errorhandler.h:61
void warningfmt(const char *format, const Args &...args)
Definition: errorhandler.h:204
void errorf(const char *format, const Args &...args)
Definition: errorhandler.h:164
void infof(const char *format, const Args &...args)
Definition: errorhandler.h:150
ErrorHandler() noexcept
Definition: errorhandler.h:54
OIIO_API void debug(string_view str)
void messagef(const char *format, const Args &...args)
Definition: errorhandler.h:176
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
void infofmt(const char *format, const Args &...args)
Definition: errorhandler.h:197
void info(const char *format, const Args &...args)
Definition: errorhandler.h:90
**If you just want to fire and args
Definition: thread.h:615
void info(const std::string &msg)
Definition: errorhandler.h:73
#define const
Definition: zconf.h:214
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
int verbosity() const noexcept
Return the current verbosity level.
Definition: errorhandler.h:64
void message(const std::string &msg)
Definition: errorhandler.h:77
void severefmt(const char *format, const Args &...args)
Definition: errorhandler.h:217
void errorfmt(const char *format, const Args &...args)
Definition: errorhandler.h:211
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93
#define OIIO_API
Definition: export.h:65