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
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  // Formatted output with the same notation as Strutil::format.
85  /// Use with caution! Some day this will change to be fmt-like rather
86  /// than printf-like.
87  template<typename... Args>
88  OIIO_FORMAT_DEPRECATED void info(const char* format, const Args&... args)
89  {
90  if (verbosity() >= VERBOSE)
91  info(Strutil::format(format, args...));
92  }
93 
94  /// Warning message with printf-like formatted error message.
95  /// Will not print unless verbosity >= NORMAL (i.e. will suppress
96  /// for QUIET).
97  template<typename... Args>
98  OIIO_FORMAT_DEPRECATED void warning(const char* format, const Args&... args)
99  {
100  if (verbosity() >= NORMAL)
101  warning(Strutil::format(format, args...));
102  }
103 
104  /// Error message with printf-like formatted error message.
105  /// Will print regardless of verbosity.
106  template<typename... Args>
107  OIIO_FORMAT_DEPRECATED void error(const char* format, const Args&... args)
108  {
109  error(Strutil::format(format, args...));
110  }
111 
112  /// Severe error message with printf-like formatted error message.
113  /// Will print regardless of verbosity.
114  template<typename... Args>
115  OIIO_FORMAT_DEPRECATED void severe(const char* format, const Args&... args)
116  {
117  severe(Strutil::format(format, args...));
118  }
119 
120  /// Prefix-less message with printf-like formatted error message.
121  /// Will not print if verbosity is QUIET. Also note that unlike
122  /// the other routines, message() will NOT append a newline.
123  template<typename... Args>
124  OIIO_FORMAT_DEPRECATED void message(const char* format, const Args&... args)
125  {
126  if (verbosity() > QUIET)
127  message(Strutil::format(format, args...));
128  }
129 
130  /// Debugging message with printf-like formatted error message.
131  /// This will not produce any output if not in DEBUG mode, or
132  /// if verbosity is QUIET.
133  template<typename... Args>
135  const Args&... args OIIO_MAYBE_UNUSED)
136  {
137 #ifndef NDEBUG
139 #endif
140  }
141 
142  //
143  // Formatted output with printf notation. Use these if you specifically
144  // want printf-notation, even after format() changes to python notation
145  // in some future OIIO release.
146  //
147  template<typename... Args>
148  void infof(const char* format, const Args&... args)
149  {
150  if (verbosity() >= VERBOSE)
151  info(Strutil::sprintf(format, args...));
152  }
153 
154  template<typename... Args>
155  void warningf(const char* format, const Args&... args)
156  {
157  if (verbosity() >= NORMAL)
158  warning(Strutil::sprintf(format, args...));
159  }
160 
161  template<typename... Args>
162  void errorf(const char* format, const Args&... args)
163  {
164  error(Strutil::sprintf(format, args...));
165  }
166 
167  template<typename... Args>
168  void severef(const char* format, const Args&... args)
169  {
170  severe(Strutil::sprintf(format, args...));
171  }
172 
173  template<typename... Args>
174  void messagef(const char* format, const Args&... args)
175  {
176  if (verbosity() > QUIET)
177  message(Strutil::sprintf(format, args...));
178  }
179 
180  template<typename... Args>
181  void debugf(const char* format OIIO_MAYBE_UNUSED,
182  const Args&... args OIIO_MAYBE_UNUSED)
183  {
184 #ifndef NDEBUG
186 #endif
187  }
188 
189  //
190  // Formatted output with std::format notation. Use these if you
191  // specifically want std::format-notation, even before format() changes
192  // to the new notation in some future OIIO release.
193  //
194  template<typename... Args>
195  void infofmt(const char* format, const Args&... args)
196  {
197  if (verbosity() >= VERBOSE)
198  info(Strutil::fmt::format(format, args...));
199  }
200 
201  template<typename... Args>
202  void warningfmt(const char* format, const Args&... args)
203  {
204  if (verbosity() >= NORMAL)
205  warning(Strutil::fmt::format(format, args...));
206  }
207 
208  template<typename... Args>
209  void errorfmt(const char* format, const Args&... args)
210  {
211  error(Strutil::fmt::format(format, args...));
212  }
213 
214  template<typename... Args>
215  void severefmt(const char* format, const Args&... args)
216  {
217  severe(Strutil::fmt::format(format, args...));
218  }
219 
220  template<typename... Args>
221  void messagefmt(const char* format, const Args&... args)
222  {
223  if (verbosity() > QUIET)
224  message(Strutil::fmt::format(format, args...));
225  }
226 
227  template<typename... Args>
228  void debugfmt(const char* format, const Args&... args)
229  {
230 #ifndef NDEBUG
231  debug(Strutil::fmt::format(format, args...));
232 #endif
233  }
234 
235  /// One built-in handler that can always be counted on to be present
236  /// and just echoes the error messages to the console (stdout or
237  /// stderr, depending on the error category).
238  static ErrorHandler& default_handler();
239 
240 private:
241  int m_verbosity;
242 };
243 
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
void debugf(const char *format OIIO_MAYBE_UNUSED, const Args &...args OIIO_MAYBE_UNUSED)
Definition: errorhandler.h:181
void error(const std::string &msg)
Definition: errorhandler.h:75
void messagefmt(const char *format, const Args &...args)
Definition: errorhandler.h:221
void severef(const char *format, const Args &...args)
Definition: errorhandler.h:168
const GLdouble * v
Definition: glcorearb.h:837
std::string format(const Str &fmt, Args &&...args)
Definition: strutil.h:121
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
OIIO_FORMAT_DEPRECATED void error(const char *format, const Args &...args)
Definition: errorhandler.h:107
virtual ~ErrorHandler()
Definition: errorhandler.h:58
OIIO_FORMAT_DEPRECATED void debug(const char *format OIIO_MAYBE_UNUSED, const Args &...args OIIO_MAYBE_UNUSED)
Definition: errorhandler.h:134
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.
#define OIIO_UTIL_API
Definition: export.h:71
void warningf(const char *format, const Args &...args)
Definition: errorhandler.h:155
< returns > If no error
Definition: snippets.dox:2
#define OIIO_MAYBE_UNUSED
Definition: platform.h:433
void debug(const std::string &msg)
Definition: errorhandler.h:79
void debugfmt(const char *format, const Args &...args)
Definition: errorhandler.h:228
OIIO_FORMAT_DEPRECATED void message(const char *format, const Args &...args)
Definition: errorhandler.h:124
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
void verbosity(int v) noexcept
Set desired verbosity level.
Definition: errorhandler.h:61
void warningfmt(const char *format, const Args &...args)
Definition: errorhandler.h:202
void errorf(const char *format, const Args &...args)
Definition: errorhandler.h:162
void infof(const char *format, const Args &...args)
Definition: errorhandler.h:148
ErrorHandler() noexcept
Definition: errorhandler.h:54
OIIO_FORMAT_DEPRECATED void warning(const char *format, const Args &...args)
Definition: errorhandler.h:98
OIIO_API void debug(string_view str)
void messagef(const char *format, const Args &...args)
Definition: errorhandler.h:174
void infofmt(const char *format, const Args &...args)
Definition: errorhandler.h:195
OIIO_FORMAT_DEPRECATED void info(const char *format, const Args &...args)
Definition: errorhandler.h:88
**If you just want to fire and args
Definition: thread.h:609
#define OIIO_FORMAT_DEPRECATED
Definition: strutil.h:49
void info(const std::string &msg)
Definition: errorhandler.h:73
#define const
Definition: zconf.h:214
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
auto sprintf(const S &fmt, const T &...args) -> std::basic_string< Char >
Definition: printf.h:574
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:215
void errorfmt(const char *format, const Args &...args)
Definition: errorhandler.h:209
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93
OIIO_FORMAT_DEPRECATED void severe(const char *format, const Args &...args)
Definition: errorhandler.h:115