HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
errorhandler.h
Go to the documentation of this file.
1 /*
2  Copyright 2009 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 #pragma once
33 
34 #include <OpenImageIO/export.h>
36 #include <OpenImageIO/strutil.h>
37 
38 
40 
41 /// ErrorHandler is a simple class that accepts error messages
42 /// (classified as errors, severe errors, warnings, info, messages, or
43 /// debug output) and handles them somehow. By default it just prints
44 /// the messages to stdout and/or stderr (and supresses some based on a
45 /// "verbosity" level).
46 ///
47 /// The basic idea is that your library code has no idea whether some
48 /// application that will use it someday will want errors or other
49 /// output to be sent to the console, go to a log file, be intercepted
50 /// by the calling application, or something else. So you punt, by
51 /// having your library take a pointer to an ErrorHandler, passed in
52 /// from the calling app (and possibly subclassed to have arbitrarily
53 /// different behavior from the default console output) and make all
54 /// error-like output via the ErrorHandler*.
55 ///
57 public:
58  /// Error categories. We use broad categories in the high order bits.
59  /// A library may just use these categories, or may create individual
60  /// error codes as long as they have the right high bits to designate
61  /// their category (file not found = ERROR + 1, etc.).
62  enum ErrCode {
63  EH_NO_ERROR = 0, // never sent to handler
64  EH_MESSAGE = 0 << 16,
65  EH_INFO = 1 << 16,
66  EH_WARNING = 2 << 16,
67  EH_ERROR = 3 << 16,
68  EH_SEVERE = 4 << 16,
69  EH_DEBUG = 5 << 16
70  };
71 
72  /// VerbosityLevel controls how much detail the calling app wants.
73  ///
75  QUIET = 0, ///< Show MESSAGE, SEVERE, ERROR only
76  NORMAL = 1, ///< Show MESSAGE, SEVERE, ERROR, WARNING
77  VERBOSE = 2 ///< Like NORMAL, but also show INFO
78  };
79 
81  : m_verbosity(NORMAL)
82  {
83  }
84  virtual ~ErrorHandler() {}
85 
86  /// Set desired verbosity level.
87  void verbosity(int v) { m_verbosity = v; }
88 
89  /// Return the current verbosity level.
90  int verbosity() const { return m_verbosity; }
91 
92  /// The main (or "full detail") method -- takes a code (with high
93  /// bits being an ErrCode) and writes the message, with a prefix
94  /// indicating the error category (no prefix for "MESSAGE") and
95  /// error string.
96  virtual void operator()(int errcode, const std::string& msg);
97 
98  // Base cases -- take a single string
99  void info(const std::string& msg) { (*this)(EH_INFO, msg); }
100  void warning(const std::string& msg) { (*this)(EH_WARNING, msg); }
101  void error(const std::string& msg) { (*this)(EH_ERROR, msg); }
102  void severe(const std::string& msg) { (*this)(EH_SEVERE, msg); }
103  void message(const std::string& msg) { (*this)(EH_MESSAGE, msg); }
104 #ifndef NDEBUG
105  void debug(const std::string& msg) { (*this)(EH_DEBUG, msg); }
106 #else
107  void debug(const std::string&) {}
108 #endif
109 
110  //
111  // Formatted output with the same notation as Strutil::format.
112  /// Use with caution! Some day this will change to be fmt-like rather
113  /// than printf-like.
114  //
115  template<typename... Args>
116  void info(const char* format, const Args&... args)
117  {
118  if (verbosity() >= VERBOSE)
119  info(Strutil::format(format, args...));
120  }
121 
122  /// Warning message with printf-like formatted error message.
123  /// Will not print unless verbosity >= NORMAL (i.e. will suppress
124  /// for QUIET).
125  template<typename... Args>
126  void warning(const char* format, const Args&... args)
127  {
128  if (verbosity() >= NORMAL)
129  warning(Strutil::format(format, args...));
130  }
131 
132  /// Error message with printf-like formatted error message.
133  /// Will print regardless of verbosity.
134  template<typename... Args>
135  void error(const char* format, const Args&... args)
136  {
137  error(Strutil::format(format, args...));
138  }
139 
140  /// Severe error message with printf-like formatted error message.
141  /// Will print regardless of verbosity.
142  template<typename... Args>
143  void severe(const char* format, const Args&... args)
144  {
145  severe(Strutil::format(format, args...));
146  }
147 
148  /// Prefix-less message with printf-like formatted error message.
149  /// Will not print if verbosity is QUIET. Also note that unlike
150  /// the other routines, message() will NOT append a newline.
151  template<typename... Args>
152  void message(const char* format, const Args&... args)
153  {
154  if (verbosity() > QUIET)
155  message(Strutil::format(format, args...));
156  }
157 
158  /// Debugging message with printf-like formatted error message.
159  /// This will not produce any output if not in DEBUG mode, or
160  /// if verbosity is QUIET.
161  template<typename... Args>
162  void debug(const char* format, const Args&... args)
163  {
164 #ifndef NDEBUG
165  debug(Strutil::format(format, args...));
166 #endif
167  }
168 
169  //
170  // Formatted output with printf notation. Use these if you specifically
171  // want printf-notation, even after format() changes to python notation
172  // for OIIO 2.1.
173  //
174  template<typename... Args>
175  void infof(const char* format, const Args&... args)
176  {
177  if (verbosity() >= VERBOSE)
178  info(Strutil::sprintf(format, args...));
179  }
180 
181  template<typename... Args>
182  void warningf(const char* format, const Args&... args)
183  {
184  if (verbosity() >= NORMAL)
185  warning(Strutil::sprintf(format, args...));
186  }
187 
188  template<typename... Args>
189  void errorf(const char* format, const Args&... args)
190  {
191  error(Strutil::sprintf(format, args...));
192  }
193 
194  template<typename... Args>
195  void severef(const char* format, const Args&... args)
196  {
197  severe(Strutil::sprintf(format, args...));
198  }
199 
200  template<typename... Args>
201  void messagef(const char* format, const Args&... args)
202  {
203  if (verbosity() > QUIET)
204  message(Strutil::sprintf(format, args...));
205  }
206 
207  template<typename... Args>
208  void debugf(const char* format, const Args&... args)
209  {
210 #ifndef NDEBUG
211  debug(Strutil::sprintf(format, args...));
212 #endif
213  }
214 
215  /// One built-in handler that can always be counted on to be present
216  /// and just echoes the error messages to the console (stdout or
217  /// stderr, depending on the error category).
218  static ErrorHandler& default_handler();
219 
220 private:
221  int m_verbosity;
222 };
223 
std::string sprintf(const char *fmt, const Args &...args)
Definition: strutil.h:136
void error(const char *format, const Args &...args)
Definition: errorhandler.h:135
void error(const std::string &msg)
Definition: errorhandler.h:101
void severe(const char *format, const Args &...args)
Definition: errorhandler.h:143
void severef(const char *format, const Args &...args)
Definition: errorhandler.h:195
const Args & args
Definition: printf.h:628
void warning(const char *format, const Args &...args)
Definition: errorhandler.h:126
virtual ~ErrorHandler()
Definition: errorhandler.h:84
int verbosity() const
Return the current verbosity level.
Definition: errorhandler.h:90
void warning(const std::string &msg)
Definition: errorhandler.h:100
const GLdouble * v
Definition: glew.h:1391
void severe(const std::string &msg)
Definition: errorhandler.h:102
String-related utilities, all in namespace Strutil.
void message(const char *format, const Args &...args)
Definition: errorhandler.h:152
void warningf(const char *format, const Args &...args)
Definition: errorhandler.h:182
void debug(const std::string &msg)
Definition: errorhandler.h:105
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
Definition: glew.h:2581
void verbosity(int v)
Set desired verbosity level.
Definition: errorhandler.h:87
void debugf(const char *format, const Args &...args)
Definition: errorhandler.h:208
void debug(const char *format, const Args &...args)
Definition: errorhandler.h:162
void errorf(const char *format, const Args &...args)
Definition: errorhandler.h:189
void infof(const char *format, const Args &...args)
Definition: errorhandler.h:175
OIIO_API void debug(string_view str)
void messagef(const char *format, const Args &...args)
Definition: errorhandler.h:201
GLsizei const GLchar *const * string
Definition: glew.h:1844
void info(const char *format, const Args &...args)
Definition: errorhandler.h:116
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
void info(const std::string &msg)
Definition: errorhandler.h:99
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
void message(const std::string &msg)
Definition: errorhandler.h:103
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65
#define OIIO_API
Definition: export.h:91