Exiv2
error.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
11 #ifndef ERROR_HPP_
12 #define ERROR_HPP_
13 
14 // *****************************************************************************
15 #include "exiv2lib_export.h"
16 
17 #include "config.h"
18 
19 #include <exception> // for exception
20 #include <sstream> // for operator<<, ostream, ostringstream, bas...
21 #include <string> // for basic_string, string
22 
23 // *****************************************************************************
24 // namespace extensions
25 namespace Exiv2 {
26 // *****************************************************************************
27 // class definitions
28 
56 class EXIV2API LogMsg {
57  public:
59  LogMsg(const LogMsg&) = delete;
61  LogMsg& operator=(const LogMsg&) = delete;
66  enum Level {
67  debug = 0,
68  info = 1,
69  warn = 2,
70  error = 3,
71  mute = 4,
72  };
79  using Handler = void (*)(int, const char*);
80 
82 
83  explicit LogMsg(Level msgType);
85 
87  ~LogMsg();
89 
91 
92  std::ostringstream& os();
95 
102  static void setLevel(Level level);
108  static void setHandler(Handler handler);
110  static Level level();
112  static Handler handler();
114  static void defaultHandler(int level, const char* s);
115 
116  private:
117  // DATA
118  // The output level. Only messages with type >= level_ will be written
119  static Level level_;
120  // The log handler in use
121  static Handler handler_;
122  // The type of this log message
123  Level msgType_;
124  // Holds the log message until it is passed to the message handler
125  std::ostringstream os_;
126 
127 }; // class LogMsg
128 
129 // Macros for simple access
131 #define EXV_DEBUG \
132  if (LogMsg::debug >= LogMsg::level() && LogMsg::handler()) \
133  LogMsg(LogMsg::debug).os()
134 #define EXV_INFO \
136  if (LogMsg::info >= LogMsg::level() && LogMsg::handler()) \
137  LogMsg(LogMsg::info).os()
138 #define EXV_WARNING \
140  if (LogMsg::warn >= LogMsg::level() && LogMsg::handler()) \
141  LogMsg(LogMsg::warn).os()
142 #define EXV_ERROR \
144  if (LogMsg::error >= LogMsg::level() && LogMsg::handler()) \
145  LogMsg(LogMsg::error).os()
146 
147 #ifdef _MSC_VER
148 // Disable MSVC warnings "non - DLL-interface classkey 'identifier' used as base
149 // for DLL-interface classkey 'identifier'"
150 #pragma warning(disable : 4275)
151 #endif
152 
154 template <typename charT, typename T>
155 std::basic_string<charT> toBasicString(const T& arg) {
156  std::basic_ostringstream<charT> os;
157  os << arg;
158  return os.str();
159 }
160 
162 enum class ErrorCode {
163  kerSuccess = 0,
164  kerGeneralError,
165  kerErrorMessage,
166  kerCallFailed,
167  kerNotAnImage,
168  kerInvalidDataset,
169  kerInvalidRecord,
170  kerInvalidKey,
171  kerInvalidTag,
172  kerValueNotSet,
173  kerDataSourceOpenFailed,
174  kerFileOpenFailed,
175  kerFileContainsUnknownImageType,
176  kerMemoryContainsUnknownImageType,
177  kerUnsupportedImageType,
178  kerFailedToReadImageData,
179  kerNotAJpeg,
180  kerFailedToMapFileForReadWrite,
181  kerFileRenameFailed,
182  kerTransferFailed,
183  kerMemoryTransferFailed,
184  kerInputDataReadFailed,
185  kerImageWriteFailed,
186  kerNoImageInInputData,
187  kerInvalidIfdId,
188  kerValueTooLarge,
189  kerDataAreaValueTooLarge,
190  kerOffsetOutOfRange,
191  kerUnsupportedDataAreaOffsetType,
192  kerInvalidCharset,
193  kerUnsupportedDateFormat,
194  kerUnsupportedTimeFormat,
195  kerWritingImageFormatUnsupported,
196  kerInvalidSettingForImage,
197  kerNotACrwImage,
198  kerFunctionNotSupported,
199  kerNoNamespaceInfoForXmpPrefix,
200  kerNoPrefixForNamespace,
201  kerTooLargeJpegSegment,
202  kerUnhandledXmpdatum,
203  kerUnhandledXmpNode,
204  kerXMPToolkitError,
205  kerDecodeLangAltPropertyFailed,
206  kerDecodeLangAltQualifierFailed,
207  kerEncodeLangAltPropertyFailed,
208  kerPropertyNameIdentificationFailed,
209  kerSchemaNamespaceNotRegistered,
210  kerNoNamespaceForPrefix,
211  kerAliasesNotSupported,
212  kerInvalidXmpText,
213  kerTooManyTiffDirectoryEntries,
214  kerMultipleTiffArrayElementTagsInDirectory,
215  kerWrongTiffArrayElementTagType,
216  kerInvalidKeyXmpValue,
217  kerInvalidIccProfile,
218  kerInvalidXMP,
219  kerTiffDirectoryTooLarge,
220  kerInvalidTypeValue,
221  kerInvalidLangAltValue,
222  kerInvalidMalloc,
223  kerCorruptedMetadata,
224  kerArithmeticOverflow,
225  kerMallocFailed,
226  kerInvalidIconvEncoding,
227 
228  kerErrorCount,
229 };
230 
235 class EXIV2API Error : public std::exception {
236  public:
238 
239  explicit Error(ErrorCode code);
241 
243  template <typename A>
244  Error(ErrorCode code, const A& arg1) : code_(code), arg1_(toBasicString<char>(arg1)) {
245  setMsg(1);
246  }
247 
249  template <typename A, typename B>
250  Error(ErrorCode code, const A& arg1, const B& arg2) :
251  code_(code), arg1_(toBasicString<char>(arg1)), arg2_(toBasicString<char>(arg2)) {
252  setMsg(2);
253  }
254 
256  template <typename A, typename B, typename C>
257  Error(ErrorCode code, const A& arg1, const B& arg2, const C& arg3) :
258  code_(code),
259  arg1_(toBasicString<char>(arg1)),
260  arg2_(toBasicString<char>(arg2)),
261  arg3_(toBasicString<char>(arg3)) {
262  setMsg(3);
263  }
264 
266  ~Error() noexcept override = default;
268 
270 
271  [[nodiscard]] ErrorCode code() const noexcept;
276  [[nodiscard]] const char* what() const noexcept override;
278 
279  private:
281 
282  void setMsg(int count);
285 
286  // DATA
287  ErrorCode code_;
288  std::string arg1_;
289  std::string arg2_;
290  std::string arg3_;
291  std::string msg_;
292 };
293 
295 inline std::ostream& operator<<(std::ostream& os, const Error& error) {
296  return os << error.what();
297 }
298 
299 #ifdef _MSC_VER
300 #pragma warning(default : 4275)
301 #endif
302 
303 } // namespace Exiv2
304 #endif // #ifndef ERROR_HPP_
Error(ErrorCode code, const A &arg1, const B &arg2, const C &arg3)
Constructor taking an error code and three arguments.
Definition: error.hpp:257
Simple error class used for exceptions. An output operator is provided to print errors to a stream...
Definition: error.hpp:235
std::basic_string< charT > toBasicString(const T &arg)
Generalised toString function.
Definition: error.hpp:155
STL namespace.
Error(ErrorCode code, const A &arg1, const B &arg2)
Constructor taking an error code and two arguments.
Definition: error.hpp:250
Error(ErrorCode code, const A &arg1)
Constructor taking an error code and one argument.
Definition: error.hpp:244
void(*)(int, const char *) Handler
Type for a log message handler function. The function receives the log level and message and can proc...
Definition: error.hpp:79
Level
Defined log levels. To suppress all log messages, either set the log level to mute or set the log mes...
Definition: error.hpp:66
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition: asfvideo.hpp:15
ErrorCode
Complete list of all Exiv2 error codes.
Definition: error.hpp:162
Class for a log message, used by the library. Applications can set the log level and provide a custom...
Definition: error.hpp:56