OpenImageIO
 All Classes Files Friends Macros Pages
errorhandler.h
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 #ifndef OPENIMAGEIO_ERRORMANAGER_H
33 #define OPENIMAGEIO_ERRORMANAGER_H
34 
35 #include <cstdarg>
36 
37 #include "export.h"
38 #include "version.h"
39 #include "strutil.h"
40 
41 
42 OIIO_NAMESPACE_ENTER
43 {
44 
60 class OIIO_API ErrorHandler {
61 public:
66  enum ErrCode {
67  EH_NO_ERROR = 0, // never sent to handler
68  EH_MESSAGE = 0 << 16,
69  EH_INFO = 1 << 16,
70  EH_WARNING = 2 << 16,
71  EH_ERROR = 3 << 16,
72  EH_SEVERE = 4 << 16,
73  EH_DEBUG = 5 << 16
74  };
75 
78  enum VerbosityLevel {
79  QUIET = 0,
80  NORMAL = 1,
81  VERBOSE = 2
82  };
83 
84  ErrorHandler () : m_verbosity(NORMAL) { }
85  virtual ~ErrorHandler () { }
86 
91  virtual void operator () (int errcode, const std::string &msg);
92 
95  void info (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
96 
100  void warning (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
101 
104  void error (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
105 
108  void severe (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
109 
113  void message (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
114 
118 #ifndef NDEBUG
119  void debug (const char *format, ...) OPENIMAGEIO_PRINTF_ARGS(2,3);
120 #else
121  void debug (const char * /*format*/, ...) OPENIMAGEIO_PRINTF_ARGS(2,3) { }
122 #endif
123 
124  void vInfo (const char *format, va_list argptr);
125  void vWarning (const char *format, va_list argptr);
126  void vError (const char *format, va_list argptr);
127  void vSevere (const char *format, va_list argptr);
128  void vMessage (const char *format, va_list argptr);
129 #ifndef NDEBUG
130  void vDebug (const char *format, va_list argptr);
131 #else
132  void vDebug (const char *, va_list) { }
133 #endif
134 
135  void info (const std::string &msg) { (*this)(EH_INFO, msg); }
136  void warning (const std::string &msg) { (*this)(EH_WARNING, msg); }
137  void error (const std::string &msg) { (*this)(EH_ERROR, msg); }
138  void severe (const std::string &msg) { (*this)(EH_SEVERE, msg); }
139  void message (const std::string &msg) { (*this)(EH_MESSAGE, msg); }
140 #ifndef NDEBUG
141  void debug (const std::string &msg) { (*this)(EH_DEBUG, msg); }
142 #else
143  void debug (const std::string &) { }
144 #endif
145 
148  void verbosity (int v) { m_verbosity = v; }
149 
152  int verbosity () const { return m_verbosity; }
153 
157  static ErrorHandler & default_handler ();
158 
159 private:
160  int m_verbosity;
161 };
162 
163 }
164 OIIO_NAMESPACE_EXIT
165 
166 #endif /* !defined(OPENIMAGEIO_ERRORMANAGER_H) */