forked from robertswiecki/intrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
117 lines (98 loc) · 2.73 KB
/
debug.c
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
/*
* intrace
*
* Debug routines
*
* author: Pawel Pisarczyk <[email protected]>
*/
/*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <intrace.h>
struct {
pthread_mutex_t mutex;
debug_level_t dl;
FILE *f;
} debug;
/* Function prints debug string on given debug level */
int _debug_printf(debug_level_t dl, const char *file, const char *func, int line, const char *fmt,
...)
{
struct tm tm;
char buf[4096], *p;
struct timeval tv;
struct timezone tz;
struct {
char *name;
char *color;
} dls[] = {
{
"<FATAL>", "\033[1;31m"}, {
"<ERROR>", "\033[0;31m"}, {
"<WARNING>", "\033[0;33m"}, {
"<INFO>", "\033[0;32m"}, {
"<DETAILS>", "\033[0m"}, {
"<DEBUG>", "\033[0;37m"}
};
va_list args;
va_start(args, fmt);
if (dl <= debug.dl) {
pthread_mutex_lock(&debug.mutex);
if ((debug.f == stdout) || (debug.f == stderr))
fprintf(debug.f, "%s", dls[dl].color);
gettimeofday(&tv, &tz);
localtime_r((const time_t *)&tv.tv_sec, &tm);
if (dl >= dlDebug)
snprintf(buf, sizeof(buf),
"%d/%02d/%02d %02d:%02d:%02d.%d %s (%s:%s %d) ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, dls[dl].name,
file, func, line);
else
snprintf(buf, sizeof(buf),
"%d/%02d/%02d %02d:%02d:%02d.%d %s ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, dls[dl].name);
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, args);
for (p = buf; *p; p++) {
if (!isascii(*p))
*p = '_';
}
fprintf(debug.f, "%s", buf);
if ((debug.f == stdout) || (debug.f == stderr))
fprintf(debug.f, "\033[0m");
fflush(debug.f);
pthread_mutex_unlock(&debug.mutex);
}
va_end(args);
return 0;
}
/* Function initializes debug */
int _debug_init(debug_level_t dl, char *l)
{
debug.dl = dl;
debug.f = stdout;
if (pthread_mutex_init(&debug.mutex, NULL) < 0)
return errMutex;
return 0;
}