-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.h
186 lines (166 loc) · 5.74 KB
/
error.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*------------------------------------------------------------------------
* Copyright 2007-2008 (c) Jeff Brown <spadix@users.sourceforge.net>
*
* This file is part of the Zebra Barcode Library.
*
* The Zebra Barcode Library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* The Zebra Barcode Library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with the Zebra Barcode Library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* http://sourceforge.net/projects/zebra
*------------------------------------------------------------------------*/
#ifndef _ERROR_H_
#define _ERROR_H_
#include "config.h"
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "zebra.h"
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
#endif
#define ERRINFO_MAGIC (0x5252457a) /* "zERR" (LE) */
typedef enum errsev_e {
SEV_FATAL = -2, /* application must terminate */
SEV_ERROR = -1, /* might be able to recover and continue */
SEV_OK = 0,
SEV_WARNING = 1, /* unexpected condition */
SEV_NOTE = 2, /* fyi */
} errsev_t;
typedef enum errmodule_e {
ZEBRA_MOD_PROCESSOR,
ZEBRA_MOD_VIDEO,
ZEBRA_MOD_WINDOW,
ZEBRA_MOD_IMAGE_SCANNER,
ZEBRA_MOD_UNKNOWN,
} errmodule_t;
typedef struct errinfo_s {
uint32_t magic; /* just in case */
errmodule_t module; /* reporting module */
char *buf; /* formatted and passed to application */
int errnum; /* errno for system errors */
errsev_t sev;
zebra_error_t type;
const char *func; /* reporting function */
const char *detail; /* description */
char *arg_str; /* single string argument */
int arg_int; /* single integer argument */
} errinfo_t;
extern int _zebra_verbosity;
/* FIXME don't we need varargs hacks here? */
#define zprintf(level, format, ...) do { \
if(_zebra_verbosity >= level) \
fprintf(stderr, "%s: " format, __func__ , ##__VA_ARGS__); \
} while(0)
static inline int err_copy (void *dst_c,
void *src_c)
{
errinfo_t *dst = dst_c;
errinfo_t *src = src_c;
assert(dst->magic == ERRINFO_MAGIC);
assert(src->magic == ERRINFO_MAGIC);
dst->errnum = src->errnum;
dst->sev = src->sev;
dst->type = src->type;
dst->func = src->func;
dst->detail = src->detail;
dst->arg_str = src->arg_str;
src->arg_str = NULL; /* unused at src, avoid double free */
dst->arg_int = src->arg_int;
return(-1);
}
static inline int err_capture (const void *container,
errsev_t sev,
zebra_error_t type,
const char *func,
const char *detail)
{
errinfo_t *err = (errinfo_t*)container;
assert(err->magic == ERRINFO_MAGIC);
if(type == ZEBRA_ERR_SYSTEM)
err->errnum = errno;
err->sev = sev;
err->type = type;
err->func = func;
err->detail = detail;
if(_zebra_verbosity >= 1)
_zebra_error_spew(err, 0);
return(-1);
}
static inline int err_capture_str (const void *container,
errsev_t sev,
zebra_error_t type,
const char *func,
const char *detail,
const char *arg)
{
errinfo_t *err = (errinfo_t*)container;
assert(err->magic == ERRINFO_MAGIC);
if(err->arg_str)
free(err->arg_str);
err->arg_str = strdup(arg);
return(err_capture(container, sev, type, func, detail));
}
static inline int err_capture_int (const void *container,
errsev_t sev,
zebra_error_t type,
const char *func,
const char *detail,
int arg)
{
errinfo_t *err = (errinfo_t*)container;
assert(err->magic == ERRINFO_MAGIC);
err->arg_int = arg;
return(err_capture(container, sev, type, func, detail));
}
static inline int err_capture_num (const void *container,
errsev_t sev,
zebra_error_t type,
const char *func,
const char *detail,
int num)
{
errinfo_t *err = (errinfo_t*)container;
assert(err->magic == ERRINFO_MAGIC);
err->errnum = num;
return(err_capture(container, sev, type, func, detail));
}
static inline void err_init (errinfo_t *err,
errmodule_t module)
{
err->magic = ERRINFO_MAGIC;
err->module = module;
}
static inline void err_cleanup (errinfo_t *err)
{
assert(err->magic == ERRINFO_MAGIC);
if(err->buf) {
free(err->buf);
err->buf = NULL;
}
if(err->arg_str) {
free(err->arg_str);
err->arg_str = NULL;
}
}
#endif