-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebugutil.c
119 lines (100 loc) · 2.22 KB
/
debugutil.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
118
/* Written by Richard David Cook
at Lawrence Livermore National Laboratory
Contact: [email protected]
See license.txt for information about usage.
Spoiler alert: it's GNU opensource.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
#include "debugutil.h"
#include <time.h>
#include <sys/time.h>
static int iVerbose = 0;
static int iCheck = 0;
static FILE *gDebugFile = NULL;
#define INITDEBUGFILE if (!gDebugFile) gDebugFile = stderr
char *datestring(void) {
static char timebuf[32];
time_t now;
now = time(NULL);
strftime(timebuf, 31, "%d/%m/%y %H:%M:%S", (struct tm *)localtime(&now));
return timebuf;
}
void __dbfprintf(FILE *stream, int level, const char *fmt, ...){
if(iVerbose >= level){
vafprintf(stream, fmt);
/* va_list ap;
va_start(ap, fmt);
vfprintf(stream,fmt,ap);
va_end(ap);
*/
fflush(stream);
}
return;
}
void __dbprintf(int level, const char *fmt, ...){
if(iVerbose >= level){
INITDEBUGFILE;
vafprintf(gDebugFile, fmt);
/* va_list ap;
va_start(ap, fmt);
vfprintf(gDebugFile,fmt,ap);
va_end(ap);
*/
fflush(gDebugFile);
}
return;
}
static int check_verbose(void)
{
if (!iCheck) {
if (getenv("DEBUG_VERBOSE")) {
iVerbose = atoi(getenv("DEBUG_VERBOSE"));
}
iCheck = 1;
}
return(iVerbose);
}
int dbg_isverbose(void)
{
return(check_verbose());
}
void dbg_setverbose(int verbose)
{
iVerbose = verbose;
iCheck = 1;
}
void dbg_stderr(char *fmt, ...)
{
/*va_list ap;*/
if (check_verbose()) {
vafprintf(stderr, fmt);
/* va_start(ap, fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
*/
}
}
void dbg_maskstderr(int mask, char *fmt, ...)
{
/* va_list ap;*/
if (check_verbose() & mask) {
vafprintf(stderr, fmt);
/* va_start(ap, fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);*/
}
}
/* return 0 on failure, 1 on success */
int dbg_setfile(const char *dbgfile) {
INITDEBUGFILE;
if (!dbgfile) return 0;
FILE *tmpf = fopen(dbgfile, "w");
if (!tmpf) return 0;
gDebugFile = tmpf;
return 1;
}