-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.c
143 lines (114 loc) · 2.31 KB
/
trace.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
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
/*
*
* $Id$
*
*/
#include "config.h"
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef STDC_HEADERS
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <errno.h>
#ifndef vfprintf
/* really don't need a decl if there's a macro of that name */
extern int vfprintf();
#endif
#include "cercs_env.h"
#include "bitarray.h"
static short int trace_init = 0;
static size_t trace_count = 0;
static const size_t max_traces = 128;
static bit_array_t* trace_bits;
size_t
cercs_trace_init ()
{
if (!trace_init) {
trace_bits = BitArrayCreate ( max_traces );
BitArrayClearAll ( trace_bits );
trace_count = 0;
trace_init++;
}
return max_traces;
}
extern void
cercs_trace_reset()
{
trace_init = 0;
BitArrayDestroy ( trace_bits );
cercs_trace_init();
}
extern size_t
cercs_trace_count()
{
return trace_count;
}
/*
takes env_var string, returns pos in bitarray
checks env_var and sets pos if env_var is set
*/
cercs_trace_t
cercs_trace_on (const char* env_var)
{
if (!trace_init) cercs_trace_init();
if ( getenv ( env_var ) ) {
BitArraySetBit ( trace_bits, trace_count );
}
return trace_count++;
}
extern void
cercs_trace_off (const cercs_trace_t trace_var)
{
if (!trace_init) cercs_trace_init();
BitArrayClearBit ( trace_bits, trace_var );
}
cercs_trace_t
cercs_trace_group_on ( const char* env_var, const cercs_trace_t group[],
const size_t group_size )
{
size_t v;
if (!trace_init) cercs_trace_init();
if ( getenv ( env_var ) ) {
BitArraySetBit ( trace_bits, trace_count );
for ( v = 0; v < group_size; v++ ) {
BitArraySetBit ( trace_bits, group[v] );
}
}
return trace_count++;
}
void
cercs_trace_out (const cercs_trace_t trace_type, char const *format, ...)
{
va_list ap;
if ( BitArrayTestBit ( trace_bits, trace_type ) ) {
#ifdef STDC_HEADERS
va_start(ap, format);
#else
va_start(ap);
#endif
vfprintf(stdout, format, ap);
va_end(ap);
printf("\n");
}
}
void
cercs_trace_check ( const cercs_trace_t trace, const char* env_var )
{
if (!trace_init) cercs_trace_init();
if (trace > trace_count) return;
if ( getenv ( env_var ) ) {
BitArraySetBit ( trace_bits, trace );
}
return;
}
/*
*
* $Log$
*
*/