1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4
5 This file is part of the SANE package.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 As a special exception, the authors of SANE give permission for
21 additional uses of the libraries contained in this release of SANE.
22
23 The exception is that, if you link a SANE library with other files
24 to produce an executable, this does not by itself cause the
25 resulting executable to be covered by the GNU General Public
26 License. Your use of that executable is in no way restricted on
27 account of linking the SANE library code into it.
28
29 This exception does not, however, invalidate any other reasons why
30 the executable file might be covered by the GNU General Public
31 License.
32
33 If you submit changes to SANE to the maintainers to be included in
34 a subsequent release, you agree by submitting the changes that
35 those changes may be distributed with this exception intact.
36
37 If you write modifications of your own for SANE, it is your choice
38 whether to permit this exception to apply to your modifications.
39 If you do not wish that, delete this exception notice.
40 */
41
42 #ifndef BACKEND_GENESYS_ERROR_H
43 #define BACKEND_GENESYS_ERROR_H
44
45 #include "../include/sane/config.h"
46 #include "../include/sane/sane.h"
47 #include "../include/sane/sanei_backend.h"
48
49 #include <stdexcept>
50 #include <cstdarg>
51 #include <cstring>
52 #include <string>
53 #include <new>
54
55 #define DBG_error0 0 /* errors/warnings printed even with devuglevel 0 */
56 #define DBG_error 1 /* fatal errors */
57 #define DBG_init 2 /* initialization and scanning time messages */
58 #define DBG_warn 3 /* warnings and non-fatal errors */
59 #define DBG_info 4 /* informational messages */
60 #define DBG_proc 5 /* starting/finishing functions */
61 #define DBG_io 6 /* io functions */
62 #define DBG_io2 7 /* io functions that are called very often */
63 #define DBG_data 8 /* log image data */
64
65 namespace genesys {
66
67 class SaneException : public std::exception {
68 public:
69 SaneException(SANE_Status status);
70 SaneException(SANE_Status status, const char* format, ...)
71 #ifdef __GNUC__
72 __attribute__((format(printf, 3, 4)))
73 #endif
74 ;
75
76 SaneException(const char* format, ...)
77 #ifdef __GNUC__
78 __attribute__((format(printf, 2, 3)))
79 #endif
80 ;
81
82 SANE_Status status() const;
83 const char* what() const noexcept override;
84
85 private:
86
87 void set_msg();
88 void set_msg(const char* format, std::va_list vlist);
89
90 std::string msg_;
91 SANE_Status status_;
92 };
93
94 // call a function and throw an exception on error
95 #define TIE(function) \
96 do { \
97 SANE_Status tmp_status = function; \
98 if (tmp_status != SANE_STATUS_GOOD) { \
99 throw ::genesys::SaneException(tmp_status); \
100 } \
101 } while (false)
102
103 class DebugMessageHelper {
104 public:
105 static constexpr unsigned MAX_BUF_SIZE = 120;
106
107 DebugMessageHelper(const char* func);
108 DebugMessageHelper(const char* func, const char* format, ...)
109 #ifdef __GNUC__
110 __attribute__((format(printf, 3, 4)))
111 #endif
112 ;
113
114 ~DebugMessageHelper();
115
status(const char * msg)116 void status(const char* msg) { vstatus("%s", msg); }
117 void vstatus(const char* format, ...)
118 #ifdef __GNUC__
119 __attribute__((format(printf, 2, 3)))
120 #endif
121 ;
122
clear()123 void clear() { msg_[0] = '\n'; }
124
125 void log(unsigned level, const char* msg);
126 void vlog(unsigned level, const char* format, ...)
127 #ifdef __GNUC__
128 __attribute__((format(printf, 3, 4)))
129 #endif
130 ;
131
132 private:
133 const char* func_ = nullptr;
134 char msg_[MAX_BUF_SIZE];
135 unsigned num_exceptions_on_enter_ = 0;
136 };
137
138 #if defined(__GNUC__) || defined(__clang__)
139 #define GENESYS_CURRENT_FUNCTION __PRETTY_FUNCTION__
140 #elif defined(__FUNCSIG__)
141 #define GENESYS_CURRENT_FUNCTION __FUNCSIG__
142 #else
143 #define GENESYS_CURRENT_FUNCTION __func__
144 #endif
145
146 #define DBG_HELPER(var) DebugMessageHelper var(GENESYS_CURRENT_FUNCTION)
147 #define DBG_HELPER_ARGS(var, ...) DebugMessageHelper var(GENESYS_CURRENT_FUNCTION, __VA_ARGS__)
148
149 bool dbg_log_image_data();
150
151 template<class F>
wrap_exceptions_to_status_code(const char * func,F && function)152 SANE_Status wrap_exceptions_to_status_code(const char* func, F&& function)
153 {
154 try {
155 function();
156 return SANE_STATUS_GOOD;
157 } catch (const SaneException& exc) {
158 DBG(DBG_error, "%s: got error: %s\n", func, exc.what());
159 return exc.status();
160 } catch (const std::bad_alloc& exc) {
161 (void) exc;
162 DBG(DBG_error, "%s: failed to allocate memory\n", func);
163 return SANE_STATUS_NO_MEM;
164 } catch (const std::exception& exc) {
165 DBG(DBG_error, "%s: got uncaught exception: %s\n", func, exc.what());
166 return SANE_STATUS_INVAL;
167 } catch (...) {
168 DBG(DBG_error, "%s: got unknown uncaught exception\n", func);
169 return SANE_STATUS_INVAL;
170 }
171 }
172
173 template<class F>
wrap_exceptions_to_status_code_return(const char * func,F && function)174 SANE_Status wrap_exceptions_to_status_code_return(const char* func, F&& function)
175 {
176 try {
177 return function();
178 } catch (const SaneException& exc) {
179 DBG(DBG_error, "%s: got error: %s\n", func, exc.what());
180 return exc.status();
181 } catch (const std::bad_alloc& exc) {
182 (void) exc;
183 DBG(DBG_error, "%s: failed to allocate memory\n", func);
184 return SANE_STATUS_NO_MEM;
185 } catch (const std::exception& exc) {
186 DBG(DBG_error, "%s: got uncaught exception: %s\n", func, exc.what());
187 return SANE_STATUS_INVAL;
188 } catch (...) {
189 DBG(DBG_error, "%s: got unknown uncaught exception\n", func);
190 return SANE_STATUS_INVAL;
191 }
192 }
193
194 template<class F>
catch_all_exceptions(const char * func,F && function)195 void catch_all_exceptions(const char* func, F&& function)
196 {
197 try {
198 function();
199 } catch (const SaneException& exc) {
200 DBG(DBG_error, "%s: got exception: %s\n", func, exc.what());
201 } catch (const std::bad_alloc& exc) {
202 DBG(DBG_error, "%s: got exception: could not allocate memory: %s\n", func, exc.what());
203 } catch (const std::exception& exc) {
204 DBG(DBG_error, "%s: got uncaught exception: %s\n", func, exc.what());
205 } catch (...) {
206 DBG(DBG_error, "%s: got unknown uncaught exception\n", func);
207 }
208 }
209
wrap_status_code_to_exception(SANE_Status status)210 inline void wrap_status_code_to_exception(SANE_Status status)
211 {
212 if (status == SANE_STATUS_GOOD)
213 return;
214 throw SaneException(status);
215 }
216
217 } // namespace genesys
218
219 #endif // BACKEND_GENESYS_ERROR_H
220