-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_log_parser.cpp
More file actions
1081 lines (947 loc) · 42.8 KB
/
4coder_log_parser.cpp
File metadata and controls
1081 lines (947 loc) · 42.8 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Mr. 4th Dimention - Allen Webster
*
* 14.08.2019
*
* Log parser.
*
*/
// TOP
internal u64
log_parse__string_code(Log_Parse *parse, String_Const_u8 string, Log_String_Source string_source){
u64 result = 0;
if (string.size > 0){
String_Const_u8 data = make_data(string.str, string.size);
Table_Lookup lookup = table_lookup(&parse->string_to_id_table, data);
if (lookup.found_match){
table_read(&parse->string_to_id_table, lookup, &result);
}
else{
if (string_source == LogParse_ExternalString){
data = push_data_copy(parse->arena, data);
}
result = parse->string_id_counter;
parse->string_id_counter += 1;
table_insert(&parse->string_to_id_table, data, result);
table_insert(&parse->id_to_string_table, result, data);
}
}
return(result);
}
internal String_Const_u8
log_parse__get_string(Log_Parse *parse, u64 code){
Table_Lookup lookup = table_lookup(&parse->id_to_string_table, code);
String_Const_u8 result = {};
if (lookup.found_match){
table_read(&parse->id_to_string_table, lookup, &result);
}
return(result);
}
internal Log_Event*
log_parse__event(Log_Parse *parse,
String_Const_u8 file_name, String_Const_u8 line_number, String_Const_u8 event_name){
Log_Event *new_event = push_array(parse->arena, Log_Event, 1);
sll_queue_push(parse->first_event, parse->last_event, new_event);
parse->event_count += 1;
new_event->src_file_name = log_parse__string_code(parse, file_name, LogParse_ExternalString);
new_event->event_name = log_parse__string_code(parse, event_name, LogParse_ExternalString);
new_event->line_number = string_to_integer(line_number, 10);
new_event->event_number = parse->event_count;
return(new_event);
}
internal Log_Tag*
log_parse__tag(Log_Parse *parse, Log_Event *event, String_Const_u8 tag_name, String_Const_u8 tag_value){
Log_Tag *new_tag = push_array(parse->arena, Log_Tag, 1);
sll_queue_push(event->first_tag, event->last_tag, new_tag);
event->tag_count += 1;
new_tag->name = log_parse__string_code(parse, tag_name, LogParse_ExternalString);
if (tag_value.size == 0){
new_tag->value.kind = LogTagKind_String;
new_tag->value.value = 0;
}
else{
if (tag_value.str[0] == '"'){
if (tag_value.size == 1){
new_tag->value.kind = LogTagKind_String;
new_tag->value.value = 0;
}
else{
tag_value = string_skip(tag_value, 1);
if (tag_value.str[tag_value.size - 1] == '"'){
tag_value = string_chop(tag_value, 1);
}
String_Const_u8 escape = string_interpret_escapes(parse->arena, tag_value);
new_tag->value.kind = LogTagKind_String;
new_tag->value.value = log_parse__string_code(parse, escape, LogParse_PreAllocatedString);
}
}
else{
new_tag->value.kind = LogTagKind_Integer;
b32 is_negative = false;
if (string_match(string_prefix(tag_value, 1), string_u8_litexpr("-"))){
tag_value = string_skip(tag_value, 1);
is_negative = true;
}
if (string_match(string_prefix(tag_value, 2), string_u8_litexpr("0x"))){
tag_value = string_skip(tag_value, 2);
new_tag->value.value_s = (i64)string_to_integer(tag_value, 16);
}
else{
new_tag->value.value_s = (i64)string_to_integer(tag_value, 10);
}
if (is_negative){
new_tag->value.value_s = -new_tag->value.value_s;
}
}
}
return(new_tag);
}
internal Log_Event_List*
log_parse_get_list_tag_value(Log_Parse *parse, u64 name, Log_Tag_Value value){
Log_Event_List *result = 0;
Log_Tag_Name_Value key = {name, value};
Table_Lookup lookup = table_lookup(&parse->tag_value_to_event_list_table, make_data_struct(&key));
if (lookup.found_match){
u64 val = 0;
table_read(&parse->tag_value_to_event_list_table, lookup, &val);
result = (Log_Event_List*)IntAsPtr(val);
}
return(result);
}
internal Log_Event_List*
log_parse__get_or_make_list_tag_value(Log_Parse *parse, Log_Tag *tag){
Log_Event_List *result = 0;
Log_Tag_Name_Value key = {tag->name, tag->value};
String_Const_u8 data_key = make_data_struct(&key);
Table_Lookup lookup = table_lookup(&parse->tag_value_to_event_list_table, data_key);
if (lookup.found_match){
u64 val = 0;
table_read(&parse->tag_value_to_event_list_table, lookup, &val);
result = (Log_Event_List*)IntAsPtr(val);
}
else{
result = push_array_zero(parse->arena, Log_Event_List, 1);
table_insert(&parse->tag_value_to_event_list_table, push_data_copy(parse->arena, data_key),
(u64)PtrAsInt(result));
}
return(result);
}
internal Log_Event_List*
log_parse_get_list_tag_name(Log_Parse *parse, u64 name){
Log_Event_List *result = 0;
Table_Lookup lookup = table_lookup(&parse->tag_name_to_event_list_table, name);
if (lookup.found_match){
u64 val = 0;
table_read(&parse->tag_name_to_event_list_table, lookup, &val);
result = (Log_Event_List*)IntAsPtr(val);
}
return(result);
}
internal Log_Event_List*
log_parse__get_or_make_list_tag_name(Log_Parse *parse, Log_Tag *tag){
Log_Event_List *result = 0;
Table_Lookup lookup = table_lookup(&parse->tag_name_to_event_list_table, tag->name);
if (lookup.found_match){
u64 val = 0;
table_read(&parse->tag_name_to_event_list_table, lookup, &val);
result = (Log_Event_List*)IntAsPtr(val);
}
else{
result = push_array_zero(parse->arena, Log_Event_List, 1);
table_insert(&parse->tag_name_to_event_list_table, tag->name, (u64)PtrAsInt(result));
}
return(result);
}
internal Log_Parse
make_log_parse(Arena *arena, String_Const_u8 source){
Log_Parse parse = {};
parse.arena = arena;
parse.string_id_counter = 1;
parse.string_to_id_table = make_table_Data_u64(arena->base_allocator, 500);
parse.id_to_string_table = make_table_u64_Data(arena->base_allocator, 500);
for (;source.size > 0;){
u64 end_of_line = string_find_first(source, '\n');
String_Const_u8 line = string_prefix(source, end_of_line);
line = string_skip_chop_whitespace(line);
source = string_skip(source, end_of_line + 1);
String_Const_u8 src_file_name = {};
String_Const_u8 src_line_number = {};
b32 got_source_position = false;
String_Const_u8 whole_line = line;
{
u64 colon1 = string_find_first(line, ':');
src_file_name = string_prefix(line, colon1);
line = string_skip(line, colon1 + 1);
u64 colon2 = string_find_first(line, ':');
src_line_number = string_prefix(line, colon2);
line = string_skip(line, colon2 + 1);
if (string_is_integer(src_line_number, 10)){
got_source_position = true;
}
}
if (!got_source_position){
line = whole_line;
u64 colon0 = string_find_first(line, ':');
u64 colon1 = string_find_first(line, colon0 + 1, ':');
src_file_name = string_prefix(line, colon1);
line = string_skip(line, colon1 + 1);
u64 colon2 = string_find_first(line, ':');
src_line_number = string_prefix(line, colon2);
line = string_skip(line, colon2 + 1);
if (string_is_integer(src_line_number, 10)){
got_source_position = true;
}
}
if (got_source_position){
u64 bracket_open = string_find_first(line, '[');
String_Const_u8 event_name = string_prefix(line, bracket_open);
event_name = string_skip_chop_whitespace(event_name);
line = string_skip(line, bracket_open + 1);
Log_Event *event = log_parse__event(&parse,
src_file_name, src_line_number, event_name);
for (;line.size > 0;){
u64 bracket_close = string_find_first(line, ']');
String_Const_u8 tag = string_prefix(line, bracket_close);
line = string_skip(line, bracket_close + 1);
bracket_open = string_find_first(line, '[');
line = string_skip(line, bracket_open + 1);
u64 equal_sign = string_find_first(tag, '=');
String_Const_u8 tag_name = string_prefix(tag, equal_sign);
String_Const_u8 tag_contents = string_skip(tag, equal_sign + 1);
log_parse__tag(&parse, event, tag_name, tag_contents);
}
}
}
////////////////////////////////
// NOTE(allen): fill acceleration structures
parse.tag_value_to_event_list_table = make_table_Data_u64(arena->base_allocator, Thousand(1));
parse.tag_name_to_event_list_table = make_table_u64_u64(arena->base_allocator, 100);
for (Log_Event *event = parse.first_event;
event != 0;
event = event->next){
for (Log_Tag *tag = event->first_tag;
tag != 0;
tag = tag->next){
{
Log_Event_List *list = log_parse__get_or_make_list_tag_value(&parse, tag);
Log_Event_Ptr_Node *node = push_array(arena, Log_Event_Ptr_Node, 1);
sll_queue_push(list->first, list->last, node);
list->count += 1;
node->event = event;
}
{
Log_Event_List *list = log_parse__get_or_make_list_tag_name(&parse, tag);
Log_Event_Ptr_Node *node = push_array(arena, Log_Event_Ptr_Node, 1);
sll_queue_push(list->first, list->last, node);
list->count += 1;
node->event = event;
}
}
}
for (Log_Event *event = parse.first_event;
event != 0;
event = event->next){
i32 slot_count = event->tag_count*3/2;
event->tag_name_to_tag_ptr_table = make_table_u64_u64(arena->base_allocator, slot_count);
for (Log_Tag *tag = event->first_tag;
tag != 0;
tag = tag->next){
table_insert(&event->tag_name_to_tag_ptr_table, tag->name, (u64)PtrAsInt(tag));
}
}
return(parse);
}
////////////////////////////////
internal void
log_events_sort_by_tag__inner(Log_Event **events, Log_Sort_Key *keys, i32 first, i32 one_past_last){
if (first + 1 < one_past_last){
i32 pivot_index = one_past_last - 1;
Log_Sort_Key *pivot_key = keys + pivot_index;
i32 j = first;
for (i32 i = first; i < one_past_last; i += 1){
Log_Sort_Key *key = keys + i;
b32 key_is_less_than_pivot_key = false;
if (key->value.kind < pivot_key->value.kind){
key_is_less_than_pivot_key = true;
}
else if (key->value.kind == pivot_key->value.kind){
if (key->value.value < pivot_key->value.value){
key_is_less_than_pivot_key = true;
}
else if (key->value.value == pivot_key->value.value){
if (key->number < pivot_key->number){
key_is_less_than_pivot_key = true;
}
}
}
if (key_is_less_than_pivot_key){
if (j < i){
Swap(Log_Event*, events[i], events[j]);
Swap(Log_Sort_Key, keys[i], keys[j]);
}
j += 1;
}
}
Swap(Log_Event*, events[pivot_index], events[j]);
Swap(Log_Sort_Key, keys[pivot_index], keys[j]);
log_events_sort_by_tag__inner(events, keys, first, j);
log_events_sort_by_tag__inner(events, keys, j + 1, one_past_last);
}
}
internal void
log_events_sort_by_tag(Arena *scratch, Log_Event_Ptr_Array array, u64 tag_name){
Temp_Memory temp = begin_temp(scratch);
Log_Sort_Key *keys = push_array(scratch, Log_Sort_Key, array.count);
for (i32 i = 0; i < array.count; i += 1){
Log_Event *event = array.events[i];
Table_Lookup lookup = table_lookup(&event->tag_name_to_tag_ptr_table, tag_name);
if (lookup.found_match){
u64 read_val = 0;
table_read(&event->tag_name_to_tag_ptr_table, lookup, &read_val);
Log_Tag *tag = (Log_Tag*)IntAsPtr(read_val);
keys[i].value = tag->value;
}
else{
keys[i].value.kind = LogTagKind_Null;
keys[i].value.value = 0;
}
keys[i].number = event->event_number;
}
log_events_sort_by_tag__inner(array.events, keys, 0, array.count);
end_temp(temp);
}
internal Log_Event_Ptr_Array
log_event_array_from_list(Arena *arena, Log_Event_List list){
Log_Event_Ptr_Array array = {};
array.count = list.count;
array.events = push_array(arena, Log_Event*, array.count);
i32 counter = 0;
for (Log_Event_Ptr_Node *node = list.first;
node != 0;
node = node->next){
array.events[counter] = node->event;
counter += 1;
}
return(array);
}
////////////////////////////////
global View_ID log_view = 0;
global Arena log_arena = {};
global Log_Parse log_parse = {};
global Log_Graph log_graph = {};
global Log_Filter_Set log_filter_set = {};
global Log_Filter_Set log_preview_set = {};
internal void
log_filter_set_init(Log_Filter_Set *set){
block_zero_struct(set);
for (i32 i = ArrayCount(set->filters_memory) - 1; i >= 0; i -= 1){
sll_stack_push(set->free_filters, &set->filters_memory[i]);
}
}
internal Log_Filter_Set*
log_filter_set_from_tab(Log_Graph_List_Tab tab){
Log_Filter_Set *result = 0;
switch (tab){
case LogTab_Filters:
{
result = &log_filter_set;
}break;
case LogTab_Previews:
{
result = &log_preview_set;
}break;
}
return(result);
}
internal Log_Filter*
log_filter_set__new_filter(Log_Filter_Set *set, Log_Filter *prototype){
Log_Filter *result = set->free_filters;
if (result != 0){
for (Log_Filter *filter = set->first;
filter != 0;
filter = filter->next){
if (filter->kind == prototype->kind &&
filter->tag_name_code == prototype->tag_name_code &&
block_match_struct(&filter->tag_value, &prototype->tag_value)){
result = 0;
break;
}
}
if (result != 0){
sll_stack_pop(set->free_filters);
block_copy_struct(result, prototype);
zdll_push_back(set->first, set->last, result);
set->count += 1;
set->alter_counter += 1;
}
}
return(result);
}
internal void
log_filter_set__free_filter(Log_Filter_Set *set, Log_Filter *filter){
zdll_remove(set->first, set->last, filter);
set->count -= 1;
set->alter_counter += 1;
sll_stack_push(set->free_filters, filter);
}
internal void
log_graph_fill(Application_Links *app, Rect_f32 layout_region, Face_ID face_id){
if (log_parse.arena != 0){
if (log_graph.holding_temp){
end_temp(log_graph.temp);
}
block_zero_struct(&log_graph);
log_graph.holding_temp = true;
log_graph.temp = begin_temp(&log_arena);
log_graph.layout_region = layout_region;
log_graph.face_id = face_id;
log_graph.filter_alter_counter = log_filter_set.alter_counter;
log_graph.preview_alter_counter = log_preview_set.alter_counter;
log_graph.tab = LogTab_Filters;
f32 details_h = rect_height(layout_region)*.22f;
details_h = clamp_top(details_h, 250.f);
Rect_f32 details_region = Rf32(layout_region.x0, layout_region.y0,
layout_region.x1, layout_region.y0 + details_h);
Rect_f32 event_list_region = Rf32(layout_region.x0, layout_region.y0 + details_h,
layout_region.x1, layout_region.y1);
log_graph.details_region = details_region;
log_graph.details_region.p0 -= layout_region.p0;
log_graph.details_region.p1 -= layout_region.p0;
u64 thread_code = log_parse__string_code(&log_parse, string_u8_litexpr("thread"),
LogParse_ExternalString);
if (log_filter_set.count == 0){
// NOTE(allen): everything goes into the filtered list
for (Log_Event *event = log_parse.first_event;
event != 0;
event = event->next){
Log_Event_Ptr_Node *node = push_array(&log_arena, Log_Event_Ptr_Node, 1);
node->event = event;
sll_queue_push(log_graph.filtered_list.first, log_graph.filtered_list.last, node);
log_graph.filtered_list.count += 1;
}
}
else{
for (Log_Filter *filter = log_filter_set.first;
filter != 0;
filter = filter->next){
Log_Event_List *filter_list = 0;
if (filter->kind == LogFilter_TagValue){
filter_list = log_parse_get_list_tag_value(&log_parse, filter->tag_name_code,
filter->tag_value);
}
else if (filter->kind == LogFilter_Tag){
filter_list = log_parse_get_list_tag_name(&log_parse, filter->tag_name_code);
}
// NOTE(allen): combine with existing result
if (filter == log_filter_set.first){
for (Log_Event_Ptr_Node *node = filter_list->first;
node != 0;
node = node->next){
Log_Event_Ptr_Node *new_node = push_array(&log_arena, Log_Event_Ptr_Node, 1);
new_node->event = node->event;
sll_queue_push(log_graph.filtered_list.first, log_graph.filtered_list.last, new_node);
log_graph.filtered_list.count += 1;
}
}
else{
Log_Event_Ptr_Node **fixup_ptr = &log_graph.filtered_list.first;
log_graph.filtered_list.last = 0;
for (Log_Event_Ptr_Node *node_a = log_graph.filtered_list.first, *next = 0;
node_a != 0;
node_a = next){
next = node_a->next;
b32 remove_node_a = true;
for (Log_Event_Ptr_Node *node_b = filter_list->first;
node_b != 0;
node_b = node_b->next){
if (node_a->event == node_b->event){
remove_node_a = false;
break;
}
}
if (remove_node_a){
*fixup_ptr = next;
}
else{
fixup_ptr = &node_a->next;
log_graph.filtered_list.last = node_a;
}
}
}
}
}
log_graph.event_array = log_event_array_from_list(&log_arena, log_graph.filtered_list);
log_events_sort_by_tag(&log_arena, log_graph.event_array, thread_code);
b32 had_a_tag = true;
u64 thread_id_value = 0;
Log_Graph_Thread_Bucket *prev_bucket = 0;
for (i32 i = 0; i < log_graph.event_array.count; i += 1){
Table_u64_u64 *tag_table = &log_graph.event_array.events[i]->tag_name_to_tag_ptr_table;
Table_Lookup lookup = table_lookup(tag_table, thread_code);
b32 emit_next_bucket = false;
if (!lookup.found_match){
if (had_a_tag){
had_a_tag = false;
thread_id_value = 0;
emit_next_bucket = true;
}
}
else{
u64 read_val = 0;
table_read(tag_table, lookup, &read_val);
Log_Tag *tag = (Log_Tag*)IntAsPtr(read_val);
if (!had_a_tag){
had_a_tag = true;
thread_id_value = tag->value.value;
emit_next_bucket = true;
}
else if (thread_id_value != tag->value.value){
thread_id_value = tag->value.value;
emit_next_bucket = true;
}
}
if (emit_next_bucket){
Log_Graph_Thread_Bucket *bucket = push_array(&log_arena, Log_Graph_Thread_Bucket, 1);
sll_queue_push(log_graph.first_bucket, log_graph.last_bucket, bucket);
log_graph.bucket_count += 1;
bucket->range.first = i;
bucket->had_a_tag = had_a_tag;
bucket->thread_id_value = thread_id_value;
if (prev_bucket != 0){
prev_bucket->range.one_past_last = i;
}
prev_bucket = bucket;
}
}
if (prev_bucket != 0){
prev_bucket->range.one_past_last = log_graph.event_array.count;
}
Face_Metrics metrics = get_face_metrics(app, face_id);
f32 line_height = metrics.line_height;
f32 box_h = f32_floor32(line_height*1.5f);
f32 box_w = f32_floor32(rect_width(event_list_region)/log_graph.bucket_count);
f32 y_cursor = event_list_region.y0 - layout_region.y0;
if (log_graph.bucket_count > 0){
f32 y_bottom = 0.f;
for (;;){
i32 smallest_event_number = max_i32;
i32 bucket_with_next_event_index = -1;
Log_Graph_Thread_Bucket *bucket_with_next_event = 0;
Log_Event *next_event = 0;
i32 iteration_counter = 0;
for (Log_Graph_Thread_Bucket *bucket = log_graph.first_bucket;
bucket != 0;
bucket = bucket->next, iteration_counter += 1){
if (bucket->range.first < bucket->range.one_past_last){
Log_Event *event = log_graph.event_array.events[bucket->range.first];
if (event->event_number < smallest_event_number){
smallest_event_number = event->event_number;
bucket_with_next_event_index = iteration_counter;
bucket_with_next_event = bucket;
next_event = event;
}
}
}
if (bucket_with_next_event == 0){
break;
}
bucket_with_next_event->range.first += 1;
Log_Graph_Box *box_node = push_array(&log_arena, Log_Graph_Box, 1);
sll_queue_push(log_graph.first_box, log_graph.last_box, box_node);
log_graph.box_count += 1;
Rect_f32 rect = Rf32(box_w*bucket_with_next_event_index , y_cursor,
box_w*(bucket_with_next_event_index + 1), y_cursor + box_h);
box_node->rect = rect;
box_node->event = next_event;
y_bottom = Max(y_bottom, rect.y1);
y_cursor += box_h;
}
log_graph.max_y_scroll = clamp_bot(line_height, y_bottom - rect_height(event_list_region)*0.5f);
}
}
}
internal void
log_parse_fill(Application_Links *app, Buffer_ID buffer){
if (log_arena.base_allocator == 0){
log_arena = make_arena_system();
}
linalloc_clear(&log_arena);
block_zero_struct(&log_graph);
log_filter_set_init(&log_filter_set);
log_filter_set_init(&log_preview_set);
String_Const_u8 log_text = push_whole_buffer(app, &log_arena, buffer);
log_parse = make_log_parse(&log_arena, log_text);
}
internal void
log_graph_render__tag(Arena *arena, Fancy_Line *line,
Log_Parse *log, Log_Tag *tag){
String_Const_u8 tag_name = log_parse__get_string(log, tag->name);
push_fancy_stringf(arena, line, f_white, "[");
push_fancy_string(arena, line, f_green, tag_name);
push_fancy_stringf(arena, line, f_white, "=");
if (tag->value.kind == LogTagKind_Integer){
push_fancy_stringf(arena, line, f_pink, "0x%llx", tag->value.value_s);
}
else if (tag->value.kind == LogTagKind_String){
String_Const_u8 value = log_parse__get_string(log, tag->value.value);
push_fancy_string(arena, line, f_pink, value);
}
push_fancy_stringf(arena, line, f_white, "]");
}
internal void
log_graph_render(Application_Links *app, Frame_Info frame_info, View_ID view){
if (log_parse.arena != 0){
////////////////////////////////
View_ID active_view = get_active_view(app, Access_Always);
b32 is_active_view = (active_view == view);
Rect_f32 view_rect = view_get_screen_rect(app, view);
Rect_f32 inner = rect_inner(view_rect, 3);
draw_rectangle_fcolor(app, view_rect, 0.f,
get_item_margin_color(is_active_view?UIHighlight_Active:UIHighlight_None));
draw_rectangle_fcolor(app, inner, 0.f, fcolor_id(defcolor_back));
Rect_f32 prev_clip = draw_set_clip(app, inner);
////////////////////////////////
Face_ID face_id = get_face_id(app, 0);
f32 y_scroll = log_graph.y_scroll;
Log_Event *selected_event = log_graph.selected_event;
if (!log_graph.holding_temp ||
inner != log_graph.layout_region ||
face_id != log_graph.face_id ||
log_filter_set.alter_counter != log_graph.filter_alter_counter){
log_graph_fill(app, inner, face_id);
}
log_graph.y_scroll = clamp(0.f, y_scroll, log_graph.max_y_scroll);
log_graph.selected_event = selected_event;
Mouse_State mouse = get_mouse_state(app);
Vec2_f32 m_p = V2f32(mouse.p) - inner.p0;
Face_Metrics metrics = get_face_metrics(app, log_graph.face_id);
f32 line_height = metrics.line_height;
Log_Event *hover_event = 0;
b32 in_details_region = (rect_contains_point(log_graph.details_region, m_p));
for (Log_Graph_Box *box_node = log_graph.first_box;
box_node != 0;
box_node = box_node->next){
Scratch_Block scratch(app);
Rect_f32 box = box_node->rect;
box.y0 -= log_graph.y_scroll;
box.y1 -= log_graph.y_scroll;
Rect_f32 box_inner = rect_inner(box, 3.f);
FColor margin_color = f_dark_gray;
if (!in_details_region && hover_event == 0 && rect_contains_point(box, m_p)){
margin_color = f_gray;
hover_event = box_node->event;
}
if (box_node->event == log_graph.selected_event){
margin_color = f_light_gray;
}
draw_rectangle_fcolor(app, box , 0.f, margin_color);
draw_rectangle_fcolor(app, box_inner, 0.f, f_black );
Log_Event *event = box_node->event;
String_Const_u8 event_name = log_parse__get_string(&log_parse, event->event_name);
Fancy_Line line = {};
push_fancy_string(scratch, &line, f_white, event_name);
for (Log_Filter *filter = log_preview_set.first;
filter != 0;
filter = filter->next){
Table_u64_u64 *table = &event->tag_name_to_tag_ptr_table;
Table_Lookup lookup = table_lookup(table, filter->tag_name_code);
if (lookup.found_match){
u64 val = 0;
table_read(table, lookup, &val);
Log_Tag *tag = (Log_Tag*)IntAsPtr(val);
push_fancy_string(scratch, &line, string_u8_litexpr(" "));
log_graph_render__tag(scratch, &line, &log_parse, tag);
}
}
Vec2_f32 p = V2f32(box_inner.x0 + 3.f,
(f32_round32((box_inner.y0 + box_inner.y1 - line_height)*0.5f)));
draw_fancy_line(app, log_graph.face_id, fcolor_zero(), &line, p);
}
{
Scratch_Block scratch(app);
Rect_f32 box = log_graph.details_region;
Rect_f32 box_inner = rect_inner(box, 3.f);
Log_Graph_List_Tab current_tab = log_graph.tab;
Log_Filter_Set *viewing_filter_set = log_filter_set_from_tab(current_tab);
draw_rectangle_fcolor(app, box , 0.f, f_dark_gray);
draw_rectangle_fcolor(app, box_inner, 0.f, f_black );
{
f32 y_cursor = box_inner.y0 + 3.f;
if (y_cursor + line_height > box_inner.y1) goto finish_list_display;
{
f32 x_cursor = box_inner.x0 + 3.f;
for (i32 i = LogTab_ERROR + 1; i < LogTab_COUNT; i += 1){
FColor color = (i == current_tab)?f_white:f_gray;
Fancy_Line line = {};
switch (i){
case LogTab_Filters:
{
push_fancy_stringf(scratch, &line, color, "filters");
}break;
case LogTab_Previews:
{
push_fancy_stringf(scratch, &line, color, "previews");
}break;
}
Vec2_f32 p = V2f32(x_cursor, y_cursor);
f32 width = get_fancy_line_width(app, log_graph.face_id,
&line);
draw_fancy_line(app, log_graph.face_id, fcolor_zero(),
&line, p);
x_cursor += width + metrics.normal_advance;
if (log_graph.has_unused_click){
Rect_f32 click_rect = Rf32_xy_wh(p.x, p.y,
width, line_height);
if (rect_contains_point(click_rect, log_graph.unused_click)){
log_graph.has_unused_click = false;
log_graph.tab = i;
}
}
}
}
if (viewing_filter_set != 0){
for (Log_Filter *filter = viewing_filter_set->first, *next = 0;
filter != 0;
filter = next){
next = filter->next;
y_cursor += line_height;
if (y_cursor + line_height > box_inner.y1) goto finish_list_display;
Fancy_Line line = {};
if (filter->kind == LogFilter_TagValue){
push_fancy_stringf(scratch, &line, f_white, "val [");
String_Const_u8 tag_name = log_parse__get_string(&log_parse, filter->tag_name_code);
push_fancy_stringf(scratch, &line, f_green, "%.*s", string_expand(tag_name));
push_fancy_stringf(scratch, &line, f_white, "=");
if (filter->tag_value.kind == LogTagKind_Integer){
push_fancy_stringf(scratch, &line, f_pink, "0x%llx", filter->tag_value.value_s);
}
else if (filter->tag_value.kind == LogTagKind_String){
String_Const_u8 value = log_parse__get_string(&log_parse, filter->tag_value.value);
push_fancy_stringf(scratch, &line, f_pink, "%.*s", string_expand(value));
}
push_fancy_stringf(scratch, &line, f_white, "]");
}
else{
push_fancy_stringf(scratch, &line, f_white, "name [");
String_Const_u8 tag_name = log_parse__get_string(&log_parse, filter->tag_name_code);
push_fancy_stringf(scratch, &line, f_green, "%.*s", string_expand(tag_name));
push_fancy_stringf(scratch, &line, f_white, "]");
}
Vec2_f32 p = V2f32(box_inner.x0 + 3.f, y_cursor);
f32 width = get_fancy_line_width(app, log_graph.face_id,
&line);
draw_fancy_line(app, log_graph.face_id, fcolor_zero(),
&line, p);
if (log_graph.has_unused_click){
Rect_f32 click_rect = Rf32_xy_wh(p.x, p.y,
width, line_height);
if (rect_contains_point(click_rect, log_graph.unused_click)){
log_graph.has_unused_click = false;
log_filter_set__free_filter(viewing_filter_set, filter);
}
}
}
}
finish_list_display:;
}
Log_Event *view_event = (hover_event!=0)?hover_event:log_graph.selected_event;
if (view_event != 0){
f32 y_cursor = box_inner.y0 + 3.f;
if (y_cursor + line_height > box_inner.y1) goto finish_event_display;
{
Fancy_Line line = {};
String_Const_u8 file_name = log_parse__get_string(&log_parse, view_event->src_file_name);
push_fancy_stringf(scratch, &line, f_green, "[%d] ", view_event->event_number);
push_fancy_stringf(scratch, &line, f_white, "%.*s:", string_expand(file_name));
push_fancy_stringf(scratch, &line, f_pink, "%llu", view_event->line_number);
Vec2_f32 right_p = V2f32(box_inner.x1 - 3.f, y_cursor);
f32 width = get_fancy_line_width(app, log_graph.face_id, &line);
Vec2_f32 p = V2f32(right_p.x - width, right_p.y);
draw_fancy_line(app, log_graph.face_id, fcolor_zero(), &line, p);
}
for (Log_Tag *tag = view_event->first_tag;
tag != 0;
tag = tag->next){
y_cursor += line_height;
if (y_cursor + line_height > box_inner.y1) goto finish_event_display;
{
Fancy_Line line = {};
log_graph_render__tag(scratch, &line, &log_parse, tag);
Vec2_f32 right_p = V2f32(box_inner.x1 - 3.f, y_cursor);
f32 width = get_fancy_line_width(app, log_graph.face_id, &line);
Vec2_f32 p = V2f32(right_p.x - width, right_p.y);
draw_fancy_line(app, log_graph.face_id, fcolor_zero(),
&line, p);
if (log_graph.has_unused_click){
Rect_f32 click_rect = Rf32(p.x, p.y, right_p.x, p.y + line_height);
if (rect_contains_point(click_rect, log_graph.unused_click)){
log_graph.has_unused_click = false;
Log_Filter filter = {};
switch (log_graph.tab){
case LogTab_Filters:
{
filter.kind = LogFilter_TagValue;
filter.tag_name_code = tag->name;
filter.tag_value = tag->value;
}break;
case LogTab_Previews:
{
filter.kind = LogFilter_Tag;
filter.tag_name_code = tag->name;
}break;
}
if (filter.kind != LogTab_ERROR){
log_filter_set__new_filter(viewing_filter_set, &filter);
animate_in_n_milliseconds(app, 0);
}
}
}
}
}
finish_event_display:;
}
}
log_graph.has_unused_click = false;
draw_set_clip(app, prev_clip);
}
}
internal Log_Graph_Box*
log_graph__get_box_at_point(Log_Graph *graph, Vec2_f32 p){
Log_Graph_Box *result = 0;
if (!rect_contains_point(graph->details_region, p)){
for (Log_Graph_Box *box_node = graph->first_box;
box_node != 0;
box_node = box_node->next){
Rect_f32 box = box_node->rect;
box.y0 -= graph->y_scroll;
box.y1 -= graph->y_scroll;
if (rect_contains_point(box, p)){
result = box_node;
break;
}
}
}
return(result);
}
internal Log_Graph_Box*
log_graph__get_box_at_mouse_point(Application_Links *app, Log_Graph *graph){
Mouse_State mouse = get_mouse_state(app);
Vec2_f32 m_p = V2f32(mouse.p) - graph->layout_region.p0;
return(log_graph__get_box_at_point(graph, m_p));
}
function void
log_graph__click_select_event(Application_Links *app, Vec2_f32 m_p)
{
if (log_view != 0 && log_graph.holding_temp){
Log_Graph_Box *box_node = log_graph__get_box_at_point(&log_graph, m_p);
if (box_node != 0){
log_graph.selected_event = box_node->event;
}
else{
log_graph.has_unused_click = true;
log_graph.unused_click = m_p;
}
}
}
function void
log_graph__click_jump_to_event_source(Application_Links *app, Vec2_f32 m_p){
if (log_view != 0 && log_graph.holding_temp){
Log_Graph_Box *box_node = log_graph__get_box_at_point(&log_graph, m_p);
if (box_node != 0){
Log_Event *event = box_node->event;
log_graph.selected_event = event;
View_ID target_view = get_next_view_looped_primary_panels(app, log_view,
Access_ReadVisible);
if (target_view != 0){
String_Const_u8 file_name = log_parse__get_string(&log_parse, event->src_file_name);
Buffer_ID target_buffer = get_buffer_by_file_name(app, file_name, Access_Always);
if (target_buffer == 0){
target_buffer = get_buffer_by_name(app, file_name, Access_Always);
}
if (target_buffer != 0){
set_view_to_location(app, target_view, target_buffer,
seek_line_col(event->line_number, 1));
}
}
}
else{
log_graph.has_unused_click = true;
log_graph.unused_click = m_p;
}
}
}
CUSTOM_UI_COMMAND_SIG(show_the_log_graph)
CUSTOM_DOC("Parses *log* and displays the 'log graph' UI")
{
if (log_view != 0){
return;
}
Buffer_ID log_buffer = get_buffer_by_name(app, string_u8_litexpr("*log*"), Access_Always);
log_parse_fill(app, log_buffer);