-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc.go
1565 lines (1388 loc) · 37.9 KB
/
pc.go
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
package slog
import (
"bytes"
"encoding"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"sync"
"time"
"unicode/utf8"
"github.com/hedzr/is/term/color"
)
var printCtxPool = sync.Pool{New: func() any {
return newPrintCtx()
// return &PrintCtx{
// buf: make([]byte, 0, 1024),
// noQuoted: true,
// clr: clrBasic,
// bg: clrNone,
// }
}}
// func newPrintCtxAsAny() any { return newPrintCtx() }
func newPrintCtx() *PrintCtx {
return &PrintCtx{
buf: make([]byte, 0, 1024),
noQuoted: true,
clr: clrBasic,
bg: clrNone,
}
}
// PrintCtx when formatting logging line in text logger
type PrintCtx struct {
buf []byte // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
lastRead readOp // last read operation, so that Unread* can work correctly.
noQuoted bool // should quote the string values? default is YES
jsonMode bool // should print out the logging with JSON format? default is NO.
noColor bool // use ansi escape sequences in console/terminal? default is ON.
layout string // time layout for formatting
utcTime int // non-set(0), local(1) or utc(2) time? default is local time mode.
lvl Level
msg string
firstLine string
restLines string
eol bool
kvps Attrs
clr, bg color.Color
now time.Time
stackFrame uintptr
prefix string
inGroupedMode bool
// curdir string
valueStringer ValueStringer
}
func (s *PrintCtx) source() Source { return getpcsource(s.stackFrame) }
func (s *PrintCtx) setentry(e *entry) {
s.buf = s.buf[:0]
s.jsonMode = e.useJSON
useColor := e.useColor
if e.useJSON && useColor {
useColor = false
}
s.noColor = !useColor
s.layout = e.timeLayout
s.utcTime = e.modeUTC
s.valueStringer = e.valueStringer
s.lvl = e.level
s.kvps = e.attrs
}
func (s *PrintCtx) set(e *entry, lvl Level, timestamp time.Time, stackFrame uintptr, msg string, kvps Attrs) {
s.setentry(e)
s.lvl = lvl
s.now = timestamp
s.stackFrame = stackFrame
s.msg = msg
s.kvps = kvps
}
//
//
//
// smallBufferSize is an initial allocation minimal capacity.
const smallBufferSize = 64
// The readOp constants describe the last action performed on
// the buffer, so that UnreadRune and UnreadByte can check for
// invalid usage. opReadRuneX constants are chosen such that
// converted to int they correspond to the rune size that was read.
type readOp int8
// Don't use iota for these, as the values need to correspond with the
// names and comments, which is easier to see when being explicit.
const (
opRead readOp = -1 // Any other read operation.
opInvalid readOp = 0 // Non-read operation.
opReadRune1 readOp = 1 // Read rune of size 1.
opReadRune2 readOp = 2 // Read rune of size 2.
opReadRune3 readOp = 3 // Read rune of size 3.
opReadRune4 readOp = 4 // Read rune of size 4.
)
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
var ErrTooLarge = errors.New("logg/slog.PrintCtx: too large")
var errNegativeRead = errors.New("logg/slog.PrintCtx: reader returned negative count from Read")
const maxInt = int(^uint(0) >> 1)
// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
// The slice is valid for use only until the next buffer modification (that is,
// only until the next call to a method like Read, Write, Reset, or Truncate).
// The slice aliases the buffer content at least until the next buffer modification,
// so immediate changes to the slice will affect the result of future reads.
func (s *PrintCtx) Bytes() []byte { return s.buf[s.off:] }
// AvailableBuffer returns an empty buffer with b.Available() capacity.
// This buffer is intended to be appended to and
// passed to an immediately succeeding Write call.
// The buffer is only valid until the next write operation on b.
func (s *PrintCtx) AvailableBuffer() []byte { return s.buf[len(s.buf):] }
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
//
// To build strings more efficiently, see the strings.Builder type.
func (s *PrintCtx) String() string {
if s == nil {
// Special case, useful in debugging.
return "<nil>"
}
return string(s.buf[s.off:])
}
// empty reports whether the unread portion of the buffer is empty.
func (s *PrintCtx) empty() bool { return len(s.buf) <= s.off }
// Len returns the number of bytes of the unread portion of the buffer;
// b.Len() == len(b.Bytes()).
func (s *PrintCtx) Len() int { return len(s.buf) - s.off }
// Cap returns the capacity of the buffer's underlying byte slice, that is, the
// total space allocated for the buffer's data.
func (s *PrintCtx) Cap() int { return cap(s.buf) }
// Available returns how many bytes are unused in the buffer.
func (s *PrintCtx) Available() int { return cap(s.buf) - len(s.buf) }
// Truncate discards all but the first n unread bytes from the buffer
// but continues to use the same allocated storage.
// It panics if n is negative or greater than the length of the buffer.
func (s *PrintCtx) Truncate(n int) {
if n == 0 {
s.Reset()
return
}
s.lastRead = opInvalid
if n < 0 || n > s.Len() {
panic("logg/slog.PrintCtx: truncation out of range")
}
s.buf = s.buf[:s.off+n]
}
// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as Truncate(0).
func (s *PrintCtx) Reset() {
s.buf = s.buf[:0]
s.off = 0
s.lastRead = opInvalid
}
// tryGrowByReslice is an inlineable version of grow for the fast-case where the
// internal buffer only needs to be resliced.
// It returns the index where bytes should be written and whether it succeeded.
func (s *PrintCtx) tryGrowByReslice(n int) (int, bool) {
if l := len(s.buf); n <= cap(s.buf)-l {
s.buf = s.buf[:l+n]
return l, true
}
return 0, false
}
// grow grows the buffer to guarantee space for n more bytes.
// It returns the index where bytes should be written.
// If the buffer can't grow it will panic with ErrTooLarge.
func (s *PrintCtx) grow(n int) int {
m := s.Len()
// If buffer is empty, reset to recover space.
if m == 0 && s.off != 0 {
s.Reset()
}
// Try to grow by means of a reslice.
if i, ok := s.tryGrowByReslice(n); ok {
return i
}
if s.buf == nil && n <= smallBufferSize {
s.buf = make([]byte, n, smallBufferSize)
return 0
}
c := cap(s.buf)
if n <= c/2-m {
// We can slide things down instead of allocating a new
// slice. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we
// don't spend all our time copying.
copy(s.buf, s.buf[s.off:])
} else if c > maxInt-c-n {
panic(ErrTooLarge)
} else {
// Add s.off to account for s.buf[:s.off] being sliced off the front.
s.buf = growSlice(s.buf[s.off:], s.off+n)
}
// Restore b.off and len(b.buf).
s.off = 0
s.buf = s.buf[:m+n]
return m
}
// Grow grows the buffer's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to the
// buffer without another allocation.
// If n is negative, Grow will panic.
// If the buffer can't grow it will panic with ErrTooLarge.
func (s *PrintCtx) Grow(n int) {
if n < 0 {
panic("logg/slog.PrintCtx.Grow: negative count")
}
m := s.grow(n)
s.buf = s.buf[:m]
}
// Write appends the contents of p to the buffer, growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large, Write will panic with ErrTooLarge.
func (s *PrintCtx) Write(p []byte) (n int, err error) {
s.lastRead = opInvalid
m, ok := s.tryGrowByReslice(len(p))
if !ok {
m = s.grow(len(p))
}
return copy(s.buf[m:], p), nil
}
// WriteString appends the contents of s to the buffer, growing the buffer as
// needed. The return value n is the length of s; err is always nil. If the
// buffer becomes too large, WriteString will panic with ErrTooLarge.
func (s *PrintCtx) WriteString(str string) (n int, err error) {
s.lastRead = opInvalid
m, ok := s.tryGrowByReslice(len(str))
if !ok {
m = s.grow(len(str))
}
return copy(s.buf[m:], str), nil
}
// MinRead is the minimum slice size passed to a Read call by
// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
// what is required to hold the contents of r, ReadFrom will not grow the
// underlying buffer.
const MinRead = 512
// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (s *PrintCtx) ReadFrom(r io.Reader) (n int64, err error) {
s.lastRead = opInvalid
for {
i := s.grow(MinRead)
s.buf = s.buf[:i]
m, e := r.Read(s.buf[i:cap(s.buf)])
if m < 0 {
panic(errNegativeRead)
}
s.buf = s.buf[:i+m]
n += int64(m)
if e == io.EOF {
return n, nil // e is EOF, so return nil explicitly
}
if e != nil {
return n, e
}
}
}
// growSlice grows b by n, preserving the original content of b.
// If the allocation fails, it panics with ErrTooLarge.
func growSlice(b []byte, n int) []byte {
defer func() {
if recover() != nil {
panic(ErrTooLarge)
}
}()
// TODO(http://golang.org/issue/51462): We should rely on the append-make
// pattern so that the compiler can call runtime.growslice. For example:
// return append(b, make([]byte, n)...)
// This avoids unnecessary zero-ing of the first len(b) bytes of the
// allocated slice, but this pattern causes b to escape onto the heap.
//
// Instead use the append-make pattern with a nil slice to ensure that
// we allocate buffers rounded up to the closest size class.
c := len(b) + n // ensure enough space for n elements
if c < 2*cap(b) {
// The growth rate has historically always been 2x. In the future,
// we could rely purely on append to determine the growth rate.
c = 2 * cap(b)
}
b2 := append([]byte(nil), make([]byte, c)...)
copy(b2, b)
return b2[:len(b)]
}
// WriteTo writes data to w until the buffer is drained or an error occurs.
// The return value n is the number of bytes written; it always fits into an
// int, but it is int64 to match the io.WriterTo interface. Any error
// encountered during the write is also returned.
func (s *PrintCtx) WriteTo(w io.Writer) (n int64, err error) {
s.lastRead = opInvalid
if nBytes := s.Len(); nBytes > 0 {
m, e := w.Write(s.buf[s.off:])
if m > nBytes {
panic("logg/slog.PrintCtx.WriteTo: invalid Write count")
}
s.off += m
n = int64(m)
if e != nil {
return n, e
}
// all bytes should have been written, by definition of
// Write method in io.Writer
if m != nBytes {
return n, io.ErrShortWrite
}
}
// Buffer is now empty; reset.
s.Reset()
return n, nil
}
// WriteByte appends the byte c to the buffer, growing the buffer as needed.
// The returned error is always nil, but is included to match bufio.Writer's
// WriteByte. If the buffer becomes too large, WriteByte will panic with
// ErrTooLarge.
func (s *PrintCtx) WriteByte(c byte) error {
s.lastRead = opInvalid
m, ok := s.tryGrowByReslice(1)
if !ok {
m = s.grow(1)
}
s.buf[m] = c
return nil
}
// WriteRune appends the UTF-8 encoding of Unicode code point r to the
// buffer, returning its length and an error, which is always nil but is
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
// if it becomes too large, WriteRune will panic with ErrTooLarge.
func (s *PrintCtx) WriteRune(r rune) (n int, err error) {
// Compare as uint32 to correctly handle negative runes.
if uint32(r) < utf8.RuneSelf {
s.WriteByte(byte(r))
return 1, nil
}
s.lastRead = opInvalid
m, ok := s.tryGrowByReslice(utf8.UTFMax)
if !ok {
m = s.grow(utf8.UTFMax)
}
s.buf = utf8.AppendRune(s.buf[:m], r)
return len(s.buf) - m, nil
// s.buf= append(s.buf, []byte(r)...)
}
// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (s *PrintCtx) Read(p []byte) (n int, err error) {
s.lastRead = opInvalid
if s.empty() {
// Buffer is empty, reset to recover space.
s.Reset()
if len(p) == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(p, s.buf[s.off:])
s.off += n
if n > 0 {
s.lastRead = opRead
}
return n, nil
}
// Next returns a slice containing the next n bytes from the buffer,
// advancing the buffer as if the bytes had been returned by Read.
// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
// The slice is only valid until the next call to a read or write method.
func (s *PrintCtx) Next(n int) []byte {
s.lastRead = opInvalid
m := s.Len()
if n > m {
n = m
}
data := s.buf[s.off : s.off+n]
s.off += n
if n > 0 {
s.lastRead = opRead
}
return data
}
// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
func (s *PrintCtx) ReadByte() (byte, error) {
if s.empty() {
// Buffer is empty, reset to recover space.
s.Reset()
return 0, io.EOF
}
c := s.buf[s.off]
s.off++
s.lastRead = opRead
return c, nil
}
// ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available, the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func (s *PrintCtx) ReadRune() (r rune, size int, err error) {
if s.empty() {
// Buffer is empty, reset to recover space.
s.Reset()
return 0, 0, io.EOF
}
c := s.buf[s.off]
if c < utf8.RuneSelf {
s.off++
s.lastRead = opReadRune1
return rune(c), 1, nil
}
r, n := utf8.DecodeRune(s.buf[s.off:])
s.off += n
s.lastRead = readOp(n)
return r, n, nil
}
// UnreadRune unreads the last rune returned by ReadRune.
// If the most recent read or write operation on the buffer was
// not a successful ReadRune, UnreadRune returns an error. (In this regard
// it is stricter than UnreadByte, which will unread the last byte
// from any read operation.)
func (s *PrintCtx) UnreadRune() error {
if s.lastRead <= opInvalid {
return errors.New("logg/slog.PrintCtx: UnreadRune: previous operation was not a successful ReadRune")
}
if s.off >= int(s.lastRead) {
s.off -= int(s.lastRead)
}
s.lastRead = opInvalid
return nil
}
var errUnreadByte = errors.New("logg/slog.PrintCtx: UnreadByte: previous operation was not a successful read")
// UnreadByte unreads the last byte returned by the most recent successful
// read operation that read at least one byte. If a write has happened since
// the last read, if the last read returned an error, or if the read read zero
// bytes, UnreadByte returns an error.
func (s *PrintCtx) UnreadByte() error {
if s.lastRead == opInvalid {
return errUnreadByte
}
s.lastRead = opInvalid
if s.off > 0 {
s.off--
}
return nil
}
// ReadBytes reads until the first occurrence of delim in the input,
// returning a slice containing the data up to and including the delimiter.
// If ReadBytes encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func (s *PrintCtx) ReadBytes(delim byte) (line []byte, err error) {
slice, err := s.readSlice(delim)
// return a copy of slice. The buffer's backing array may
// be overwritten by later calls.
line = append(line, slice...)
return line, err
}
// readSlice is like ReadBytes but returns a reference to internal buffer data.
func (s *PrintCtx) readSlice(delim byte) (line []byte, err error) {
i := bytes.IndexByte(s.buf[s.off:], delim)
end := s.off + i + 1
if i < 0 {
end = len(s.buf)
err = io.EOF
}
line = s.buf[s.off:end]
s.off = end
s.lastRead = opRead
return line, err
}
// ReadString reads until the first occurrence of delim in the input,
// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end
// in delim.
func (s *PrintCtx) ReadString(delim byte) (line string, err error) {
slice, err := s.readSlice(delim)
return string(slice), err
}
// NewPrintCtx creates and initializes a new Buffer using buf as its
// initial contents. The new Buffer takes ownership of buf, and the
// caller should not use buf after this call. NewBuffer is intended to
// prepare a Buffer to read existing data. It can also be used to set
// the initial size of the internal buffer for writing. To do that,
// buf should have the desired capacity but a length of zero.
//
// In most cases, new(Buffer) (or just declaring a Buffer variable) is
// sufficient to initialize a Buffer.
func NewPrintCtx(buf []byte) *PrintCtx { return &PrintCtx{buf: buf} }
// NewPrintCtxString creates and initializes a new Buffer using string s as its
// initial contents. It is intended to prepare a buffer to read an existing
// string.
//
// In most cases, new(Buffer) (or just declaring a Buffer variable) is
// sufficient to initialize a Buffer.
func NewPrintCtxString(s string) *PrintCtx {
return &PrintCtx{buf: []byte(s)}
}
//
//
//
// func (s *PrintCtx) addRune(r rune) { s.WriteRune(r) }
// func (s *PrintCtx) addString(name string, value string) {
// // s.Grow(len(name)*3 + 1 + len(value)*3)
// s.pcAppendStringKey(name)
// s.pcAppendColon()
// s.pcAppendQuotedStringValue(value)
// }
func (s *PrintCtx) AddInt64(name string, value int64) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddInt32(name string, value int32) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddInt16(name string, value int16) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddInt8(name string, value int8) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddInt(name string, value int) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddPrefixedInt(prefix, name string, value int) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKeyPrefixed(name, prefix)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
itoaS(s, value)
}
func (s *PrintCtx) AddUint64(name string, value uint64) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(uintToString(value))
utoaS(s, value)
}
func (s *PrintCtx) AddUint32(name string, value uint32) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(uintToString(value))
utoaS(s, value)
}
func (s *PrintCtx) AddUint16(name string, value uint16) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(uintToString(value))
utoaS(s, value)
}
func (s *PrintCtx) AddUint8(name string, value uint8) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(uintToString(value))
utoaS(s, value)
}
func (s *PrintCtx) AddUint(name string, value uint) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(uintToString(value))
utoaS(s, value)
}
func (s *PrintCtx) AddFloat64(name string, value float64) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(floatToString(value))
ftoaS(s, value)
}
func (s *PrintCtx) AddFloat32(name string, value float32) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(floatToString(value))
ftoaS(s, value)
}
func (s *PrintCtx) AddComplex128(name string, value complex128) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(complexToString(value))
ctoaS(s, value)
}
func (s *PrintCtx) AddComplex64(name string, value complex64) {
// s.Grow(len(name)*3 + 1 + 32)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(complexToString(value))
ctoaS(s, value)
}
func (s *PrintCtx) AddBool(name string, value bool) {
// s.Grow(len(name)*3 + 1 + 5)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(boolToString(value))
btoaS(s, value)
}
func (s *PrintCtx) AddString(name string, value string) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKey(name)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
if s.noColor {
s.pcAppendQuotedStringValue(value)
} else {
s.pcAppendString(value)
}
}
func (s *PrintCtx) AddPrefixedString(prefix, name string, value string) {
// s.Grow(len(name)*3 + 1 + 10)
s.pcAppendStringKeyPrefixed(name, prefix)
s.pcAppendColon()
// s.pcAppendStringValue(intToString(value))
if s.noColor {
s.pcAppendQuotedStringValue(value)
} else {
s.pcAppendString(value)
}
}
func (s *PrintCtx) AppendRune(value rune) {
s.pcAppendRune(value)
}
func (s *PrintCtx) AppendByte(value byte) {
s.pcAppendByte(value)
}
func (s *PrintCtx) AppendBytes(value []byte) {
_, err := s.Write(value)
if err != nil {
hintInternal(err, "PrintCtx.AppendBytes failed")
}
}
func (s *PrintCtx) AppendRunes(value []rune) {
s.buf = append(s.buf, []byte(string(value))...)
}
//
//
//
func (s *PrintCtx) AppendInt(val int) {
itoaS(s, val)
// s.WriteString(intToStringEx(val, 10))
}
//
func (s *PrintCtx) preCheck() {
// if s.jsonMode {
// s.WriteRune('{')
// }
}
func (s *PrintCtx) postCheck() {
// if s.jsonMode {
// s.WriteRune('}')
// }
}
//
func (s *PrintCtx) pcAppendByte(b byte) {
s.checkerr(s.WriteByte(b))
}
func (s *PrintCtx) pcAppendRune(r rune) {
s.preCheck()
_, err := s.WriteRune(r)
if err != nil {
hintInternal(err, "PrintCtx.pcAppendRune failed")
}
}
func (s *PrintCtx) pcTryQuoteValue(val string) {
s.preCheck()
if s.noColor {
// return strconv.Quote(val)
s.pcAppendByte('"')
s.appendEscapedJSONString(val)
s.pcAppendByte('"')
} else {
s.pcAppendStringValue(val)
}
}
func (s *PrintCtx) pcQuoteValue(val string) {
s.pcAppendByte('"')
s.appendEscapedJSONString(val)
s.pcAppendByte('"')
}
func (s *PrintCtx) pcAppendColon() {
s.preCheck()
if s.jsonMode {
s.pcAppendByte(':')
} else {
s.pcAppendByte('=')
}
}
func (s *PrintCtx) pcAppendComma() {
if s.jsonMode {
s.pcAppendByte(',')
} else {
s.pcAppendByte(' ')
}
}
func (s *PrintCtx) pcAppendString(str string) {
s.preCheck()
_, err := s.WriteString(str)
if err != nil {
hintInternal(err, "PrintCtx.pcAppendString failed")
}
}
// pcAppendStringValue append string without quotes, the string represents a value
func (s *PrintCtx) pcAppendStringValue(str string) {
s.preCheck()
s.WriteString(str)
}
// pcAppendQuotedStringValue append string with quotes always, the string represents a value
func (s *PrintCtx) pcAppendQuotedStringValue(str string) {
s.preCheck()
s.WriteRune('"')
s.appendEscapedJSONString(str)
s.WriteRune('"')
}
// pcAppendStringKey appends string with quotes (in json mode), the string represents a key.
//
// If a PrintCtx is in non json mode, the key shouldn't wrapped with quotes.
func (s *PrintCtx) pcAppendStringKey(str string) {
s.preCheck()
if s.jsonMode {
// s.WriteString(strconv.Quote(str))
// s.Grow(2 + len([]byte(str)))
s.checkerr(s.WriteByte('"'))
s.WriteString(str)
s.checkerr(s.WriteByte('"'))
} else {
s.WriteString(str)
}
}
func (s *PrintCtx) pcAppendStringKeyPrefixed(str, prefix string) {
s.preCheck()
if s.jsonMode {
// s.WriteString(strconv.Quote(str))
// s.Grow(2 + len([]byte(str)))
s.checkerr(s.WriteByte('"'))
s.WriteString(prefix)
s.checkerr(s.WriteByte('.'))
s.WriteString(str)
s.checkerr(s.WriteByte('"'))
} else {
s.WriteString(prefix)
s.checkerr(s.WriteByte('.'))
s.WriteString(str)
}
}
// func (s *PrintCtx) appendRune(val rune) {
// s.preCheck()
// s.WriteRune(val)
// }
// func (s *PrintCtx) appendRunes(val []rune) {
// s.pcAppendString(string(val))
// }
// func (s *PrintCtx) appendRuneValue(val rune) {
// s.pcAppendStringValue(string(val))
// }
// func (s *PrintCtx) appendRunesValue(val []rune) {
// s.pcAppendStringValue(string(val))
// }
func (s *PrintCtx) appendValue(val any) {
switch z := val.(type) {
case nil:
s.pcAppendStringValue("<nil>")
case ObjectSerializer:
// pc.useColor = !s.noColor
// pc.clr = color.FgDarkColor
// pc.bg = clrNone
z.SerializeValueTo(s)
// z.SerializeValueTo(pc, prefix, inGrouping, !s.noColor, color.FgDarkColor, clrNone)
case ArrayMarshaller:
if err := z.MarshalSlogArray(s); err != nil {
hintInternal(err, "MarshalLogArray failed")
break
}
case ObjectMarshaller:
if err := z.MarshalSlogObject(s); err != nil {
hintInternal(err, "MarshalLogObject failed")
break
}
case time.Duration:
s.appendDuration(z)
case time.Time:
s.appendTime(z)
case []time.Time:
s.appendTimeSlice(z)
case []time.Duration:
s.appendDurationSlice(z)
case Level:
s.pcQuoteValue(z.String())
case error:
s.pcTryQuoteValue(z.Error())
case ToString:
s.pcQuoteValue(z.ToString())
case Stringer:
s.pcQuoteValue(z.String())
case string:
s.pcQuoteValue(z)
case bool:
btoaS(s, z)
case []byte:
s.appendBytes(z)
case []string:
s.appendStringSlice(z)
case []bool:
s.appendBoolSlice(z)
case []int:
intSliceTo(s, z)
case []int8:
intSliceTo(s, z)
case []int16:
intSliceTo(s, z)
case []int32:
intSliceTo(s, z)
case []int64:
intSliceTo(s, z)
case int:
itoaS(s, z)
case int8:
itoaS(s, z)
case int16:
itoaS(s, z)
case int32:
itoaS(s, z)
case int64:
itoaS(s, z)
case []uint:
uintSliceTo(s, z)
// case []uint8: // = []byte
case []uint16:
uintSliceTo(s, z)
case []uint32:
uintSliceTo(s, z)
case []uint64:
uintSliceTo(s, z)
case uint:
utoaS(s, z)
case uint8:
utoaS(s, z)
case uint16:
utoaS(s, z)
case uint32:
utoaS(s, z)
case uint64:
utoaS(s, z)
case []float32: