RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
RawspeedException.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2009-2014 Klaus Post
5 Copyright (C) 2017 Roman Lebedev
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#pragma once
23
24#include "rawspeedconfig.h"
25#include "common/Common.h"
26#include <array>
27#include <cstdarg>
28#include <cstdio>
29#include <stdexcept>
30
31namespace rawspeed {
32
33template <typename T>
34[[noreturn]] void RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE
35 __attribute__((noreturn, format(printf, 1, 2)))
36 ThrowException(const char* fmt, ...) {
37 static constexpr size_t bufSize = 8192;
38#if defined(HAVE_CXX_THREAD_LOCAL)
39 static thread_local std::array<char, bufSize> buf;
40#elif defined(HAVE_GCC_THREAD_LOCAL)
41 static __thread char buf[bufSize];
42#else
43#pragma message \
44 "Don't have thread-local-storage! Exception text may be garbled if used multithreaded"
45 static char buf[bufSize];
46#endif
47
48 va_list val;
50 vsnprintf(buf.data(), sizeof(buf), fmt, val);
52 writeLog(DEBUG_PRIO::EXTRA, "EXCEPTION: %s", buf.data());
53 throw T(buf.data());
54}
55
56class RawspeedException : public std::runtime_error {
57 static void RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE
58 log(const char* msg) {
59 writeLog(DEBUG_PRIO::EXTRA, "EXCEPTION: %s", msg);
60 }
61
62 virtual void anchor() const;
63
64public:
65 explicit RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE
66 RawspeedException(const char* msg)
67 : std::runtime_error(msg) {
68 log(msg);
69 }
70};
71
72#ifdef XSTR
73#undef XSTR
74#endif
75#define XSTR(a) #a
76
77#ifdef STR
78#undef STR
79#endif
80#define STR(a) XSTR(a)
81
82#ifndef DEBUG
83#define ThrowExceptionHelper(CLASS, fmt, ...) \
84 rawspeed::ThrowException<CLASS>("%s, line " STR(__LINE__) ": " fmt, \
85 __PRETTY_FUNCTION__ __VA_OPT__(, ) \
86 __VA_ARGS__)
87#else
88#define ThrowExceptionHelper(CLASS, fmt, ...) \
89 rawspeed::ThrowException<CLASS>(__FILE__ ":" STR(__LINE__) ": %s: " fmt, \
90 __PRETTY_FUNCTION__ __VA_OPT__(, ) \
91 __VA_ARGS__)
92#endif
93
94#define ThrowRSE(...) \
95 ThrowExceptionHelper(rawspeed::RawspeedException, __VA_ARGS__)
96
97} // namespace rawspeed
static void RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE log(const char *msg)
RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE RawspeedException(const char *msg)
va_start(val, fmt)
vsnprintf(buf.data(), sizeof(buf), fmt, val)
throw T(buf.data())
va_end(val)
void writeLog(DEBUG_PRIO priority, const char *format,...)
Definition Common.cpp:37
__attribute__((noinline)) __attribute__((visibility("default"))) JPEGStuffedByteStreamGenerator
void RAWSPEED_UNLIKELY_FUNCTION RAWSPEED_NOINLINE static char buf[bufSize]