Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef ERROR_HPP_
#define ERROR_HPP_
// *****************************************************************************
#include "exiv2lib_export.h"
#include "config.h"
#include <exception> // for exception
#include <sstream> // for operator<<, ostream, ostringstream, bas...
#include <string> // for basic_string, string
// *****************************************************************************
// namespace extensions
namespace Exiv2 {
// *****************************************************************************
// class definitions
class EXIV2API LogMsg {
public:
LogMsg(const LogMsg&) = delete;
LogMsg& operator=(const LogMsg&) = delete;
enum Level {
debug = 0,
info = 1,
warn = 2,
error = 3,
mute = 4,
};
using Handler = void (*)(int, const char*);
explicit LogMsg(Level msgType);
~LogMsg();
std::ostringstream& os();
static void setLevel(Level level);
static void setHandler(Handler handler);
static Level level();
static Handler handler();
static void defaultHandler(int level, const char* s);
private:
// DATA
// The output level. Only messages with type >= level_ will be written
static Level level_;
// The log handler in use
static Handler handler_;
// The type of this log message
Level msgType_;
// Holds the log message until it is passed to the message handler
std::ostringstream os_;
}; // class LogMsg
// Macros for simple access
#define EXV_DEBUG \
if (LogMsg::debug >= LogMsg::level() && LogMsg::handler()) \
LogMsg(LogMsg::debug).os()
#define EXV_INFO \
if (LogMsg::info >= LogMsg::level() && LogMsg::handler()) \
LogMsg(LogMsg::info).os()
#define EXV_WARNING \
if (LogMsg::warn >= LogMsg::level() && LogMsg::handler()) \
LogMsg(LogMsg::warn).os()
#define EXV_ERROR \
if (LogMsg::error >= LogMsg::level() && LogMsg::handler()) \
LogMsg(LogMsg::error).os()
#ifdef _MSC_VER
// Disable MSVC warnings "non - DLL-interface classkey 'identifier' used as base
// for DLL-interface classkey 'identifier'"
#pragma warning(disable : 4275)
#endif
template <typename charT, typename T>
std::basic_string<charT> toBasicString(const T& arg) {
std::basic_ostringstream<charT> os;
os << arg;
return os.str();
}
enum class ErrorCode {
kerSuccess = 0,
kerGeneralError,
kerErrorMessage,
kerCallFailed,
kerNotAnImage,
kerInvalidDataset,
kerInvalidRecord,
kerInvalidKey,
kerInvalidTag,
kerValueNotSet,
kerDataSourceOpenFailed,
kerFileOpenFailed,
kerFileContainsUnknownImageType,
kerMemoryContainsUnknownImageType,
kerUnsupportedImageType,
kerFailedToReadImageData,
kerNotAJpeg,
kerFailedToMapFileForReadWrite,
kerFileRenameFailed,
kerTransferFailed,
kerMemoryTransferFailed,
kerInputDataReadFailed,
kerImageWriteFailed,
kerNoImageInInputData,
kerInvalidIfdId,
kerValueTooLarge,
kerDataAreaValueTooLarge,
kerOffsetOutOfRange,
kerUnsupportedDataAreaOffsetType,
kerInvalidCharset,
kerUnsupportedDateFormat,
kerUnsupportedTimeFormat,
kerWritingImageFormatUnsupported,
kerInvalidSettingForImage,
kerNotACrwImage,
kerFunctionNotSupported,
kerNoNamespaceInfoForXmpPrefix,
kerNoPrefixForNamespace,
kerTooLargeJpegSegment,
kerUnhandledXmpdatum,
kerUnhandledXmpNode,
kerXMPToolkitError,
kerDecodeLangAltPropertyFailed,
kerDecodeLangAltQualifierFailed,
kerEncodeLangAltPropertyFailed,
kerPropertyNameIdentificationFailed,
kerSchemaNamespaceNotRegistered,
kerNoNamespaceForPrefix,
kerAliasesNotSupported,
kerInvalidXmpText,
kerTooManyTiffDirectoryEntries,
kerMultipleTiffArrayElementTagsInDirectory,
kerWrongTiffArrayElementTagType,
kerInvalidKeyXmpValue,
kerInvalidIccProfile,
kerInvalidXMP,
kerTiffDirectoryTooLarge,
kerInvalidTypeValue,
kerInvalidLangAltValue,
kerInvalidMalloc,
kerCorruptedMetadata,
kerArithmeticOverflow,
kerMallocFailed,
kerInvalidIconvEncoding,
kerFileAccessDisabled,
kerErrorCount,
};
class EXIV2API Error : public std::exception {
public:
explicit Error(ErrorCode code);
template <typename A>
Error(ErrorCode code, const A& arg1) : code_(code), arg1_(toBasicString<char>(arg1)) {
setMsg(1);
}
template <typename A, typename B>
Error(ErrorCode code, const A& arg1, const B& arg2) :
code_(code), arg1_(toBasicString<char>(arg1)), arg2_(toBasicString<char>(arg2)) {
setMsg(2);
}
template <typename A, typename B, typename C>
Error(ErrorCode code, const A& arg1, const B& arg2, const C& arg3) :
code_(code),
arg1_(toBasicString<char>(arg1)),
arg2_(toBasicString<char>(arg2)),
arg3_(toBasicString<char>(arg3)) {
setMsg(3);
}
~Error() noexcept override = default;
[[nodiscard]] ErrorCode code() const noexcept;
[[nodiscard]] const char* what() const noexcept override;
private:
void setMsg(int count);
// DATA
ErrorCode code_;
std::string arg1_;
std::string arg2_;
std::string arg3_;
std::string msg_;
};
inline std::ostream& operator<<(std::ostream& os, const Error& error) {
return os << error.what();
}
#ifdef _MSC_VER
#pragma warning(default : 4275)
#endif
} // namespace Exiv2
#endif // #ifndef ERROR_HPP_
Exiv2::Error
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition error.hpp:236
Exiv2::Error::~Error
~Error() noexcept override=default
Virtual destructor. (Needed because of throw()).
Exiv2::Error::Error
Error(ErrorCode code, const A &arg1)
Constructor taking an error code and one argument.
Definition error.hpp:245
Exiv2::Error::Error
Error(ErrorCode code, const A &arg1, const B &arg2, const C &arg3)
Constructor taking an error code and three arguments.
Definition error.hpp:258
Exiv2::Error::Error
Error(ErrorCode code, const A &arg1, const B &arg2)
Constructor taking an error code and two arguments.
Definition error.hpp:251
Exiv2::Error::what
const char * what() const noexcept override
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition error.cpp:162
Exiv2::Error::Error
Error(ErrorCode code)
Constructor taking only an error code.
Definition error.cpp:154
Exiv2::LogMsg::Handler
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
Exiv2::LogMsg::setLevel
static void setLevel(Level level)
Set the log level. Only log messages with a level greater or equal level are sent to the log message ...
Definition error.cpp:118
Exiv2::LogMsg::LogMsg
LogMsg(const LogMsg &)=delete
Prevent copy-construction: not implemented.
Exiv2::LogMsg::operator=
LogMsg & operator=(const LogMsg &)=delete
Prevent assignment: not implemented.
Exiv2::LogMsg::os
std::ostringstream & os()
Return a reference to the ostringstream which holds the log message.
Definition error.cpp:114
Exiv2::LogMsg::handler
static Handler handler()
Return the current log message handler.
Definition error.cpp:130
Exiv2::LogMsg::level
static Level level()
Return the current log level.
Definition error.cpp:126
Exiv2::LogMsg::setHandler
static void setHandler(Handler handler)
Set the log message handler. The default handler writes log messages to standard error....
Definition error.cpp:122
Exiv2::LogMsg::Level
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
Exiv2::LogMsg::defaultHandler
static void defaultHandler(int level, const char *s)
The default log handler. Sends the log message to standard error.
Definition error.cpp:134
Exiv2
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition asfvideo.hpp:15
Exiv2::ErrorCode
ErrorCode
Complete list of all Exiv2 error codes.
Definition error.hpp:162
Exiv2::toBasicString
std::basic_string< charT > toBasicString(const T &arg)
Generalised toString function.
Definition error.hpp:155