-
Notifications
You must be signed in to change notification settings - Fork 1
/
CirLog.c
208 lines (180 loc) · 3.75 KB
/
CirLog.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
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
#include "cir_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#define MAX_LOCATIONS 60
typedef struct Location {
CirName filename;
uint32_t line;
} Location;
static Location locationStack[MAX_LOCATIONS];
static size_t locationStackTop;
static Location realLocation;
static uint32_t logLevel;
static bool hasNewline;
void
CirLog_begin(uint32_t level)
{
assert(!logLevel);
switch (level) {
case CIRLOG_DEBUG:
fputs("debug: ", stderr);
break;
case CIRLOG_INFO:
fputs("info: ", stderr);
break;
case CIRLOG_WARN:
fputs("warning: ", stderr);
break;
case CIRLOG_ERROR:
fputs("error: ", stderr);
break;
case CIRLOG_FATAL:
fputs("FATAL: ", stderr);
break;
case CIRLOG_BUG:
fputs("BUG: ", stderr);
break;
}
hasNewline = false;
logLevel = level;
}
void
CirLog_end(void)
{
assert(logLevel);
// Complete newline
if (!hasNewline)
fputs("\n", stderr);
// Print location information
for (size_t i = 0; i < locationStackTop; i++) {
fprintf(stderr, " %s %s:%u\n", !i ? "in" : "included by",
CirName_cstr(locationStack[locationStackTop - i - 1].filename),
locationStack[locationStackTop - i - 1].line);
}
fprintf(stderr, " in real location %s:%u\n",
CirName_cstr(realLocation.filename),
realLocation.line);
fflush(stderr);
logLevel = 0;
}
void
CirLog_printb(const void *s, size_t len)
{
fwrite(s, len, 1, stderr);
}
void
CirLog_print(const char *s)
{
fputs(s, stderr);
}
void
CirLog_printf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
CirLog_vprintf(fmt, va);
va_end(va);
}
void
CirLog_vprintf(const char *fmt, va_list va)
{
vfprintf(stderr, fmt, va);
}
void
CirLog_printq(const char *s)
{
CirLog_printqb(s, strlen(s));
}
void
CirLog_printqb(const char *s, size_t len)
{
fputs("\"", stderr);
for (size_t i = 0; i < len; i++) {
uint8_t c = s[i];
fputs(CirQuote__table[c], stderr);
if (c >= 127)
fputs("\"\"", stderr);
}
fputs("\"", stderr);
}
void
cir_fatal(const char *fmt, ...)
{
va_list va;
CirLog_begin(CIRLOG_FATAL);
va_start(va, fmt);
CirLog_vprintf(fmt, va);
va_end(va);
CirLog_end();
exit(1);
}
void
cir_bug(const char *fmt, ...)
{
va_list va;
CirLog_begin(CIRLOG_BUG);
va_start(va, fmt);
CirLog_vprintf(fmt, va);
va_end(va);
CirLog_end();
abort();
}
void
cir_warn(const char *fmt, ...)
{
va_list va;
CirLog_begin(CIRLOG_WARN);
va_start(va, fmt);
CirLog_vprintf(fmt, va);
va_end(va);
CirLog_end();
}
void
cir_log(const char *fmt, ...)
{
va_list va;
CirLog_begin(CIRLOG_DEBUG);
va_start(va, fmt);
CirLog_vprintf(fmt, va);
va_end(va);
CirLog_end();
}
void
CirLog__announceNewLine(void)
{
if (locationStackTop) {
locationStack[locationStackTop - 1].line++;
}
realLocation.line++;
}
void
CirLog__pushLocation(CirName filename, uint32_t line)
{
Location loc = { filename, line };
if (locationStackTop >= MAX_LOCATIONS)
cir_fatal("location stack too large");
locationStack[locationStackTop++] = loc;
}
void
CirLog__popLocation(void)
{
if (!locationStackTop)
cir_bug("location stack is empty");
locationStackTop--;
}
void
CirLog__setLocation(CirName filename, uint32_t line)
{
if (!locationStackTop)
cir_bug("location stack is empty");
locationStack[locationStackTop-1].filename = filename;
locationStack[locationStackTop-1].line = line;
}
void
CirLog__setRealLocation(CirName filename, uint32_t line)
{
realLocation.filename = filename;
realLocation.line = line;
}