Debian Codegraph @

lib/grn_error.h

raw symbols SCIP
1 /*
2 Copyright (C) 2013-2016 Brazil
3 Copyright (C) 2020-2025 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #pragma once
21
22 #include "grn.h"
23
24 #include <stdarg.h>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define GRN_EMERG GRN_LOG_EMERG
31 #define GRN_ALERT GRN_LOG_ALERT
32 #define GRN_CRIT GRN_LOG_CRIT
33 #define GRN_ERROR GRN_LOG_ERROR
34 #define GRN_WARN GRN_LOG_WARNING
35 #define GRN_OK GRN_LOG_NOTICE
36
37 #define ERRCLR(ctx) \
38 do { \
39 if (ctx) { \
40 ((grn_ctx *)ctx)->errlvl = GRN_OK; \
41 if (((grn_ctx *)ctx)->rc != GRN_CANCEL) { \
42 ((grn_ctx *)ctx)->rc = GRN_SUCCESS; \
43 ((grn_ctx *)ctx)->errbuf[0] = '\0'; \
44 } \
45 } \
46 errno = 0; \
47 grn_gctx.errlvl = GRN_OK; \
48 grn_gctx.rc = GRN_SUCCESS; \
49 } while (0)
50
51 void
52 grn_error_init_from_env(void);
53 void
54 grn_error_cancel(grn_ctx *ctx);
55
56 static inline grn_rc
57 grn_ctx_rc_propagate(grn_ctx *ctx, grn_rc rc)
58 {
59 if (rc != GRN_SUCCESS) {
60 return rc;
61 }
62 return ctx->rc;
63 }
64
65 GRN_API bool
66 grn_ctx_impl_should_log(grn_ctx *ctx);
67 GRN_API void
68 grn_ctx_impl_set_current_error_message(grn_ctx *ctx);
69
70 GRN_API void
71 grn_ctx_log(grn_ctx *ctx, const char *fmt, ...) GRN_ATTRIBUTE_PRINTF(2);
72 GRN_API void
73 grn_ctx_logv(grn_ctx *ctx, const char *fmt, va_list ap);
74 GRN_API void
75 grn_ctx_log_back_trace(grn_ctx *ctx, grn_log_level level);
76
77 static inline void
78 grn_error_setv(grn_ctx *ctx,
79 grn_log_level level,
80 grn_rc rc,
81 const char *file,
82 uint32_t line,
83 const char *function,
84 const char *format,
85 va_list args)
86 {
87 ctx->errlvl = (unsigned char)level;
88 if (ctx->rc != GRN_CANCEL) {
89 ctx->rc = rc;
90 }
91 ctx->errfile = file;
92 ctx->errline = line;
93 ctx->errfunc = function;
94 va_list logger_putv_args;
95 va_copy(logger_putv_args, args);
96 grn_ctx_logv(ctx, format, args);
97 if (grn_ctx_impl_should_log(ctx)) {
98 grn_ctx_impl_set_current_error_message(ctx);
99 if (grn_logger_pass(ctx, level)) {
100 grn_logger_putv(ctx,
101 level,
102 file,
103 line,
104 function,
105 format,
106 logger_putv_args);
107 }
108 if (level <= GRN_LOG_ERROR) {
109 grn_ctx_log_back_trace(ctx, level);
110 }
111 }
112 va_end(logger_putv_args);
113 }
114
115 static inline void
116 grn_error_set(grn_ctx *ctx,
117 grn_log_level level,
118 grn_rc rc,
119 const char *file,
120 uint32_t line,
121 const char *function,
122 const char *format,
123 ...) GRN_ATTRIBUTE_PRINTF(7);
124 static inline void
125 grn_error_set(grn_ctx *ctx,
126 grn_log_level level,
127 grn_rc rc,
128 const char *file,
129 uint32_t line,
130 const char *function,
131 const char *format,
132 ...)
133 {
134 va_list args;
135 va_start(args, format);
136 grn_error_setv(ctx, level, rc, file, line, function, format, args);
137 va_end(args);
138 }
139
140 #define ERRSET(ctx, lvl, r, ...) \
141 grn_error_set((ctx), \
142 (lvl), \
143 (r), \
144 __FILE__, \
145 __LINE__, \
146 __FUNCTION__, \
147 __VA_ARGS__)
148
149 #define ERRP(ctx, lvl) \
150 (((ctx) && ((grn_ctx *)(ctx))->errlvl <= (lvl)) || (grn_gctx.errlvl <= (lvl)))
151
152 #ifdef ERR
153 # undef ERR
154 #endif /* ERR */
155 #define CRIT(rc, ...) ERRSET(ctx, GRN_CRIT, (rc), __VA_ARGS__)
156 #define ERR(rc, ...) ERRSET(ctx, GRN_ERROR, (rc), __VA_ARGS__)
157 #define WARN(rc, ...) ERRSET(ctx, GRN_WARN, (rc), __VA_ARGS__)
158 #define MERR(...) ERRSET(ctx, GRN_ALERT, GRN_NO_MEMORY_AVAILABLE, __VA_ARGS__)
159 #define ALERT(...) ERRSET(ctx, GRN_ALERT, GRN_SUCCESS, __VA_ARGS__)
160
161 #define USER_MESSAGE_SIZE 1024
162
163 #ifdef WIN32
164
165 # define SERR(...) \
166 do { \
167 grn_rc rc; \
168 int error_code; \
169 const char *system_message; \
170 char user_message[USER_MESSAGE_SIZE]; \
171 error_code = GetLastError(); \
172 system_message = grn_error_get_current_system_message(); \
173 rc = grn_windows_error_code_to_rc(error_code); \
174 grn_snprintf(user_message, \
175 USER_MESSAGE_SIZE, \
176 USER_MESSAGE_SIZE, \
177 __VA_ARGS__); \
178 ERR(rc, \
179 "system error[%d]: %s: %s", \
180 error_code, \
181 system_message, \
182 user_message); \
183 } while (0)
184
185 # define SOERR(...) \
186 do { \
187 grn_rc rc; \
188 const char *m; \
189 char user_message[USER_MESSAGE_SIZE]; \
190 int e = WSAGetLastError(); \
191 switch (e) { \
192 case WSANOTINITIALISED: \
193 rc = GRN_SOCKET_NOT_INITIALIZED; \
194 m = "please call grn_com_init first"; \
195 break; \
196 case WSAEFAULT: \
197 rc = GRN_BAD_ADDRESS; \
198 m = "bad address"; \
199 break; \
200 case WSAEINVAL: \
201 rc = GRN_INVALID_ARGUMENT; \
202 m = "invalid argument"; \
203 break; \
204 case WSAEMFILE: \
205 rc = GRN_TOO_MANY_OPEN_FILES; \
206 m = "too many sockets"; \
207 break; \
208 case WSAEWOULDBLOCK: \
209 rc = GRN_OPERATION_WOULD_BLOCK; \
210 m = "operation would block"; \
211 break; \
212 case WSAENOTSOCK: \
213 rc = GRN_NOT_SOCKET; \
214 m = "given fd is not socket fd"; \
215 break; \
216 case WSAEOPNOTSUPP: \
217 rc = GRN_OPERATION_NOT_SUPPORTED; \
218 m = "operation is not supported"; \
219 break; \
220 case WSAEADDRINUSE: \
221 rc = GRN_ADDRESS_IS_IN_USE; \
222 m = "address is already in use"; \
223 break; \
224 case WSAEADDRNOTAVAIL: \
225 rc = GRN_ADDRESS_IS_NOT_AVAILABLE; \
226 m = "address is not available"; \
227 break; \
228 case WSAENETDOWN: \
229 rc = GRN_NETWORK_IS_DOWN; \
230 m = "network is down"; \
231 break; \
232 case WSAENOBUFS: \
233 rc = GRN_NO_BUFFER; \
234 m = "no buffer"; \
235 break; \
236 case WSAEISCONN: \
237 rc = GRN_SOCKET_IS_ALREADY_CONNECTED; \
238 m = "socket is already connected"; \
239 break; \
240 case WSAENOTCONN: \
241 rc = GRN_SOCKET_IS_NOT_CONNECTED; \
242 m = "socket is not connected"; \
243 break; \
244 case WSAESHUTDOWN: \
245 rc = GRN_SOCKET_IS_ALREADY_SHUTDOWNED; \
246 m = "socket is already shutdowned"; \
247 break; \
248 case WSAETIMEDOUT: \
249 rc = GRN_OPERATION_TIMEOUT; \
250 m = "connection time out"; \
251 break; \
252 case WSAECONNREFUSED: \
253 rc = GRN_CONNECTION_REFUSED; \
254 m = "connection refused"; \
255 break; \
256 case WSAEINTR: \
257 rc = GRN_INTERRUPTED_FUNCTION_CALL; \
258 m = "interrupted function call"; \
259 break; \
260 case WSAECONNRESET: \
261 rc = GRN_CONNECTION_RESET; \
262 m = "connection reset by peer"; \
263 break; \
264 default: \
265 rc = GRN_UNKNOWN_ERROR; \
266 m = "unknown error"; \
267 break; \
268 } \
269 grn_snprintf(user_message, \
270 USER_MESSAGE_SIZE, \
271 USER_MESSAGE_SIZE, \
272 __VA_ARGS__); \
273 ERR(rc, "socket error[%d]: %s: %s", e, m, user_message); \
274 } while (0)
275
276 # define ERRNO_ERR(...) \
277 do { \
278 grn_rc rc; \
279 int errno_keep = errno; \
280 bool show_errno = false; \
281 const char *system_message; \
282 char user_message[USER_MESSAGE_SIZE]; \
283 system_message = grn_strerror(errno); \
284 switch (errno_keep) { \
285 case EPERM: \
286 rc = GRN_OPERATION_NOT_PERMITTED; \
287 break; \
288 case ENOENT: \
289 rc = GRN_NO_SUCH_FILE_OR_DIRECTORY; \
290 break; \
291 case ESRCH: \
292 rc = GRN_NO_SUCH_PROCESS; \
293 break; \
294 case EINTR: \
295 rc = GRN_INTERRUPTED_FUNCTION_CALL; \
296 break; \
297 case EIO: \
298 rc = GRN_INPUT_OUTPUT_ERROR; \
299 break; \
300 case E2BIG: \
301 rc = GRN_ARG_LIST_TOO_LONG; \
302 break; \
303 case ENOEXEC: \
304 rc = GRN_EXEC_FORMAT_ERROR; \
305 break; \
306 case EBADF: \
307 rc = GRN_BAD_FILE_DESCRIPTOR; \
308 break; \
309 case ECHILD: \
310 rc = GRN_NO_CHILD_PROCESSES; \
311 break; \
312 case EAGAIN: \
313 rc = GRN_OPERATION_WOULD_BLOCK; \
314 break; \
315 case ENOMEM: \
316 rc = GRN_NO_MEMORY_AVAILABLE; \
317 break; \
318 case EACCES: \
319 rc = GRN_PERMISSION_DENIED; \
320 break; \
321 case EFAULT: \
322 rc = GRN_BAD_ADDRESS; \
323 break; \
324 case EEXIST: \
325 rc = GRN_FILE_EXISTS; \
326 break; \
327 /* case EXDEV : */ \
328 case ENODEV: \
329 rc = GRN_NO_SUCH_DEVICE; \
330 break; \
331 case ENOTDIR: \
332 rc = GRN_NOT_A_DIRECTORY; \
333 break; \
334 case EISDIR: \
335 rc = GRN_IS_A_DIRECTORY; \
336 break; \
337 case EINVAL: \
338 rc = GRN_INVALID_ARGUMENT; \
339 break; \
340 case EMFILE: \
341 rc = GRN_TOO_MANY_OPEN_FILES; \
342 break; \
343 case ENOTTY: \
344 rc = GRN_INAPPROPRIATE_I_O_CONTROL_OPERATION; \
345 break; \
346 case EFBIG: \
347 rc = GRN_FILE_TOO_LARGE; \
348 break; \
349 case ENOSPC: \
350 rc = GRN_NO_SPACE_LEFT_ON_DEVICE; \
351 break; \
352 case ESPIPE: \
353 rc = GRN_INVALID_SEEK; \
354 break; \
355 case EROFS: \
356 rc = GRN_READ_ONLY_FILE_SYSTEM; \
357 break; \
358 case EMLINK: \
359 rc = GRN_TOO_MANY_LINKS; \
360 break; \
361 case EPIPE: \
362 rc = GRN_BROKEN_PIPE; \
363 break; \
364 case EDOM: \
365 rc = GRN_DOMAIN_ERROR; \
366 break; \
367 case ERANGE: \
368 rc = GRN_RANGE_ERROR; \
369 break; \
370 case EDEADLOCK: \
371 rc = GRN_RESOURCE_DEADLOCK_AVOIDED; \
372 break; \
373 case ENAMETOOLONG: \
374 rc = GRN_FILENAME_TOO_LONG; \
375 break; \
376 case EILSEQ: \
377 rc = GRN_ILLEGAL_BYTE_SEQUENCE; \
378 break; \
379 case ECONNRESET: \
380 rc = GRN_CONNECTION_RESET; \
381 break; \
382 /* case STRUNCATE : */ \
383 default: \
384 rc = GRN_UNKNOWN_ERROR; \
385 show_errno = true; \
386 break; \
387 } \
388 grn_snprintf(user_message, \
389 USER_MESSAGE_SIZE, \
390 USER_MESSAGE_SIZE, \
391 __VA_ARGS__); \
392 if (show_errno) { \
393 ERR(rc, \
394 "system call error[%d]: %s: %s", \
395 errno_keep, \
396 system_message, \
397 user_message); \
398 } else { \
399 ERR(rc, "system call error: %s: %s", system_message, user_message); \
400 } \
401 } while (0)
402
403 #else /* WIN32 */
404
405 # ifdef ESHUTDOWN
406 # define GRN_ESHUTDOWN ESHUTDOWN
407 # else /* e.g. __wasi__ */
408 # define GRN_ESHUTDOWN 9999 /* unused errno value */
409 # endif
410
411 # define SERR(...) \
412 do { \
413 grn_rc rc; \
414 int errno_keep = errno; \
415 bool show_errno = false; \
416 const char *system_message = grn_error_get_current_system_message(); \
417 char user_message[USER_MESSAGE_SIZE]; \
418 switch (errno_keep) { \
419 case ELOOP: \
420 rc = GRN_TOO_MANY_SYMBOLIC_LINKS; \
421 break; \
422 case ENAMETOOLONG: \
423 rc = GRN_FILENAME_TOO_LONG; \
424 break; \
425 case ENOENT: \
426 rc = GRN_NO_SUCH_FILE_OR_DIRECTORY; \
427 break; \
428 case ENOMEM: \
429 rc = GRN_NO_MEMORY_AVAILABLE; \
430 break; \
431 case ENOTDIR: \
432 rc = GRN_NOT_A_DIRECTORY; \
433 break; \
434 case EPERM: \
435 rc = GRN_OPERATION_NOT_PERMITTED; \
436 break; \
437 case ESRCH: \
438 rc = GRN_NO_SUCH_PROCESS; \
439 break; \
440 case EINTR: \
441 rc = GRN_INTERRUPTED_FUNCTION_CALL; \
442 break; \
443 case EIO: \
444 rc = GRN_INPUT_OUTPUT_ERROR; \
445 break; \
446 case ENXIO: \
447 rc = GRN_NO_SUCH_DEVICE_OR_ADDRESS; \
448 break; \
449 case E2BIG: \
450 rc = GRN_ARG_LIST_TOO_LONG; \
451 break; \
452 case ENOEXEC: \
453 rc = GRN_EXEC_FORMAT_ERROR; \
454 break; \
455 case EBADF: \
456 rc = GRN_BAD_FILE_DESCRIPTOR; \
457 break; \
458 case ECHILD: \
459 rc = GRN_NO_CHILD_PROCESSES; \
460 break; \
461 case EACCES: \
462 rc = GRN_PERMISSION_DENIED; \
463 break; \
464 case EFAULT: \
465 rc = GRN_BAD_ADDRESS; \
466 break; \
467 case EBUSY: \
468 rc = GRN_RESOURCE_BUSY; \
469 break; \
470 case EEXIST: \
471 rc = GRN_FILE_EXISTS; \
472 break; \
473 case ENODEV: \
474 rc = GRN_NO_SUCH_DEVICE; \
475 break; \
476 case EISDIR: \
477 rc = GRN_IS_A_DIRECTORY; \
478 break; \
479 case EINVAL: \
480 rc = GRN_INVALID_ARGUMENT; \
481 break; \
482 case EMFILE: \
483 rc = GRN_TOO_MANY_OPEN_FILES; \
484 break; \
485 case EFBIG: \
486 rc = GRN_FILE_TOO_LARGE; \
487 break; \
488 case ENOSPC: \
489 rc = GRN_NO_SPACE_LEFT_ON_DEVICE; \
490 break; \
491 case EROFS: \
492 rc = GRN_READ_ONLY_FILE_SYSTEM; \
493 break; \
494 case EMLINK: \
495 rc = GRN_TOO_MANY_LINKS; \
496 break; \
497 case EPIPE: \
498 rc = GRN_BROKEN_PIPE; \
499 break; \
500 case EDOM: \
501 rc = GRN_DOMAIN_ERROR; \
502 break; \
503 case ERANGE: \
504 rc = GRN_RANGE_ERROR; \
505 break; \
506 case ENOTSOCK: \
507 rc = GRN_NOT_SOCKET; \
508 break; \
509 case EADDRINUSE: \
510 rc = GRN_ADDRESS_IS_IN_USE; \
511 break; \
512 case ENETDOWN: \
513 rc = GRN_NETWORK_IS_DOWN; \
514 break; \
515 case ENOBUFS: \
516 rc = GRN_NO_BUFFER; \
517 break; \
518 case EISCONN: \
519 rc = GRN_SOCKET_IS_ALREADY_CONNECTED; \
520 break; \
521 case ENOTCONN: \
522 rc = GRN_SOCKET_IS_NOT_CONNECTED; \
523 break; \
524 /* \
525 case ESOCKTNOSUPPORT : \
526 case EOPNOTSUPP : \
527 case EPFNOSUPPORT : \
528 */ \
529 case EPROTONOSUPPORT: \
530 rc = GRN_OPERATION_NOT_SUPPORTED; \
531 break; \
532 case GRN_ESHUTDOWN: \
533 rc = GRN_SOCKET_IS_ALREADY_SHUTDOWNED; \
534 break; \
535 case ETIMEDOUT: \
536 rc = GRN_OPERATION_TIMEOUT; \
537 break; \
538 case ECONNREFUSED: \
539 rc = GRN_CONNECTION_REFUSED; \
540 break; \
541 case EAGAIN: \
542 rc = GRN_OPERATION_WOULD_BLOCK; \
543 break; \
544 case ECONNRESET: \
545 rc = GRN_CONNECTION_RESET; \
546 break; \
547 default: \
548 rc = GRN_UNKNOWN_ERROR; \
549 show_errno = true; \
550 break; \
551 } \
552 grn_snprintf(user_message, \
553 USER_MESSAGE_SIZE, \
554 USER_MESSAGE_SIZE, \
555 __VA_ARGS__); \
556 if (show_errno) { \
557 ERR(rc, \
558 "system call error[%d]: %s: %s", \
559 errno_keep, \
560 system_message, \
561 user_message); \
562 } else { \
563 ERR(rc, "system call error: %s: %s", system_message, user_message); \
564 } \
565 } while (0)
566
567 # define SOERR(...) SERR(__VA_ARGS__)
568
569 # define ERRNO_ERR(...) SERR(__VA_ARGS__)
570
571 #endif /* WIN32 */
572
573 #define GERR(rc, ...) ERRSET(&grn_gctx, GRN_ERROR, (rc), __VA_ARGS__)
574 #define GMERR(...) \
575 ERRSET(&grn_gctx, GRN_ALERT, GRN_NO_MEMORY_AVAILABLE, __VA_ARGS__)
576
577 GRN_API const char *
578 grn_strerror(int error_code);
579
580 #ifdef __cplusplus
581 }
582 #endif