Exiv2
error.hpp
Go to the documentation of this file.
1// ***************************************************************** -*- C++ -*-
2/*
3 * Copyright (C) 2004-2018 Exiv2 authors
4 * This program is part of the Exiv2 distribution.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19 */
28#ifndef ERROR_HPP_
29#define ERROR_HPP_
30
31// *****************************************************************************
32#include "exiv2lib_export.h"
33
34// included header files
35#include "types.hpp"
36
37// + standard includes
38#include <exception>
39#include <string>
40
41// *****************************************************************************
42// namespace extensions
43namespace Exiv2 {
44
45// *****************************************************************************
46// class definitions
47
75 class EXIV2API LogMsg {
77 LogMsg(const LogMsg&);
79 LogMsg& operator=(const LogMsg&);
80 public:
85 enum Level { debug = 0, info = 1, warn = 2, error = 3, mute = 4 };
92 typedef void (*Handler)(int, const char*);
93
95
96
97 explicit LogMsg(Level msgType);
98
100 ~LogMsg();
102
104
105
106 std::ostringstream& os();
108
115 static void setLevel(Level level);
121 static void setHandler(Handler handler);
123 static Level level();
125 static Handler handler();
127 static void defaultHandler(int level, const char* s);
128
129 private:
130 // DATA
131 // The output level. Only messages with type >= level_ will be written
132 static Level level_;
133 // The log handler in use
134 static Handler handler_;
135 // The type of this log message
136 const Level msgType_;
137 // Holds the log message until it is passed to the message handler
138 std::ostringstream os_;
139
140 }; // class LogMsg
141
142// Macros for simple access
144#define EXV_DEBUG if (LogMsg::debug >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::debug).os()
146#define EXV_INFO if (LogMsg::info >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::info).os()
148#define EXV_WARNING if (LogMsg::warn >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::warn).os()
150#define EXV_ERROR if (LogMsg::error >= LogMsg::level() && LogMsg::handler()) LogMsg(LogMsg::error).os()
151
152#ifdef _MSC_VER
153// Disable MSVC warnings "non - DLL-interface classkey 'identifier' used as base
154// for DLL-interface classkey 'identifier'"
155# pragma warning( disable : 4275 )
156#endif
157
159 template<typename charT, typename T>
160 std::basic_string<charT> toBasicString(const T& arg)
161 {
162 std::basic_ostringstream<charT> os;
163 os << arg;
164 return os.str();
165 }
166
174 class EXIV2API AnyError : public std::exception {
175 public:
176 AnyError();
177 AnyError(const AnyError& o);
178
179 virtual ~AnyError() throw();
181 virtual int code() const throw() =0;
182 };
183
185 inline std::ostream& operator<<(std::ostream& os, const AnyError& error)
186 {
187 return os << error.what();
188 }
189
192 kerGeneralError = -1,
193 kerSuccess = 0,
194 kerErrorMessage,
195 kerCallFailed,
196 kerNotAnImage,
197 kerInvalidDataset,
198 kerInvalidRecord,
199 kerInvalidKey,
200 kerInvalidTag,
201 kerValueNotSet,
202 kerDataSourceOpenFailed,
203 kerFileOpenFailed,
204 kerFileContainsUnknownImageType,
205 kerMemoryContainsUnknownImageType,
206 kerUnsupportedImageType,
207 kerFailedToReadImageData,
208 kerNotAJpeg,
209 kerFailedToMapFileForReadWrite,
210 kerFileRenameFailed,
211 kerTransferFailed,
212 kerMemoryTransferFailed,
213 kerInputDataReadFailed,
214 kerImageWriteFailed,
215 kerNoImageInInputData,
216 kerInvalidIfdId,
221 kerOffsetOutOfRange,
222 kerUnsupportedDataAreaOffsetType,
223 kerInvalidCharset,
224 kerUnsupportedDateFormat,
225 kerUnsupportedTimeFormat,
226 kerWritingImageFormatUnsupported,
227 kerInvalidSettingForImage,
228 kerNotACrwImage,
229 kerFunctionNotSupported,
230 kerNoNamespaceInfoForXmpPrefix,
231 kerNoPrefixForNamespace,
232 kerTooLargeJpegSegment,
233 kerUnhandledXmpdatum,
234 kerUnhandledXmpNode,
235 kerXMPToolkitError,
236 kerDecodeLangAltPropertyFailed,
237 kerDecodeLangAltQualifierFailed,
238 kerEncodeLangAltPropertyFailed,
239 kerPropertyNameIdentificationFailed,
240 kerSchemaNamespaceNotRegistered,
241 kerNoNamespaceForPrefix,
242 kerAliasesNotSupported,
243 kerInvalidXmpText,
244 kerTooManyTiffDirectoryEntries,
245 kerMultipleTiffArrayElementTagsInDirectory,
246 kerWrongTiffArrayElementTagType,
247 kerInvalidKeyXmpValue,
248 kerInvalidIccProfile,
249 kerInvalidXMP,
250 kerTiffDirectoryTooLarge,
251 kerInvalidTypeValue,
252 kerInvalidMalloc,
253 kerCorruptedMetadata,
254 kerArithmeticOverflow,
255 kerMallocFailed,
256 };
257
262 template<typename charT>
263 class EXIV2API BasicError : public AnyError {
264 public:
266
267
268 explicit inline BasicError(ErrorCode code);
269
271 template<typename A>
272 inline BasicError(ErrorCode code, const A& arg1);
273
275 template<typename A, typename B>
276 inline BasicError(ErrorCode code, const A& arg1, const B& arg2);
277
279 template<typename A, typename B, typename C>
280 inline BasicError(ErrorCode code, const A& arg1, const B& arg2, const C& arg3);
281
283 virtual inline ~BasicError() throw();
285
287
288 virtual inline int code() const throw();
293 virtual inline const char* what() const throw();
294#ifdef EXV_UNICODE_PATH
299 virtual inline const wchar_t* wwhat() const throw();
300#endif
302
303 private:
305
306
307 void setMsg();
309
310 // DATA
311 ErrorCode code_;
312 int count_;
313 std::basic_string<charT> arg1_;
314 std::basic_string<charT> arg2_;
315 std::basic_string<charT> arg3_;
316 std::string msg_;
317#ifdef EXV_UNICODE_PATH
318 std::wstring wmsg_;
319#endif
320 }; // class BasicError
321
324#ifdef EXV_UNICODE_PATH
326 typedef BasicError<wchar_t> WError;
327#endif
328
329// *****************************************************************************
330// free functions, template and inline definitions
331
333 const char* errMsg(int code);
334
335 template<typename charT>
337 : code_(code), count_(0)
338 {
339 setMsg();
340 }
341
342 template<typename charT> template<typename A>
344 : code_(code), count_(1), arg1_(toBasicString<charT>(arg1))
345 {
346 setMsg();
347 }
348
349 template<typename charT> template<typename A, typename B>
350 BasicError<charT>::BasicError(ErrorCode code, const A& arg1, const B& arg2)
351 : code_(code), count_(2),
352 arg1_(toBasicString<charT>(arg1)),
353 arg2_(toBasicString<charT>(arg2))
354 {
355 setMsg();
356 }
357
358 template<typename charT> template<typename A, typename B, typename C>
359 BasicError<charT>::BasicError(ErrorCode code, const A& arg1, const B& arg2, const C& arg3)
360 : code_(code), count_(3),
361 arg1_(toBasicString<charT>(arg1)),
362 arg2_(toBasicString<charT>(arg2)),
363 arg3_(toBasicString<charT>(arg3))
364 {
365 setMsg();
366 }
367
368 template<typename charT>
370 {
371 }
372
373 template<typename charT>
374 int BasicError<charT>::code() const throw()
375 {
376 return code_;
377 }
378
379 template<typename charT>
380 const char* BasicError<charT>::what() const throw()
381 {
382 return msg_.c_str();
383 }
384
385#ifdef EXV_UNICODE_PATH
386 template<typename charT>
387 const wchar_t* BasicError<charT>::wwhat() const throw()
388 {
389 return wmsg_.c_str();
390 }
391#endif
392
393#ifdef _MSC_VER
394# pragma warning( default : 4275 )
395#endif
396
397} // namespace Exiv2
398#endif // #ifndef ERROR_HPP_
Error class interface. Allows the definition and use of a hierarchy of error classes which can all be...
Definition: error.hpp:174
virtual int code() const =0
Return the error code.
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition: error.hpp:263
BasicError(ErrorCode code)
Constructor taking only an error code.
Definition: error.hpp:336
virtual ~BasicError()
Virtual destructor. (Needed because of throw())
Definition: error.hpp:369
virtual int code() const
Return the error code.
Definition: error.hpp:374
virtual const char * what() const
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition: error.hpp:380
Class for a log message, used by the library. Applications can set the log level and provide a custom...
Definition: error.hpp:75
Level
Defined log levels. To suppress all log messages, either set the log level to mute or set the log mes...
Definition: error.hpp:85
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
ErrorCode
Complete list of all Exiv2 error codes.
Definition: error.hpp:191
@ kerValueTooLarge
Entry::setValue: Value too large.
Definition: error.hpp:218
@ kerDataAreaValueTooLarge
Entry::setDataArea: Value too large.
Definition: error.hpp:220
std::basic_string< charT > toBasicString(const T &arg)
Generalised toString function.
Definition: error.hpp:160
@ string
IPTC string type.
Definition: types.hpp:147
const char * errMsg(int code)
Return the error message for the error with code code.
Definition: error.cpp:298
BasicError< char > Error
Error class used for exceptions (std::string based)
Definition: error.hpp:323
Type definitions for Exiv2 and related functionality.