gdebug.c
5.28 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005,
2007, 2008 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This 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 General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <mailutils/assoc.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/argcv.h>
#include <mailutils/debug.h>
#include <mailutils/cfg.h>
#include <mailutils/nls.h>
int mu_debug_line_info = 0;
struct debug_level
{
unsigned level;
};
static mu_assoc_t debug_table;
mu_log_level_t
mu_global_debug_level (const char *object_name)
{
struct debug_level *p = mu_assoc_ref (debug_table, object_name);
if (p)
return p->level;
return 0;
}
int
mu_global_debug_set_level (const char *object_name, mu_log_level_t level)
{
int rc;
struct debug_level *dbg;
if (!debug_table)
{
rc = mu_assoc_create (&debug_table, sizeof(struct debug_level), 0);
if (rc)
return rc;
}
rc = mu_assoc_ref_install (debug_table, object_name, (void**) &dbg);
if (rc == 0 || rc == MU_ERR_EXISTS)
dbg->level = level;
return rc;
}
int
mu_global_debug_clear_level (const char *object_name)
{
int rc = 0;
if (!object_name)
mu_assoc_clear (debug_table);
else
rc = mu_assoc_remove (debug_table, object_name);
return rc;
}
int
decode_debug_level (const char *p, int *lev)
{
if (strcmp (p, "error") == 0)
*lev = MU_DEBUG_ERROR;
else if (strncmp (p, "trace", 5) == 0 && isdigit (p[5]) && p[6] == 0)
*lev = MU_DEBUG_TRACE0 + atoi (p + 5);
else if (strcmp (p, "proto") == 0)
*lev = MU_DEBUG_PROT;
else
return 1;
return 0;
}
int
mu_debug_level_from_string (const char *string, mu_log_level_t *plev,
mu_debug_t debug)
{
char *q;
unsigned level = MU_DEBUG_INHERIT;
if (isdigit (*string))
{
level = strtoul (string, &q, 0);
if (*q)
{
mu_cfg_format_error (debug, MU_DEBUG_ERROR,
_("invalid debugging specification `%s': "
"expected levels or number after `=', "
"but found `%s'"),
string, string);
return MU_ERR_FAILURE;
}
}
else
{
char *p = strdup (string);
for (q = strtok (p, ","); q; q = strtok (NULL, ","))
{
int flag;
int revert = 0;
int upto = 0;
if (*q == '!')
{
q++;
revert = 1;
}
if (*q == '<')
{
q++;
upto = 1;
}
if (decode_debug_level (q, &flag))
mu_cfg_format_error (debug, MU_DEBUG_ERROR,
_("invalid debugging level `%s'"),
q);
else if (revert)
{
if (upto)
level &= ~MU_DEBUG_LEVEL_UPTO (flag);
else
level &= ~MU_DEBUG_LEVEL_MASK (flag);
}
else
{
if (upto)
level |= MU_DEBUG_LEVEL_UPTO (flag);
else
level |= MU_DEBUG_LEVEL_MASK (flag);
}
}
free (p);
}
*plev = level;
return 0;
}
int
mu_global_debug_from_string (const char *string, const char *errpfx)
{
int rc;
int argc;
char **argv;
int i;
rc = mu_argcv_get (string, ";", NULL, &argc, &argv);
if (rc)
return rc;
for (i = 0; i < argc; i++)
{
char *p;
mu_log_level_t level = MU_DEBUG_INHERIT;
char *object_name = argv[i];
for (p = object_name; *p && *p != '='; p++)
;
if (*p == '=')
{
/* FIXME: Use mu_debug_level_from_string */
char *q;
*p++ = 0;
if (isdigit (*p))
{
level = strtoul (p, &q, 0);
if (*q)
{
mu_error ("%s: invalid debugging specification `%s': "
"expected levels or number after `=', "
"but found `%s'",
errpfx, argv[i], p);
break;
}
}
else
{
char *q;
for (q = strtok (p, ","); q; q = strtok (NULL, ","))
{
int flag;
int revert = 0;
int upto = 0;
if (*q == '!')
{
q++;
revert = 1;
}
if (*q == '<')
{
q++;
upto = 1;
}
if (decode_debug_level (q, &flag))
mu_error ("%s: invalid debugging level `%s'",
errpfx, q);
else if (revert)
{
if (upto)
level &= ~MU_DEBUG_LEVEL_UPTO (flag);
else
level &= ~MU_DEBUG_LEVEL_MASK (flag);
}
else
{
if (upto)
level |= MU_DEBUG_LEVEL_UPTO (flag);
else
level |= MU_DEBUG_LEVEL_MASK (flag);
}
}
}
}
else
level |= MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT);
if (p[-1] == ':')
{
p[-1] = 0;
level &= ~MU_DEBUG_INHERIT;
}
mu_global_debug_set_level (object_name, level);
}
mu_argcv_free (argc, argv);
return 0;
}