forked from inaneverb/ekacore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_console.go
More file actions
1232 lines (991 loc) · 35.5 KB
/
Copy pathencoder_console.go
File metadata and controls
1232 lines (991 loc) · 35.5 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
// Copyright © 2020. All rights reserved.
// Author: Ilya Stroy.
// Contacts: qioalice@gmail.com, https://github.com/qioalice
// License: https://opensource.org/licenses/MIT
package ekalog
import (
"math"
"strconv"
"strings"
"time"
"github.com/qioalice/ekago/v2/ekasys"
"github.com/qioalice/ekago/v2/internal/ekafield"
"github.com/qioalice/ekago/v2/internal/ekaletter"
)
//noinspection GoSnakeCaseUsage
type (
// CI_ConsoleEncoder is a type that built to be used as a part of CommonIntegrator
// as an log Entries encoder to the TTY (console) with human readable format,
// custom format supporting and ability to enable coloring.
//
// If you want to use CI_ConsoleEncoder, you need to instantiate object,
// set log's text format string using SetFormat() and then call
// FreezeAndGetEncoder() method. By that you'll get the function that has
// an alias CI_Encoder and you can add it as encoder by
// CommonIntegrator.WithEncoder().
//
// See https://github.com/qioalice/ekago/ekalog/integrator.go ,
// https://github.com/qioalice/ekago/ekalog/integrator_common.go for more info.
CI_ConsoleEncoder struct {
// RAW FORMAT
// This is what user is set by SetFormat() method.
//
// It will be parsed, converted and casted to internal structures
// that are more convenient to be used as source of log message format
// generator.
format string
// BUILT FORMAT
// This part represents parsed raw format string.
//
// First of all RAW format string parsed to the two group of entities:
// - Just string, not verb (writes as is),
// - Format verbs (will be substituted to the log's parts, such as
// log's message, timestamp, log's level, log's fields, stacktrace, etc).
//
// These parts (w/o reallocation) will be stored with the same sequence
// as they represented in 'format' but there will be specified verb's types also.
// Moreover their common length will be calculated and stored to decrease
// destination []byte buffer reallocations (just allocate big buffer,
// at least as more as 'formatParts' required).
formatParts []_CICE_FormatPart
colorMap map[Level]string // map of default colors for each level
colorMapMax int // max used len of ASCII color encoded seq.
// Sum of: len of just text parts + predicted len of log's parts.
minimumBufferLen int
ff _CICE_FieldsFormat // parts of fields formatting
bf _CICE_BodyFormat // parts of body formatting
cf _CICE_CallerFormat // parts of caller, stacktrace's frames formatting
sf _CICE_StacktraceFormat // parts of stacktrace formatting
ef _CICE_ErrorFormat // parts of attached error formatting
}
// _CICE_FormatPart represents built format's part (parsed RAW format string).
// - for 'typ' == '_CICE_FPT_VERB_JUST_TEXT', 'value' is original RAW format string's part;
// - for 'typ' == '_CICE_FPT_VERB_COLOR_CUSTOM', 'value' is calculated bash escape sequence
// (like "\033[01;03;38;05;144m");
// - for other 'typ' variants, 'value' is empty, 'cause it's log's verbs
// and they're runtime calculated entities.
_CICE_FormatPart struct {
typ _CICE_FormatPartType
value string
}
// _CICE_FormatPartType is a special type of _CICE_FormatPart's field 'typ' that contains
// an info what kind of format verb current _CICE_FormatPart object is
// and how exactly it will be converted to the text at the runtime.
_CICE_FormatPartType uint16
_CICE_FieldsFormat struct {
isSet bool
beforeFields string
afterFields string
beforeKey string
afterKey string
afterValue string
afterNewLine string
afterNewLineForError string
itemsPerLine int16
}
_CICE_BodyFormat struct {
isSet bool
beforeBody string
afterBody string
}
_CICE_CallerFormat struct {
isSet bool
isDefault bool
parts [10]struct {
typ int16
val string
}
}
_CICE_StacktraceFormat struct {
isSet bool
beforeStack string
afterStack string
}
_CICE_ErrorFormat struct {
isSet bool
beforeError string
afterError string
}
)
//noinspection GoSnakeCaseUsage
const (
// Common Integrator Console Encoder Format Part Type (CICE FPT)
// predefined constants.
_CICE_FPT_MASK_TYPE _CICE_FormatPartType = 0x00_FF
_CICE_FPT_MASK_DATA _CICE_FormatPartType = 0xFF_00
_CICE_FPT_VERB_JUST_TEXT _CICE_FormatPartType = 0x01
_CICE_FPT_VERB_COLOR_CUSTOM _CICE_FormatPartType = 0x02
_CICE_FPT_VERB_COLOR_FOR_LEVEL _CICE_FormatPartType = 0x03
_CICE_FPT_VERB_BODY _CICE_FormatPartType = 0x0A
_CICE_FPT_VERB_TIME _CICE_FormatPartType = 0x0B
_CICE_FPT_VERB_LEVEL _CICE_FormatPartType = 0x0C
_CICE_FPT_VERB_STACKTRACE _CICE_FormatPartType = 0x1A
_CICE_FPT_VERB_FIELDS _CICE_FormatPartType = 0x2A
_CICE_FPT_VERB_CALLER _CICE_FormatPartType = 0x3A
// Common Integrator Console Encoder Level Format (CICE LF)
// type constants.
_CICE_LF_NUMBER _CICE_FormatPartType = 1
_CICE_LF_SHORT_NORMAL _CICE_FormatPartType = 2
_CICE_LF_SHORT_UPPER_CASE _CICE_FormatPartType = 3
_CICE_LF_FULL_NORMAL _CICE_FormatPartType = 4
_CICE_LF_FULL_UPPER_CASE _CICE_FormatPartType = 5
// Common Integrator Console Encoder Time Format (CICE TF)
// type constants.
_CICE_TF_TIMESTAMP _CICE_FormatPartType = 1
_CICE_TF_ANSIC _CICE_FormatPartType = 2
_CICE_TF_UNIXDATE _CICE_FormatPartType = 3
_CICE_TF_RUBYDATE _CICE_FormatPartType = 4
_CICE_TF_RFC822 _CICE_FormatPartType = 5
_CICE_TF_RFC822_Z _CICE_FormatPartType = 6
_CICE_TF_RFC850 _CICE_FormatPartType = 7
_CICE_TF_RFC1123 _CICE_FormatPartType = 8
_CICE_TF_RFC1123_Z _CICE_FormatPartType = 9
_CICE_TF_RFC3339 _CICE_FormatPartType = 10
// Common Integrator Console Encoder Caller Format (CICE CF)
// type constants.
_CICE_CF_TYPE_SEPARATOR int16 = -1
_CICE_CF_TYPE_FUNC_SHORT int16 = 1
_CICE_CF_TYPE_FUNC_FULL int16 = 2
_CICE_CF_TYPE_FILE_SHORT int16 = 3
_CICE_CF_TYPE_FILE_FULL int16 = 4
_CICE_CF_TYPE_LINE_NUM int16 = 5
_CICE_CF_TYPE_PKG_SHORT int16 = 6 // unused
_CICE_CF_TYPE_PKG_FULL int16 = 7
// Common Integrator Console Encoder (CICE) verb predefined constants.
_CICE_VERB_START_INDICATOR rune = '{'
_CICE_VERB_END_INDICATOR rune = '}'
_CICE_VERB_SEPARATOR byte = '/'
// Common Integrator Console Integrator Standard Colors (CICE SC)
// predefined constants.
_CICE_SC_DEBUG string = `c/fg:ascii:36`
_CICE_SC_INFO string = `c/fg:ascii:32`
_CICE_SC_WARNING string = `c/fg:ascii:33/b`
_CICE_SC_ERROR string = `c/fg:ascii:31/b`
_CICE_SC_FATAL string = `c/fg:ascii:35/b`
// Common Integrator Console Encoder (CICE) defaults.
_CICE_DEFAULT_FORMAT string =
// include colored level, colored time
"{{c}}{{l}} {{t}}{{c/0}}\n" +
// include message with \n if non-empty
"{{m/?$\n}}" +
// include fields with " = " as key-value separator, colored key
"{{f/?$\n/v = /e, /l\t/le\t\t}}" +
// include stacktrace with \n if non-empty
"{{s/?$\n/e, }}" +
// omit caller, specify each stacktrace's frame format
"{{w/0/fd}}" +
//
"\n"
_CICE_DEFAULT_TIME_FORMAT string = "Mon, Jan 02 15:04:05"
)
// Common Integrator Console Encoder Verb Types (CICE VT)
// that are used in format string to determine what kind of verb must be used.
var (
cevtCaller = []string{"caller", "who", "w"}
cevtColor = []string{"color", "c"}
cevtLevel = []string{"level", "lvl", "l"}
cevtTime = []string{"time", "t"}
cevtMessage = []string{"message", "body", "m", "b"}
cevtFields = []string{"fields", "f"}
cevtStacktrace = []string{"stacktrace", "s"}
)
var (
// Make sure we won't break API by declaring package's console encoder
defaultConsoleEncoder CI_Encoder
)
// Type extracts verb's type from current _CICE_FormatPartType that can be compared
// with _CEFPT_VERB... constants.
func (fpt _CICE_FormatPartType) Type() _CICE_FormatPartType {
return fpt & _CICE_FPT_MASK_TYPE
}
// Data extracts verb's type data from current _CICE_FormatPartType that is needed
// for some internal verb's encoders.
func (fpt _CICE_FormatPartType) Data() _CICE_FormatPartType {
return (fpt & _CICE_FPT_MASK_DATA) >> 8
}
// SetFormat
func (ce *CI_ConsoleEncoder) SetFormat(newFormat string) *CI_ConsoleEncoder {
if s := strings.TrimSpace(newFormat); s == "" {
// Just check whether 'newFormat' is non-empty string or a string
// that contains not only whitespace characters. But do not ignore them.
return ce
}
// TODO: calculate buf size
ce.format = newFormat
return ce
}
// SetColorFor sets color what will be used as a replace for level-depended
// color verb from the 'format' string that is set by SetFormat() func.
func (ce *CI_ConsoleEncoder) SetColorFor(level Level, color string) *CI_ConsoleEncoder {
if ce.colorMap == nil {
ce.colorMap = make(map[Level]string)
}
if encodedColor := ce.rvColorHelper(color); encodedColor != "" {
ce.colorMap[level] = encodedColor
if l := len(encodedColor); ce.colorMapMax < l {
ce.colorMapMax = l
}
}
return ce
}
// FreezeAndGetEncoder builds current CI_ConsoleEncoder if it has not built yet
// and if format string has been provided by SetFormat() returning a function
// (has an alias CI_Encoder) that can be used at the
// CommonIntegrator.WithEncoder() call while initializing.
func (ce *CI_ConsoleEncoder) FreezeAndGetEncoder() CI_Encoder {
return ce.doBuild().encode
}
// doBuild builds the current CI_ConsoleEncoder only if it has not built yet.
// There is no-op if format string is empty or encoder already built.
func (ce *CI_ConsoleEncoder) doBuild() *CI_ConsoleEncoder {
switch {
case ce == nil:
return nil
case len(ce.formatParts) > 0:
// do not build if it's so already
return ce
}
ce.format = strings.TrimSpace(ce.format)
if ce.format == "" {
ce.format = _CICE_DEFAULT_FORMAT
}
// start parsing ce.format
// all parsing loops are for-range based (because there is UTF-8 support)
// (yes, you can use not only ASCII parts in your format string,
// and yes if you do it, you are mad. stop it!).
for rest := ce.format; rest != ""; rest = ce.parseFirstVerb(rest) {
}
ce.uniteJustTextVerbs()
ce.setStandardParts()
return ce
}
// uniteJustTextVerbs unites "just text" verbs in 'ce.formatParts'
// that follows each other. It may happen when something with bad verbs
// were included in 'ce.format'.
func (ce *CI_ConsoleEncoder) uniteJustTextVerbs() *CI_ConsoleEncoder {
var (
// idx of "just text" verb next "just text" verbs will be united with
justTextVerbIdx = -1
// new out slice of verbs
newFormatParts = make([]_CICE_FormatPart, 0, len(ce.formatParts))
)
for idx, verb := range ce.formatParts {
switch verbTyp := verb.typ.Type(); {
case justTextVerbIdx == -1 && verbTyp == _CICE_FPT_VERB_JUST_TEXT:
justTextVerbIdx = idx
case justTextVerbIdx != -1 && verbTyp == _CICE_FPT_VERB_JUST_TEXT:
ce.formatParts[justTextVerbIdx].value += verb.value
case justTextVerbIdx != -1 && verbTyp != _CICE_FPT_VERB_JUST_TEXT:
newFormatParts = append(newFormatParts, ce.formatParts[justTextVerbIdx])
justTextVerbIdx = -1
fallthrough
case justTextVerbIdx == -1 && verbTyp != _CICE_FPT_VERB_JUST_TEXT:
newFormatParts = append(newFormatParts, verb)
}
}
// it was definitely the last "just text" verb we should add
if justTextVerbIdx != -1 {
newFormatParts = append(newFormatParts, ce.formatParts[justTextVerbIdx])
}
ce.formatParts = newFormatParts[:]
return ce
}
// setStandardParts saves standard colors for standard log levels
// if they has not been set yet.
func (ce *CI_ConsoleEncoder) setStandardParts() *CI_ConsoleEncoder {
if ce.colorMap == nil {
ce.colorMap = make(map[Level]string)
}
if ce.colorMap[LEVEL_DEBUG] == "" {
ce.colorMap[LEVEL_DEBUG] = ce.rvColorHelper(_CICE_SC_DEBUG)
}
if ce.colorMap[LEVEL_INFO] == "" {
ce.colorMap[LEVEL_INFO] = ce.rvColorHelper(_CICE_SC_INFO)
}
if ce.colorMap[LEVEL_WARNING] == "" {
ce.colorMap[LEVEL_WARNING] = ce.rvColorHelper(_CICE_SC_WARNING)
}
if ce.colorMap[LEVEL_ERROR] == "" {
ce.colorMap[LEVEL_ERROR] = ce.rvColorHelper(_CICE_SC_ERROR)
}
if ce.colorMap[LEVEL_FATAL] == "" {
ce.colorMap[LEVEL_FATAL] = ce.rvColorHelper(_CICE_SC_FATAL)
}
if !ce.cf.isSet {
ce.cf.isDefault = true
}
return ce
}
// parseFirstVerb parses 'format', extracts first verb (even if it's "just text"
// verb), saves it to ce.formatParts and then returns the rest of 'format' string.
func (ce *CI_ConsoleEncoder) parseFirstVerb(format string) (rest string) {
var (
i = 0
pc rune // prev char
wv bool // true if current parsing verb is complex verb (not "just text")
wve bool // true if complex verb has been closed correctly
)
// loop is for-range based (because there is UTF-8 support)
// (yes, you can use not only ASCII parts in your format string,
// and yes if you do it, you are mad. stop it!).
for _, c := range format {
switch {
case c == _CICE_VERB_START_INDICATOR && pc == _CICE_VERB_START_INDICATOR && wv:
// unexpected "{{" inside complex verb, treat all prev as "just text",
// try to treat as starting complex verb
wv = false
i--
case c == _CICE_VERB_START_INDICATOR && pc == _CICE_VERB_START_INDICATOR && i > 1:
// > 1 (not > 0) because if string started with "{{", after first "{"
// i already == 1.
//
// was "just text", found complex verb start
i--
case c == _CICE_VERB_END_INDICATOR && pc == _CICE_VERB_END_INDICATOR && wv:
// ending of complex verb
wve = true
i++
case c == _CICE_VERB_START_INDICATOR && pc == _CICE_VERB_START_INDICATOR:
// this is the beginning of 'format' and of complex verb
wv = true
i++
continue
default:
pc = c
i++
continue
}
break
}
// what kind of verb we did parse and did verb has been closed correctly?
if wv && wve {
ce.minimumBufferLen += ce.rv(format[:i])
} else {
ce.minimumBufferLen += ce.rvJustText(format[:i])
}
return format[i:]
}
// rv (resolve verb) tries to determine what kind of complex verb 'verb' is,
// creates related '_CICE_FormatPart', fills it and adds to 'ce.formatParts'.
// Returned predicted minimum length of bytes that required in buffer to store
// formatted verb.
func (ce *CI_ConsoleEncoder) rv(verb string) (predictedLen int) {
// applyOnce is a helper func to avoid many if-else statements in tht switch below.
applyOnce := func(isSet *bool, fallback, applicator func(string) int, verb string) int {
if !*isSet {
*isSet = true
return applicator(verb)
} else {
return fallback(verb)
}
}
// it guarantees that "verb" starts from "{{",
// so we can remove leading "{{" and trailing "}}"
switch verb = verb[2 : len(verb)-2]; {
case hpm(verb, cevtCaller):
return applyOnce(&ce.cf.isSet, ce.rvJustText, ce.rvCaller, verb)
case hpm(verb, cevtColor):
return ce.rvColor(verb)
case hpm(verb, cevtLevel):
return ce.rvLevel(verb)
case hpm(verb, cevtTime):
return ce.rvTime(verb)
case hpm(verb, cevtMessage):
return applyOnce(&ce.bf.isSet, ce.rvJustText, ce.rvBody, verb)
case hpm(verb, cevtFields):
return applyOnce(&ce.ff.isSet, ce.rvJustText, ce.rvFields, verb)
case hpm(verb, cevtStacktrace):
return applyOnce(&ce.sf.isSet, ce.rvJustText, ce.rvStacktrace, verb)
default:
// incorrect verb, treat it as "just text" verb
return ce.rvJustText(verb)
}
}
// rvHelper is a part of "resolve verb" functions but moreover it's a helper.
// This function literally have no recognition algorithm but splits 'verb'
// to verb parts (ignoring first part, assuming that its verb's name) and then
// calls 'resolver' for each that part.
// Stops splitting and calling 'resolver' if it's return 'false' one time.
//
// It guarantees that if 'verbPart' is empty, 'resolver' won't be called for that
// and processing will stopped.
//
// Requirements:
// 'verb' != "", 'resolver' != nil. Otherwise no-op.
func (_ *CI_ConsoleEncoder) rvHelper(verb string, resolver func(verbPart string) (continue_ bool)) {
if verb == "" || resolver == nil {
return
}
// skip verb name
if idx := strings.IndexByte(verb, _CICE_VERB_SEPARATOR); idx != -1 {
verb = verb[idx+1:]
} else {
// there is no verb separator, it's simple verb. Do nothing
return
}
for idx := strings.IndexByte(verb, _CICE_VERB_SEPARATOR); idx != -1; {
if idx == 0 || !resolver(verb[:idx]) {
goto STOP
}
verb = verb[idx+1:]
idx = strings.IndexByte(verb, _CICE_VERB_SEPARATOR)
}
// parse last entity
if !resolver(verb) {
goto STOP
}
STOP:
return
}
// rvJustText is a part of "resolve verb" functions.
//
func (ce *CI_ConsoleEncoder) rvJustText(text string) (predictedLen int) {
if text != "" {
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_JUST_TEXT,
value: text,
})
}
return len(text)
}
// rvLevel is a part of "resolve verb" functions.
//
func (ce *CI_ConsoleEncoder) rvLevel(verb string) (predictedLen int) {
formattedLevel := _CICE_LF_FULL_NORMAL
if idx := strings.IndexByte(verb, _CICE_VERB_SEPARATOR); idx != -1 {
switch verb[idx+1:] {
case "d", "D": formattedLevel = _CICE_LF_NUMBER
case "s": formattedLevel = _CICE_LF_SHORT_NORMAL
case "S": formattedLevel = _CICE_LF_SHORT_UPPER_CASE
case "ss": formattedLevel = _CICE_LF_FULL_NORMAL
case "SS": formattedLevel = _CICE_LF_FULL_UPPER_CASE
}
}
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_LEVEL | (formattedLevel << 8),
})
return predictedLen
}
// rvTime is a part of "resolve verb" functions.
//
func (ce *CI_ConsoleEncoder) rvTime(verb string) (predictedLen int) {
format := _CICE_DEFAULT_TIME_FORMAT
formattedTime := _CICE_FormatPartType(0)
(*CI_ConsoleEncoder)(nil).rvHelper(verb, func(verbPart string) (continue_ bool) {
if verbPart = strings.TrimSpace(format); verbPart != "" {
switch predefined := strings.ToUpper(verbPart); predefined {
case "UNIX", "TIMESTAMP": formattedTime = _CICE_TF_TIMESTAMP
case "ANSIC": formattedTime = _CICE_TF_ANSIC
case "UNIXDATE", "UNIX_DATE": formattedTime = _CICE_TF_UNIXDATE
case "RUBYDATE", "RUBY_DATE": formattedTime = _CICE_TF_RUBYDATE
case "RFC822": formattedTime = _CICE_TF_RFC822
case "RFC822Z": formattedTime = _CICE_TF_RFC822_Z
case "RFC850": formattedTime = _CICE_TF_RFC850
case "RFC1123": formattedTime = _CICE_TF_RFC1123
case "RFC1123Z": formattedTime = _CICE_TF_RFC1123_Z
case "RFC3339": formattedTime = _CICE_TF_RFC3339
default: format = verbPart
}
}
return false // only first time verb is allowed and will be parsed
})
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_TIME | (formattedTime << 8),
value: format,
})
return len(format) + 10 // stock for some weekdays
}
//
// ""
func (ce *CI_ConsoleEncoder) rvColor(verb string) (predictedLen int) {
if idx := strings.IndexByte(verb, _CICE_VERB_SEPARATOR); idx == -1 {
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_COLOR_FOR_LEVEL,
})
return ce.colorMapMax
}
if encodedColor := ce.rvColorHelper(verb); encodedColor != "" {
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_COLOR_CUSTOM,
value: encodedColor,
})
return len(encodedColor)
} else {
return ce.rvJustText(verb)
}
}
//
func (_ *CI_ConsoleEncoder) rvColorHelper(colorVerb string) string {
cb := colorBuilder{}
cb.init()
(*CI_ConsoleEncoder)(nil).rvHelper(colorVerb, func(verbPart string) (continue_ bool) {
return cb.parseEntity(verbPart)
})
return cb.encode()
}
// rvBody is a part of "resolve verb" functions.
// rvBody tries to parse 'verb' as logger Entry's body and anyway indicates that
// here will be stored Entry's body.
//
// Verb's arguments:
// - For non-empty body:
// - "?^<text>": <text> will be prepended to the Entry's body at the runtime.
// - "?$<text>": <text> will be appended to the Entry's body at the runtime.
func (ce *CI_ConsoleEncoder) rvBody(verb string) (predictedLen int) {
(*CI_ConsoleEncoder)(nil).rvHelper(verb, func(verbPart string) (continue_ bool) {
switch {
case strings.HasPrefix(verbPart, "?^"): ce.bf.beforeBody = verbPart[2:]
case strings.HasPrefix(verbPart, "?$"): ce.bf.afterBody = verbPart[2:]
default: return false
}
return true
})
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_BODY,
})
return 256 + len(ce.bf.beforeBody) + len(ce.bf.afterBody)
}
//
func (ce *CI_ConsoleEncoder) rvCaller(verb string) (predictedLen int) {
isAdd := true
formatPrefixes := []string{"f", "F"}
(*CI_ConsoleEncoder)(nil).rvHelper(verb, func(verbPart string) (continue_ bool) {
switch {
case verbPart == "0": isAdd = false
case hpm(verbPart, formatPrefixes): predictedLen += ce.rvCallerFormat(verbPart[1:])
default: return false
}
return true
})
if isAdd {
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_CALLER,
})
return predictedLen
} else {
return 0
}
}
//
func (ce *CI_ConsoleEncoder) rvCallerFormat(f string) (predictedLen int) {
if f == "" || f == "d" || f == "D" {
ce.cf.isDefault = true
return 256
}
j := 0 // index of ce.cf.parts
for _, fc := range f {
t := _CICE_CF_TYPE_SEPARATOR // by default threat it as a separator
switch fc {
case 'w': t = _CICE_CF_TYPE_FUNC_SHORT
case 'W': t = _CICE_CF_TYPE_FUNC_FULL
case 'f': t = _CICE_CF_TYPE_FILE_SHORT
case 'F': t = _CICE_CF_TYPE_FILE_FULL
case 'l', 'L': t = _CICE_CF_TYPE_LINE_NUM
case 'p', 'P': t = _CICE_CF_TYPE_PKG_FULL
default:
switch ce.cf.parts[j].typ {
default:
j++
fallthrough
case 0:
ce.cf.parts[j].typ = _CICE_CF_TYPE_SEPARATOR
fallthrough
case -1:
ce.cf.parts[j].val += string(fc)
}
continue
}
// maybe it's already existed? skip it if it so
for i, n := 0, len(ce.cf.parts); i < n && ce.cf.parts[i].typ != 0; i++ {
if ce.cf.parts[i].typ == t {
t = _CICE_CF_TYPE_SEPARATOR
}
}
if t != _CICE_CF_TYPE_SEPARATOR {
if ce.cf.parts[j].typ != 0 {
j++
}
ce.cf.parts[j].typ = t
}
}
return 256
}
// rvFields is a part of "resolve verb" functions.
// rvFields tries to parse 'verb' as logger Entry's fields (not attached Error's)
// but anyway indicates that here will be stored Entry's body.
//
// Verb's arguments:
// - If at least one Field presented:
// - "?^<text>": <text> will be write before any Field is written at the runtime.
// - "?$<text>": <text> will be appended to the end of last Field at the runtime.
// - "k<text>": <text> will be written before Field's keys is written.
// - "v<text>": <text> will be written before Field's value is written.
// - "e<text>": <text> will be written after Field's value excluding last.
// - "l<text>": <text> will be written at the each new line of fields' part set.
// - "*<int>": <int> is how much fields are placed at the one line
// (by default: 4. Use <= 0 value to place all fields at the one line).
func (ce *CI_ConsoleEncoder) rvFields(verb string) (predictedLen int) {
ce.ff.itemsPerLine = 4
(*CI_ConsoleEncoder)(nil).rvHelper(verb, func(verbPart string) (continue_ bool) {
switch upperCased := strings.ToUpper(verbPart); {
case strings.HasPrefix(verbPart, "?^"): ce.ff.beforeFields = verbPart[2:]
case strings.HasPrefix(verbPart, "?$"): ce.ff.afterFields = verbPart[2:]
case strings.HasPrefix(upperCased, "LE"): ce.ff.afterNewLineForError = verbPart[2:]
case upperCased[0] == 'L': ce.ff.afterNewLine = verbPart[1:]
case upperCased[0] == 'K': ce.ff.beforeKey = verbPart[1:]
case upperCased[0] == 'V': ce.ff.afterKey = verbPart[1:]
case upperCased[0] == 'E': ce.ff.afterValue = verbPart[1:]
case verbPart[0] == '*':
if perLine_, err := strconv.Atoi(verbPart[1:]); err == nil {
if perLine_ < 0 {
ce.ff.itemsPerLine = 0
} else {
ce.ff.itemsPerLine = int16(perLine_)
}
}
default:
return false
}
return true
})
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_FIELDS,
})
return 512 + len(ce.ff.beforeFields) + len(ce.ff.afterFields) + len(ce.ff.beforeKey) +
len(ce.ff.afterKey) + len(ce.ff.afterValue) + len(ce.ff.afterNewLine)
}
// rvStacktrace is a part of "resolve verb" functions.
func (ce *CI_ConsoleEncoder) rvStacktrace(verb string) (predictedLen int) {
(*CI_ConsoleEncoder)(nil).rvHelper(verb, func(verbPart string) (continue_ bool) {
switch {
case strings.HasPrefix(verbPart, "?^"): ce.sf.beforeStack = verbPart[2:]
case strings.HasPrefix(verbPart, "?$"): ce.sf.afterStack = verbPart[2:]
default: return false
}
return true
})
ce.formatParts = append(ce.formatParts, _CICE_FormatPart{
typ: _CICE_FPT_VERB_STACKTRACE,
})
return 2048
}
//
func (ce *CI_ConsoleEncoder) encode(e *Entry) []byte {
// TODO: Reuse allocated buffers
buf := make([]byte, 0, ce.minimumBufferLen)
allowEmpty := e.LogLetter.Items.Flags.TestAll(FLAG_INTEGRATOR_IGNORE_EMPTY_PARTS)
for _, part := range ce.formatParts {
switch part.typ.Type() {
case _CICE_FPT_VERB_JUST_TEXT: buf = ce.encodeJustText(buf, part)
case _CICE_FPT_VERB_COLOR_CUSTOM: buf = ce.encodeColor(buf, part)
case _CICE_FPT_VERB_COLOR_FOR_LEVEL: buf = ce.encodeColorForLevel(buf, e)
case _CICE_FPT_VERB_BODY: buf = ce.encodeBody(buf, e)
case _CICE_FPT_VERB_TIME: buf = ce.encodeTime(e, part, buf)
case _CICE_FPT_VERB_LEVEL: buf = ce.encodeLevel(buf, part, e)
case _CICE_FPT_VERB_STACKTRACE: buf = ce.encodeStacktrace(buf, e, allowEmpty)
case _CICE_FPT_VERB_CALLER: buf = ce.encodeCaller(buf, e)
case _CICE_FPT_VERB_FIELDS:
buf = ce.encodeFields(buf, e.LogLetter.SystemFields, allowEmpty, false)
if e.ErrLetter != nil {
buf = ce.encodeFields(buf, e.ErrLetter.SystemFields, allowEmpty, false)
}
buf = ce.encodeFields(buf, e.LogLetter.Items.Fields, allowEmpty, false)
}
}
return buf
}
// easy case because fp.value is the text we should add.
func (ce *CI_ConsoleEncoder) encodeJustText(to []byte, fp _CICE_FormatPart) []byte {
return bufw(to, fp.value)
}
//
func (ce *CI_ConsoleEncoder) encodeLevel(to []byte, fp _CICE_FormatPart, e *Entry) []byte {
formattedLevel := ""
switch fp.typ.Data() {
case _CICE_LF_NUMBER: formattedLevel = strconv.Itoa(int(e.Level))
case _CICE_LF_SHORT_NORMAL: formattedLevel = e.Level.String()[:3]
case _CICE_LF_SHORT_UPPER_CASE: formattedLevel = strings.ToUpper(e.Level.String()[:3])
case _CICE_LF_FULL_NORMAL: formattedLevel = e.Level.String()
case _CICE_LF_FULL_UPPER_CASE: formattedLevel = strings.ToUpper(e.Level.String())
}
return bufw(to, formattedLevel)
}
//
func (ce *CI_ConsoleEncoder) encodeTime(e *Entry, fp _CICE_FormatPart, to []byte) []byte {
formattedTime := ""
switch fp.typ.Data() {
case _CICE_TF_TIMESTAMP: formattedTime = strconv.FormatInt(e.Time.Unix(), 10)
case _CICE_TF_ANSIC: formattedTime = e.Time.Format(time.ANSIC)
case _CICE_TF_UNIXDATE: formattedTime = e.Time.Format(time.UnixDate)
case _CICE_TF_RUBYDATE: formattedTime = e.Time.Format(time.RubyDate)
case _CICE_TF_RFC822: formattedTime = e.Time.Format(time.RFC822)
case _CICE_TF_RFC822_Z: formattedTime = e.Time.Format(time.RFC822Z)
case _CICE_TF_RFC850: formattedTime = e.Time.Format(time.RFC850)
case _CICE_TF_RFC1123: formattedTime = e.Time.Format(time.RFC1123)
case _CICE_TF_RFC1123_Z: formattedTime = e.Time.Format(time.RFC1123Z)
case _CICE_TF_RFC3339: formattedTime = e.Time.Format(time.RFC3339)
default: formattedTime = e.Time.Format(fp.value)
}
return bufw(to, formattedTime)
}
// easy case because ASCII sequence already generated at the rvColor method.
func (ce *CI_ConsoleEncoder) encodeColor(to []byte, fp _CICE_FormatPart) []byte {
return bufw(to, fp.value)
}
//
func (ce *CI_ConsoleEncoder) encodeColorForLevel(to []byte, e *Entry) []byte {
if color := ce.colorMap[e.Level]; color != "" {
return bufw(to, color)
}
// TODO: mocked (find the closes log level and use that's color)
return to
}
// easy case because e.Message is the text we should add.
func (ce *CI_ConsoleEncoder) encodeBody(to []byte, e *Entry) []byte {
body := e.LogLetter.Items.Message
//if body == "" && e.ErrLetter != nil {
// // TODO: It's guaranteed that first err letter's item has non-empty
// // and marked message (it's what error has been constructed w/)
// // but anyway the better way is to find first marked letter item w/ message
// body = e.ErrLetter.Items.Message
//}
if body == "" {
return to
}
if ce.bf.beforeBody != "" {
to = bufw(to, ce.bf.beforeBody)
}
to = bufw(to, body)
if ce.bf.afterBody != "" {
to = bufw(to, ce.bf.afterBody)
}
return to
}
func (ce *CI_ConsoleEncoder) encodeCaller(to []byte, e *Entry) []byte {
var frame ekasys.StackFrame
switch {
case len(e.LogLetter.StackTrace) > 0:
frame = e.LogLetter.StackTrace[0]
case e.ErrLetter != nil:
frame = e.ErrLetter.StackTrace[0]
default:
return to
}
return ce.encodeStackFrame(to, frame, nil, false)
}
//
func (ce *CI_ConsoleEncoder) encodeFields(
to []byte,
fields []ekafield.Field,
allowEmpty,
isErrors bool,
) []byte {
if len(fields) == 0 {
return to
}
if !isErrors && ce.ff.beforeFields != "" {
to = bufw(to, ce.ff.beforeFields)
}
newLine := ce.ff.afterNewLine
if isErrors {
newLine = ce.ff.afterNewLineForError
}
if newLine != "" {
to = bufw(to, newLine)
}
unnamedFieldIdx := 0
writtenFieldIdx := int16(0)
for i, n := int16(0), int16(len(fields)); i < n; i++ {
// write new line and new line title
if ce.ff.itemsPerLine > 0 &&
writtenFieldIdx != 0 &&
writtenFieldIdx%ce.ff.itemsPerLine == 0 {
to = bufw(to, "\n")
newLine := ce.ff.afterNewLine
if isErrors {
newLine = ce.ff.afterNewLineForError
}
if newLine != "" {
to = bufw(to, newLine)
}
}
// Despite the fact, that letter.ParseTo() already contains IsZero() call
// it's only vary fields filtering (fields with the "?" at the end of name).