-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.c
More file actions
441 lines (386 loc) · 13.6 KB
/
info.c
File metadata and controls
441 lines (386 loc) · 13.6 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "info.h"
#include "info_internal.h"
#include "info_def.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <time.h>
#define DECIMAL 10
#define DEFAULT_STR_SIZE (1<<10)
#define STR(x) [x]=#x,
const char *field_str[FIELDS_COUNT] = {
FIELDS(STR)
};
LIST_IMPL(struct info_DrawCall, DrawCall)
LIST_IMPL(struct info_Style, Style)
static const info_char* parse_color(struct info_Color *col, const info_char *text)
{
for(size_t i=0; i<COLORS_COUNT; i++){
size_t len = strlen(info_colors_str[i]);
if(!STRCMP(info_colors_str[i], text, len)){
*col = info_colors_val[i];
return text+len;
}
}
char *end;
col->r = strtoul(text, &end, DECIMAL);
if(!(*end == ',')) return NULL;
col->g = strtoul(end+1, &end, DECIMAL);
if(!(*end == ',')) return NULL;
col->b = strtoul(end+1, &end, DECIMAL);
if(!(*end == ')')) return NULL;
return end;
}
static const info_char* parse_style(struct info_Style *style, const info_char *text, const char *styles[])
{
style->kind = 0;
style->mode = 1;
if(*text == INFO_STR('!')){
style->mode = 0;
text++;
}
for(;style->kind<STYLES_COUNT; style->kind++){
if(!styles[style->kind]) continue;
size_t len = strlen(styles[style->kind]);
if(!strncmp(text, styles[style->kind], len)){
text += len;
switch(style->kind){
case FOREGROUND:
case BACKGROUND:
if( !style->mode || !(text = parse_color(&style->color, text)) ) return NULL;
if(*text++!=')') return NULL;
break;
case NORMAL:
style->mode = 0;
style->kind = INTENSITY;
break;
case BOLD:
style->mode = 1;
style->kind = INTENSITY;
break;
case FAINT:
style->mode = -1;
style->kind = INTENSITY;
break;
default:
break;
}
return text;
}
}
return NULL;
}
static const info_char* info_parse_styles(List_Style *list, const info_char *text, const info_char *end)
{
while( text < end ){
while(*text == INFO_STR(' ')) ++text;
struct info_Style style;
const info_char *next;
if(!(( next = parse_style(&style, text, info_styles_str_short) )
|| ( next = parse_style(&style, text, info_styles_str) ))) return NULL;
text = next;
List_Style_push(list, style);
while(*text == INFO_STR(' ')) ++text;
if(!(*text == INFO_STR(',') || *text == INFO_STR(':'))) return NULL;
text++;
}
return text;
}
static struct List_DrawCall* parse(const info_char *text, const info_char *end);
static const info_char* info_parse_group(struct info_DrawCall *dc, const info_char *text, const info_char *end)
{
const info_char *col = STRSTR(text, ":");
if(col && col < end){
if(!info_parse_styles(&dc->styled.styles, text, col) ){
while(dc->styled.styles) List_Style_pop(&dc->styled.styles);
return NULL;
}
const info_char *content_start = col+1, *content_end = end-1;
while(*content_start==INFO_STR(' ')) content_start++;
while(*content_end==INFO_STR(' ')) content_end--;
dc->styled.sub = parse(content_start, content_end+1);
} else {
// parse Field
dc->kind = FIELD;
for(size_t i=0; i<FIELDS_COUNT; i++){
size_t len = end-text;
if(!STRCMP(text, field_str[i], len)){
dc->field = i;
return text+len;
}
}
}
return end;
}
static const info_char* next_curly(const info_char *text, const info_char *end){
while(text<end) {
if(*text==INFO_STR('{')) return text;
if(*text==INFO_STR('}')) return text;
text++;
};
return NULL;
}
static const info_char* find_group_end(const info_char *text, const info_char *end){
int lvl = 0;
for(;;text++) {
text = next_curly(text, end);
if(!text) return NULL;
if(text<end-1 && text[0]==text[1]) {
text++;
continue;
}
if(*text=='{') lvl++;
else lvl--;
if(!lvl) return text;
}
}
static struct List_DrawCall* parse(const info_char *text, const info_char *end)
{
if(text>=end) return NULL;
const info_char *next = next_curly(text, end);
struct List_DrawCall *list = NULL;
int mark_error = 0;
struct info_DrawCall dc = { 0 };
if(!next){
list = NULL;
next = end;
} else {
if(next[0]==next[1]) {
// escape characters
list = parse(next+2, end);
dc = (struct info_DrawCall){.kind = TEXT, .text = {.str = next, .len = 1}};
} else if(*next==INFO_STR('{')) {
const info_char *cb = find_group_end(next, end);
if(!cb){
mark_error = 1;
dc = (struct info_DrawCall){.kind = TEXT, .text = {.str = next, .len = end-next}};
} else {
list = parse(cb+1, end);
if(!info_parse_group(&dc, next+1, cb)){
mark_error = 1;
dc = (struct info_DrawCall){.kind = TEXT, .text = {.str = next, .len = cb-next}};
}
}
} else if(*next==INFO_STR('}')){
dc = (struct info_DrawCall){.kind = TEXT, .text = {.str = "<}>", .len = 3}};
mark_error = 1;
}
if(mark_error){
// Mark Error Red
struct info_DrawCall error = { 0 };
error.kind = STYLE;
List_Style_push(&error.styled.styles, (struct info_Style){.kind=FOREGROUND, .color={255,0,0}});
List_Style_push(&error.styled.styles, (struct info_Style){.kind=STRIKE, .mode=1});
List_DrawCall_push(&error.styled.sub, dc);
List_DrawCall_push(&list, error);
} else {
List_DrawCall_push(&list, dc);
}
}
List_DrawCall_push(&list, (struct info_DrawCall){.kind = TEXT, .text = {.str = text, .len = next-text}});
return list;
}
struct List_DrawCall* info_parse(const info_char *text)
{
return parse(text, text+strlen(text));
}
LIST_INC(struct List_Style*, Styles)
LIST_IMPL(struct List_Style*, Styles)
static struct info_Style style_get_prev(enum info_Style_Type type, List_Styles *history)
{
List_Styles ptr = *history;
struct List_Style **elem;
while(( elem = List_Styles_next(&ptr) )){
struct List_Style *ptr = *elem;
struct info_Style *current;
while(( current = List_Style_next(&ptr) )){
if(current->kind == type){
return *current;
}
}
}
struct info_Style res = {.kind = type, .mode = 0};
return res;
}
static void info_tabulate(size_t n, info_String *str, struct info_Data *data)
{
if(!n) return;
size_t offset = data->current_len%TAB_WIDTH,
len = (n*TAB_WIDTH)-offset;
info_char *buf = malloc(sizeof(info_char)*len);
for(size_t i=0; i<len; i++) buf[i] = ' ';
info_string_puts(str, buf, len);
data->current_len += len;
free(buf);
}
static void info_render_list(List_DrawCall *list,
info_String *str,
List_Styles *history,
struct info_Data *data);
static void info_drawcall_render(struct info_DrawCall *dc,
info_String *str,
List_Styles *history,
struct info_Data *data)
{
switch(dc->kind){
case TEXT:{
const info_char *ptr = dc->text.str;
for(size_t i=0; i<dc->text.len; i++){
if(dc->text.str[i]=='\n'){
size_t len = dc->text.str+i-ptr+1;
info_string_puts(str, ptr, len);
data->current_len += len;
ptr = dc->text.str+i+1;
// indent to prefix
info_char *buf = malloc(sizeof(info_char)*data->prefix_len);
for(size_t i=0; i<data->prefix_len; i++) buf[i] = ' ';
info_string_puts(str, buf, data->prefix_len);
data->current_len = data->prefix_len;
free(buf);
} else if(dc->text.str[i] == '\t'){
info_tabulate(1, str, data);
ptr = dc->text.str+i+1;
} else if(dc->text.str[i] == '\r'){
size_t len = dc->text.str+i-ptr;
info_string_puts(str, ptr, len);
info_string_puts(str, "\\r", 2);
ptr = dc->text.str+i+1;
data->current_len += 2+len;
}
}
size_t len = dc->text.str+dc->text.len-ptr;
info_string_puts(str, ptr, len);
data->current_len += len;
} break;
case STYLE:{
List_Style ptr = dc->styled.styles;
struct info_Style *current;
while( (current = List_Style_next(&ptr)) ){
struct info_Style prev = style_get_prev(current->kind, history);
info_ansi_apply(*current, prev, str); // activate
}
List_Styles_push(history, dc->styled.styles);
info_render_list(&dc->styled.sub, str, history, data);
List_Styles_pop(history);
while(dc->styled.styles){
struct info_Style current = List_Style_pop(&dc->styled.styles),
prev = style_get_prev(current.kind, history);
info_ansi_apply(prev, current, str); // deactivate
}
} break;
case FIELD:
switch (dc->field) {
case File:
info_string_puts(str, data->origin.file, strlen(data->origin.file));
data->current_len += strlen(data->origin.file);
break;
case Func:
info_string_puts(str, data->origin.func, strlen(data->origin.func));
data->current_len += strlen(data->origin.func);
break;
case Line:
data->current_len += info_string_printf(str, "%d", data->origin.line);
break;
case Time:{
time_t time_rel;
struct tm *tm;
time(&time_rel);
tm = localtime(&time_rel);
data->current_len += info_string_printf(str, INFO_STR("%d:%.2d:%.2d"), tm->tm_hour, tm->tm_min, tm->tm_sec);
} break;
case Level:{
info_tabulate(data->level, str, data);
} break;
default: break;
}
}
}
static void info_render_list(List_DrawCall *list,
info_String *str,
List_Styles *history,
struct info_Data *data)
{
while(*list){
struct info_DrawCall dc = List_DrawCall_pop(list);
info_drawcall_render(&dc, str, history, data);
}
}
struct Segment{
const char *name;
struct timespec start;
};
LIST_INC(struct Segment, seg)
LIST_IMPL(struct Segment, seg)
FILE *out = NULL;
thread_local static struct info_Msg msg = { 0 };
thread_local static int hold = 0, holding = 0, newlined=0;
thread_local static List_seg segments = NULL;
void info_msg(struct info_Origin origin, const info_char *prefix)
{
msg = (struct info_Msg){ 0 };
msg.data.origin = origin;
msg.data.level = List_seg_length(&segments);
struct List_DrawCall *list = info_parse(prefix);
struct List_Styles *history = NULL;
info_String str = info_string_create(DEFAULT_STR_SIZE);
info_render_list(&list, &str, &history, &msg.data);
msg.data.prefix_len = msg.data.current_len;
msg.str = str;
}
static void release()
{
if(!newlined) FPUTC('\n', out); // TODO conditionally
if(msg.str.str){
free(msg.str.str);
msg.str.str = NULL;
}
holding = 0;
}
void info_printf(const info_char *format, ...)
{
if(!out) out = stderr;
if(!holding && msg.str.str )
FPUTS(msg.str.str, out);
info_String rendered = info_string_create(DEFAULT_STR_SIZE);
va_list args;
va_start(args, format);
info_string_vprintf(&rendered, format, args);
va_end(args);
struct List_DrawCall *list = info_parse(rendered.str);
struct List_Styles *history = NULL;
info_String str = info_string_create(DEFAULT_STR_SIZE);
info_render_list(&list, &str, &history, &msg.data);
FPUTS(str.str, out);
newlined = str.str[str.len-1]=='\n';
if(!hold) release();
else holding = 1;
fflush(out);
free(rendered.str);
free(str.str);
}
void info_hold(int set)
{
if(!set && holding) release();
hold = set;
}
void info_seg_begin(const char *name, struct info_Origin org)
{
info_msg(org, _PREFIX("[{F(LIGHTGREEN):SEG}]"));
info_printf("Begin Segment '{BF:%s}'", name);
struct timespec t = {0};
timespec_get(&t, TIME_UTC);
List_seg_push(&segments, (struct Segment){name, t});
}
void info_seg_end(struct info_Origin org)
{
struct Segment s = List_seg_pop(&segments);
struct timespec now = {0};
timespec_get(&now, TIME_UTC);
info_msg(org, _PREFIX("[{F(LIGHTGREEN):SEG}]"));
info_printf("End Segment '{BF:%s}' took %.3fs\n", s.name,
(now.tv_sec - s.start.tv_sec)
+ ((now.tv_nsec - s.start.tv_nsec) / 1000000000.0));
}