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  kerFileAccessDisabled,
228 
229  kerErrorCount,
230 };
231 
236 class EXIV2API Error : public std::exception {
237  public:
239 
240  explicit Error(ErrorCode code);
242 
244  template <typename A>
245  Error(ErrorCode code, const A& arg1) : code_(code), arg1_(toBasicString<char>(arg1)) {
246  setMsg(1);
247  }
248 
250  template <typename A, typename B>
251  Error(ErrorCode code, const A& arg1, const B& arg2) :
252  code_(code), arg1_(toBasicString<char>(arg1)), arg2_(toBasicString<char>(arg2)) {
253  setMsg(2);
254  }
255 
257  template <typename A, typename B, typename C>
258  Error(ErrorCode code, const A& arg1, const B& arg2, const C& arg3) :
259  code_(code),
260  arg1_(toBasicString<char>(arg1)),
261  arg2_(toBasicString<char>(arg2)),
262  arg3_(toBasicString<char>(arg3)) {
263  setMsg(3);
264  }
265 
267  ~Error() noexcept override = default;
269 
271 
272  [[nodiscard]] ErrorCode code() const noexcept;
277  [[nodiscard]] const char* what() const noexcept override;
279 
280  private:
282 
283  void setMsg(int count);
286 
287  // DATA
288  ErrorCode code_;
289  std::string arg1_;
290  std::string arg2_;
291  std::string arg3_;
292  std::string msg_;
293 };
294 
296 inline std::ostream& operator<<(std::ostream& os, const Error& error) {
297  return os << error.what();
298 }
299 
300 #ifdef _MSC_VER
301 #pragma warning(default : 4275)
302 #endif
303 
304 } // namespace Exiv2
305 #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:258
Simple error class used for exceptions. An output operator is provided to print errors to a stream...
Definition: error.hpp:236
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:251
Error(ErrorCode code, const A &arg1)
Constructor taking an error code and one argument.
Definition: error.hpp:245
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