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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* debug.cc
*
* Wed Feb 11 11:22:12 CET 2009
* Copyright 2009 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Pracro 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "log.h"
#include <pthread.h>
#include "mutex.h"
static Mutex mutex;
static unsigned int gettid()
{
return (unsigned int)pthread_self();
}
static FILE *logfp = stderr;
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
struct __debug_channel
{
char name[32];
unsigned flags;
};
static const char * const debug_class_str[] =
{ "fixme", "err", "warn", "info", "debug" };
#define __DEBUG_CHANNEL_MAX 256
static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX];
static unsigned n_debug_channel = 0;
static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme);
static int __debug_enabled(const enum __debug_class cl, const char *ch)
{
unsigned i;
for(i = 0; i < n_debug_channel; i++) {
if(!strcmp(ch, debug_channel[i].name)) {
return (debug_channel[i].flags & (1 << cl)) != 0;
}
}
return debug_flags & (1 << cl);
}
#ifdef WITH_DEBUG
int __debug(const char *func, const int line,
const enum __debug_class cl,
const char *ch, const char *fmt, ...)
{
MutexAutolock m(mutex);
int ret = 0;
if(__debug_enabled(cl, ch)) {
if((unsigned)cl < NELEM(debug_class_str))
ret += fprintf(logfp, "%u %s:%s:%s:%d ",
gettid(), debug_class_str[(unsigned)cl], ch, func, line);
if(fmt) {
va_list va;
va_start(va, fmt);
ret += vfprintf(logfp, fmt, va);
va_end(va);
}
}
if(ret){
fflush(logfp);
}
return ret;
}
int __debug_va(const char *func, const int line,
const enum __debug_class cl,
const char *ch, const char *fmt, va_list va)
{
MutexAutolock m(mutex);
int ret = 0;
if(__debug_enabled(cl, ch)) {
if((unsigned)cl < NELEM(debug_class_str))
ret += fprintf(logfp, "%u %s:%s:%s:%d ",
gettid(), debug_class_str[(unsigned)cl], ch, func, line);
if(fmt)
ret += vfprintf(logfp, fmt, va);
}
if(ret) {
fflush(logfp);
}
return ret;
}
#endif
int __log(const char *func, const int line, const enum __debug_class cl,
const char *ch, const char *fmt, ...)
{
MutexAutolock m(mutex);
std::string logmsg;
char str[8];
#ifdef WITH_DEBUG
int ret = 0;
#endif
if(__debug_enabled(cl, ch)) {
if((unsigned)cl < NELEM(debug_class_str))
if((unsigned)cl < NELEM(debug_class_str))
#ifdef WITH_DEBUG
ret = fprintf(logfp, "%u %s:%s:%s:%d ",
gettid(), debug_class_str[(unsigned)cl], ch, func, line);
#endif
sprintf(str, "%d", line);
logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str;
if(fmt) {
va_list va;
va_start(va, fmt);
#ifdef WITH_DEBUG
ret += vfprintf(logfp, fmt, va);
#endif
char* ptr;
if(vasprintf(&ptr, fmt, va) == -1) {}
logmsg += ptr;
va_end(va);
}
}
#ifdef WITH_DEBUG
if(ret) {
fprintf(logfp, "\n");
fflush(logfp);
}
#endif
log(logmsg);
return logmsg.size();
}
int __log_va(const char *func, const int line, const enum __debug_class cl,
const char *ch, const char *fmt, va_list va)
{
MutexAutolock m(mutex);
std::string logmsg;
char str[8];
#ifdef WITH_DEBUG
int ret = 0;
#endif
if(__debug_enabled(cl, ch)) {
if((unsigned)cl < NELEM(debug_class_str))
#ifdef WITH_DEBUG
ret = fprintf(logfp, "%u %s:%s:%s:%d ",
gettid(), debug_class_str[(unsigned)cl], ch, func, line);
#endif
sprintf(str, "%d", line);
logmsg = std::string(debug_class_str[(unsigned)cl]) + ":" + ch + ":" + func + ":" + str;
if(fmt) {
#ifdef WITH_DEBUG
ret += vfprintf(logfp, fmt, va);
#endif
char* ptr;
if(vasprintf(&ptr, fmt, va) == -1) {}
logmsg += ptr;
}
}
#ifdef WITH_DEBUG
if(ret) {
fprintf(logfp, "\n");
fflush(logfp);
}
#endif
return logmsg.size();
}
void debug_init(FILE *fp)
{
mutex.name = "debug";
MutexAutolock m(mutex);
logfp = fp;
}
void debug_reinit(const char *logfile)
{
MutexAutolock m(mutex);
if(logfp != stderr) {
fclose(logfp);
logfp = fopen(logfile, "a");
if(!logfp) {
fprintf(stderr, "Could not write to logfile: '%s'\n", logfile);
logfp = stderr;
}
}
}
/*
* fmt := [set[,set]*]*
* set := [+-]channel
* | class[+-]channel
* | [+-]all
*/
void debug_parse(const char *fmt)
{
MutexAutolock m(mutex);
char *s;
char *next;
char *opt;
if(!(s = strdup(fmt))) return;
for(opt = s; opt; opt = next) {
int set = 0;
int clr = 0;
unsigned i;
if((next = strchr(opt, ','))) *next++ = '\0';
char *p = opt + strcspn(opt, "+-");
if(!*p) p = opt; // All chars -> a channel name
if(p > opt) {
// we have a class
for(i = 0; i < NELEM(debug_class_str); i++) {
int n = strlen(debug_class_str[i]);
if(n != (p - opt)) continue;
if(!memcmp(opt, debug_class_str[i], n)) {
// Found the class
if(*p == '+')
set = 1 << i;
else
clr = 1 << i;
break;
}
}
if(i == NELEM(debug_class_str)) continue;
} else {
if(*p == '-')
clr = ~0;
else
set = ~0;
}
if(*p == '+' || *p == '-') p++;
if(!*p) continue;
if(!strcmp("all", p)) {
debug_flags = (debug_flags & ~clr) | set;
} else {
if(strlen(p) >= sizeof(debug_channel[0].name)) continue;
for(i = 0; i < n_debug_channel; i++) {
if(!strcmp(p, debug_channel[i].name)) {
debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set;
break;
}
}
if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) {
strcpy(debug_channel[i].name, p);
debug_channel[i].flags = (debug_flags & ~clr) | set;
n_debug_channel++;
}
}
}
free(s);
}
|