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