-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
clog.go
1489 lines (1336 loc) · 42.3 KB
/
clog.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
// Copyright 2013 Google Inc. All Rights Reserved.
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This code originated in the github.com/golang/glog package.
package log
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
stdLog "log"
"math"
"os"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/util/caller"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/sysutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/ttycolor"
"github.com/petermattis/goid"
)
// maxSyncDuration is set to a conservative value since this is a new mechanism.
// In practice, even a fraction of that would indicate a problem.
var maxSyncDuration = envutil.EnvOrDefaultDuration("COCKROACH_LOG_MAX_SYNC_DURATION", 30*time.Second)
const fatalErrorPostamble = `
****************************************************************************
This node experienced a fatal error (printed above), and as a result the
process is terminating.
Fatal errors can occur due to faulty hardware (disks, memory, clocks) or a
problem in CockroachDB. With your help, the support team at Cockroach Labs
will try to determine the root cause, recommend next steps, and we can
improve CockroachDB based on your report.
Please submit a crash report by following the instructions here:
https://github.com/cockroachdb/cockroach/issues/new/choose
If you would rather not post publicly, please contact us directly at:
The Cockroach Labs team appreciates your feedback.
`
// FatalChan is closed when Fatal is called. This can be used to make
// the process stop handling requests while the final log messages and
// crash report are being written.
func FatalChan() <-chan struct{} {
return logging.fatalCh
}
const severityChar = "IWEF"
const (
tracebackNone = iota
tracebackSingle
tracebackAll
)
// Obey the GOTRACEBACK environment variable for determining which stacks to
// output during a log.Fatal.
var traceback = func() int {
switch os.Getenv("GOTRACEBACK") {
case "none":
return tracebackNone
case "single", "":
return tracebackSingle
default: // "all", "system", "crash"
return tracebackAll
}
}()
// DisableTracebacks turns off tracebacks for log.Fatals. Returns a function
// that sets the traceback settings back to where they were.
// Only intended for use by tests.
func DisableTracebacks() func() {
oldVal := traceback
traceback = tracebackNone
return func() { traceback = oldVal }
}
// get returns the value of the Severity.
func (s *Severity) get() Severity {
return Severity(atomic.LoadInt32((*int32)(s)))
}
// set sets the value of the Severity.
func (s *Severity) set(val Severity) {
atomic.StoreInt32((*int32)(s), int32(val))
}
// Set is part of the flag.Value interface.
func (s *Severity) Set(value string) error {
var threshold Severity
// Is it a known name?
if v, ok := SeverityByName(value); ok {
threshold = v
} else {
v, err := strconv.Atoi(value)
if err != nil {
return err
}
threshold = Severity(v)
}
s.set(threshold)
return nil
}
// Name returns the string representation of the severity (i.e. ERROR, INFO).
func (s *Severity) Name() string {
return s.String()
}
// SeverityByName attempts to parse the passed in string into a severity. (i.e.
// ERROR, INFO). If it succeeds, the returned bool is set to true.
func SeverityByName(s string) (Severity, bool) {
s = strings.ToUpper(s)
if i, ok := Severity_value[s]; ok {
return Severity(i), true
}
switch s {
case "TRUE":
return Severity_INFO, true
case "FALSE":
return Severity_NONE, true
}
return 0, false
}
// Level is exported because it appears in the arguments to V and is
// the type of the v flag, which can be set programmatically.
// It's a distinct type because we want to discriminate it from logType.
// Variables of type level are only changed under logging.mu.
// The --verbosity flag is read only with atomic ops, so the state of the logging
// module is consistent.
// Level is treated as a sync/atomic int32.
// Level specifies a level of verbosity for V logs. *Level implements
// flag.Value; the --verbosity flag is of type Level and should be modified
// only through the flag.Value interface.
type level int32
// get returns the value of the Level.
func (l *level) get() level {
return level(atomic.LoadInt32((*int32)(l)))
}
// set sets the value of the Level.
func (l *level) set(val level) {
atomic.StoreInt32((*int32)(l), int32(val))
}
// String is part of the flag.Value interface.
func (l *level) String() string {
return strconv.FormatInt(int64(*l), 10)
}
// Set is part of the flag.Value interface.
func (l *level) Set(value string) error {
v, err := strconv.Atoi(value)
if err != nil {
return err
}
logging.mu.Lock()
defer logging.mu.Unlock()
logging.setVState(level(v), logging.vmodule.filter, false)
return nil
}
// moduleSpec represents the setting of the --vmodule flag.
type moduleSpec struct {
filter []modulePat
}
// modulePat contains a filter for the --vmodule flag.
// It holds a verbosity level and a file pattern to match.
type modulePat struct {
pattern string
literal bool // The pattern is a literal string
level level
}
// match reports whether the file matches the pattern. It uses a string
// comparison if the pattern contains no metacharacters.
func (m *modulePat) match(file string) bool {
if m.literal {
return file == m.pattern
}
match, _ := filepath.Match(m.pattern, file)
return match
}
func (m *moduleSpec) String() string {
// Lock because the type is not atomic. TODO: clean this up.
logging.mu.Lock()
defer logging.mu.Unlock()
var b bytes.Buffer
for i, f := range m.filter {
if i > 0 {
b.WriteRune(',')
}
fmt.Fprintf(&b, "%s=%d", f.pattern, f.level)
}
return b.String()
}
var errVmoduleSyntax = errors.New("syntax error: expect comma-separated list of filename=N")
// Syntax: --vmodule=recordio=2,file=1,gfs*=3
func (m *moduleSpec) Set(value string) error {
var filter []modulePat
for _, pat := range strings.Split(value, ",") {
if len(pat) == 0 {
// Empty strings such as from a trailing comma can be ignored.
continue
}
patLev := strings.Split(pat, "=")
if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 {
return errVmoduleSyntax
}
pattern := patLev[0]
v, err := strconv.Atoi(patLev[1])
if err != nil {
return errors.New("syntax error: expect comma-separated list of filename=N")
}
if v < 0 {
return errors.New("negative value for vmodule level")
}
if v == 0 {
continue // Ignore. It's harmless but no point in paying the overhead.
}
// TODO: check syntax of filter?
filter = append(filter, modulePat{pattern, isLiteral(pattern), level(v)})
}
logging.mu.Lock()
defer logging.mu.Unlock()
logging.setVState(logging.verbosity, filter, true)
return nil
}
// isLiteral reports whether the pattern is a literal string, that is, has no metacharacters
// that require filepath.Match to be called to match the pattern.
func isLiteral(pattern string) bool {
return !strings.ContainsAny(pattern, `\*?[]`)
}
// traceLocation represents the setting of the -log_backtrace_at flag.
type traceLocation struct {
file string
line int
}
// isSet reports whether the trace location has been specified.
// logging.mu is held.
func (t *traceLocation) isSet() bool {
return t.line > 0
}
// match reports whether the specified file and line matches the trace location.
// The argument file name is the full path, not the basename specified in the flag.
// logging.mu is held.
func (t *traceLocation) match(file string, line int) bool {
if t.line != line {
return false
}
if i := strings.LastIndexByte(file, '/'); i >= 0 {
file = file[i+1:]
}
return t.file == file
}
func (t *traceLocation) String() string {
// Lock because the type is not atomic. TODO: clean this up.
logging.mu.Lock()
defer logging.mu.Unlock()
return fmt.Sprintf("%s:%d", t.file, t.line)
}
var errTraceSyntax = errors.New("syntax error: expect file.go:234")
// Syntax: -log_backtrace_at=gopherflakes.go:234
// Note that unlike vmodule the file extension is included here.
func (t *traceLocation) Set(value string) error {
if value == "" {
// Unset.
logging.mu.Lock()
defer logging.mu.Unlock()
t.line = 0
t.file = ""
return nil
}
fields := strings.Split(value, ":")
if len(fields) != 2 {
return errTraceSyntax
}
file, line := fields[0], fields[1]
if !strings.Contains(file, ".") {
return errTraceSyntax
}
v, err := strconv.Atoi(line)
if err != nil {
return errTraceSyntax
}
if v <= 0 {
return errors.New("negative or zero value for level")
}
logging.mu.Lock()
defer logging.mu.Unlock()
t.line = v
t.file = file
return nil
}
// We don't include a capture group for the log message here, just for the
// preamble, because a capture group that handles multiline messages is very
// slow when running on the large buffers passed to EntryDecoder.split.
var entryRE = regexp.MustCompile(
`(?m)^([IWEF])(\d{6} \d{2}:\d{2}:\d{2}.\d{6}) (?:(\d+) )?([^:]+):(\d+)`)
// EntryDecoder reads successive encoded log entries from the input
// buffer. Each entry is preceded by a single big-ending uint32
// describing the next entry's length.
type EntryDecoder struct {
re *regexp.Regexp
scanner *bufio.Scanner
truncatedLastEntry bool
}
// NewEntryDecoder creates a new instance of EntryDecoder.
func NewEntryDecoder(in io.Reader) *EntryDecoder {
d := &EntryDecoder{scanner: bufio.NewScanner(in), re: entryRE.Copy()}
d.scanner.Split(d.split)
return d
}
// MessageTimeFormat is the format of the timestamp in log message headers as
// used in time.Parse and time.Format.
const MessageTimeFormat = "060102 15:04:05.999999"
// Decode decodes the next log entry into the provided protobuf message.
func (d *EntryDecoder) Decode(entry *Entry) error {
for {
if !d.scanner.Scan() {
if err := d.scanner.Err(); err != nil {
return err
}
return io.EOF
}
b := d.scanner.Bytes()
m := d.re.FindSubmatch(b)
if m == nil {
continue
}
entry.Severity = Severity(strings.IndexByte(severityChar, m[1][0]) + 1)
t, err := time.Parse(MessageTimeFormat, string(m[2]))
if err != nil {
return err
}
entry.Time = t.UnixNano()
if len(m[3]) > 0 {
goroutine, err := strconv.Atoi(string(m[3]))
if err != nil {
return err
}
entry.Goroutine = int64(goroutine)
}
entry.File = string(m[4])
line, err := strconv.Atoi(string(m[5]))
if err != nil {
return err
}
entry.Line = int64(line)
entry.Message = strings.TrimSpace(string(b[len(m[0]):]))
return nil
}
}
func (d *EntryDecoder) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if d.truncatedLastEntry {
i := d.re.FindIndex(data)
if i == nil {
// If there's no entry that starts in this chunk, advance past it, since
// we've truncated the entry it was originally part of.
return len(data), nil, nil
}
d.truncatedLastEntry = false
if i[0] > 0 {
// If an entry starts anywhere other than the first index, advance to it
// to maintain the invariant that entries start at the beginning of data.
// This isn't necessary, but simplifies the code below.
return i[0], nil, nil
}
// If i[0] == 0, then a new entry starts at the beginning of data, so fall
// through to the normal logic.
}
// From this point on, we assume we're currently positioned at a log entry.
// We want to find the next one so we start our search at data[1].
i := d.re.FindIndex(data[1:])
if i == nil {
if atEOF {
return len(data), data, nil
}
if len(data) >= bufio.MaxScanTokenSize {
// If there's no room left in the buffer, return the current truncated
// entry.
d.truncatedLastEntry = true
return len(data), data, nil
}
// If there is still room to read more, ask for more before deciding whether
// to truncate the entry.
return 0, nil, nil
}
// i[0] is the start of the next log entry, but we need to adjust the value
// to account for using data[1:] above.
i[0]++
return i[0], data[:i[0]], nil
}
// flushSyncWriter is the interface satisfied by logging destinations.
type flushSyncWriter interface {
Flush() error
Sync() error
io.Writer
}
// the --no-color flag.
var noColor bool
// formatHeader formats a log header using the provided file name and
// line number. Log lines are colorized depending on severity.
//
// Log lines have this form:
// Lyymmdd hh:mm:ss.uuuuuu goid file:line msg...
// where the fields are defined as follows:
// L A single character, representing the log level (eg 'I' for INFO)
// yy The year (zero padded; ie 2016 is '16')
// mm The month (zero padded; ie May is '05')
// dd The day (zero padded)
// hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds
// goid The goroutine id (omitted if zero for use by tests)
// file The file name
// line The line number
// msg The user-supplied message
func formatHeader(
s Severity, now time.Time, gid int, file string, line int, cp ttycolor.Profile,
) *buffer {
if noColor {
cp = nil
}
buf := logging.getBuffer()
if line < 0 {
line = 0 // not a real line number, but acceptable to someDigits
}
if s > Severity_FATAL || s <= Severity_UNKNOWN {
s = Severity_INFO // for safety.
}
tmp := buf.tmp[:len(buf.tmp)]
var n int
var prefix []byte
switch s {
case Severity_INFO:
prefix = cp[ttycolor.Cyan]
case Severity_WARNING:
prefix = cp[ttycolor.Yellow]
case Severity_ERROR, Severity_FATAL:
prefix = cp[ttycolor.Red]
}
n += copy(tmp, prefix)
// Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand.
// It's worth about 3X. Fprintf is hard.
year, month, day := now.Date()
hour, minute, second := now.Clock()
// Lyymmdd hh:mm:ss.uuuuuu file:line
tmp[n] = severityChar[s-1]
n++
if year < 2000 {
year = 2000
}
n += buf.twoDigits(n, year-2000)
n += buf.twoDigits(n, int(month))
n += buf.twoDigits(n, day)
n += copy(tmp[n:], cp[ttycolor.Gray]) // gray for time, file & line
tmp[n] = ' '
n++
n += buf.twoDigits(n, hour)
tmp[n] = ':'
n++
n += buf.twoDigits(n, minute)
tmp[n] = ':'
n++
n += buf.twoDigits(n, second)
tmp[n] = '.'
n++
n += buf.nDigits(6, n, now.Nanosecond()/1000, '0')
tmp[n] = ' '
n++
if gid > 0 {
n += buf.someDigits(n, gid)
tmp[n] = ' '
n++
}
buf.Write(tmp[:n])
buf.WriteString(file)
tmp[0] = ':'
n = buf.someDigits(1, line)
n++
// Extra space between the header and the actual message for scannability.
tmp[n] = ' '
n++
n += copy(tmp[n:], cp[ttycolor.Reset])
tmp[n] = ' '
n++
buf.Write(tmp[:n])
return buf
}
// Some custom tiny helper functions to print the log header efficiently.
const digits = "0123456789"
// twoDigits formats a zero-prefixed two-digit integer at buf.tmp[i].
// Returns two.
func (buf *buffer) twoDigits(i, d int) int {
buf.tmp[i+1] = digits[d%10]
d /= 10
buf.tmp[i] = digits[d%10]
return 2
}
// nDigits formats an n-digit integer at buf.tmp[i],
// padding with pad on the left.
// It assumes d >= 0. Returns n.
func (buf *buffer) nDigits(n, i, d int, pad byte) int {
j := n - 1
for ; j >= 0 && d > 0; j-- {
buf.tmp[i+j] = digits[d%10]
d /= 10
}
for ; j >= 0; j-- {
buf.tmp[i+j] = pad
}
return n
}
// someDigits formats a zero-prefixed variable-width integer at buf.tmp[i].
func (buf *buffer) someDigits(i, d int) int {
// Print into the top, then copy down. We know there's space for at least
// a 10-digit number.
j := len(buf.tmp)
for {
j--
buf.tmp[j] = digits[d%10]
d /= 10
if d == 0 {
break
}
}
return copy(buf.tmp[i:], buf.tmp[j:])
}
func formatLogEntry(entry Entry, stacks []byte, cp ttycolor.Profile) *buffer {
buf := formatHeader(entry.Severity, timeutil.Unix(0, entry.Time),
int(entry.Goroutine), entry.File, int(entry.Line), cp)
_, _ = buf.WriteString(entry.Message)
if buf.Bytes()[buf.Len()-1] != '\n' {
_ = buf.WriteByte('\n')
}
if len(stacks) > 0 {
buf.Write(stacks)
}
return buf
}
func init() {
// Default stderrThreshold and fileThreshold to log everything.
// This will be the default in tests unless overridden; the CLI
// commands set their default separately in cli/flags.go
logging.stderrThreshold = Severity_INFO
logging.fileThreshold = Severity_INFO
logging.pcsPool = sync.Pool{
New: func() interface{} {
return [1]uintptr{}
},
}
logging.prefix = program
logging.setVState(0, nil, false)
logging.gcNotify = make(chan struct{}, 1)
logging.fatalCh = make(chan struct{})
go flushDaemon()
go signalFlusher()
}
// signalFlusher flushes the log(s) every time SIGHUP is received.
func signalFlusher() {
ch := sysutil.RefreshSignaledChan()
for sig := range ch {
Infof(context.Background(), "%s received, flushing logs", sig)
Flush()
}
}
// LoggingToStderr returns true if log messages of the given severity
// are visible on stderr.
func LoggingToStderr(s Severity) bool {
return s >= logging.stderrThreshold.get()
}
// StartGCDaemon starts the log file GC -- this must be called after
// command-line parsing has completed so that no data is lost when the
// user configures larger max sizes than the defaults.
//
// The logger's GC daemon stops when the provided context is canceled.
func StartGCDaemon(ctx context.Context) {
go logging.gcDaemon(ctx)
}
// Flush flushes all pending log I/O.
func Flush() {
logging.lockAndFlushAll()
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for _, l := range secondaryLogRegistry.mu.loggers {
// Some loggers (e.g. the audit log) want to keep all the files.
l.logger.lockAndFlushAll()
}
}
// SetSync configures whether logging synchronizes all writes.
func SetSync(sync bool) {
logging.lockAndSetSync(sync)
func() {
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for _, l := range secondaryLogRegistry.mu.loggers {
if !sync && l.forceSyncWrites {
// We're not changing this.
continue
}
l.logger.lockAndSetSync(sync)
}
}()
if sync {
// There may be something in the buffers already; flush it.
Flush()
}
}
// loggingT collects all the global state of the logging setup.
type loggingT struct {
noStderrRedirect bool
// Directory prefix where to store this logger's files.
logDir DirName
// Name prefix for log files.
prefix string
// Level flag for output to stderr. Handled atomically.
stderrThreshold Severity
// Level flag for output to files.
fileThreshold Severity
// freeList is a list of byte buffers, maintained under freeListMu.
freeList *buffer
// freeListMu maintains the free list. It is separate from the main mutex
// so buffers can be grabbed and printed to without holding the main lock,
// for better parallelization.
freeListMu syncutil.Mutex
// mu protects the remaining elements of this structure and is
// used to synchronize logging.
mu syncutil.Mutex
// file holds the log file writer.
file flushSyncWriter
// syncWrites if true calls file.Flush and file.Sync on every log write.
syncWrites bool
// pcsPool maintains a set of [1]uintptr buffers to be used in V to avoid
// allocating every time we compute the caller's PC.
pcsPool sync.Pool
// vmap is a cache of the V Level for each V() call site, identified by PC.
// It is wiped whenever the vmodule flag changes state.
vmap map[uintptr]level
// filterLength stores the length of the vmodule filter chain. If greater
// than zero, it means vmodule is enabled. It may be read safely
// using sync.LoadInt32, but is only modified under mu.
filterLength int32
// traceLocation is the state of the -log_backtrace_at flag.
traceLocation traceLocation
// disableDaemons can be used to turn off both the GC and flush deamons.
disableDaemons bool
// These flags are modified only under lock, although verbosity may be fetched
// safely using atomic.LoadInt32.
vmodule moduleSpec // The state of the --vmodule flag.
verbosity level // V logging level, the value of the --verbosity flag/
exitOverride struct {
f func(int) // overrides os.Exit when non-nil; testing only
hideStack bool // hides stack trace; only in effect when f is not nil
}
gcNotify chan struct{} // notify GC daemon that a new log file was created
fatalCh chan struct{} // closed on fatal error
interceptor atomic.Value // InterceptorFn
// The Cluster ID is reported on every new log file so as to ease the correlation
// of panic reports with self-reported log files.
clusterID string
}
// buffer holds a byte Buffer for reuse. The zero value is ready for use.
type buffer struct {
bytes.Buffer
tmp [64]byte // temporary byte array for creating headers.
next *buffer
}
var logging loggingT
// SetClusterID stores the Cluster ID for further reference.
func SetClusterID(clusterID string) {
// Ensure that the clusterID is logged with the same format as for
// new log files, even on the first log file. This ensures that grep
// will always find it.
file, line, _ := caller.Lookup(1)
logging.outputLogEntry(Severity_INFO, file, line,
fmt.Sprintf("[config] clusterID: %s", clusterID))
// Perform the change proper.
logging.mu.Lock()
defer logging.mu.Unlock()
if logging.clusterID != "" {
panic("clusterID already set")
}
logging.clusterID = clusterID
}
// setVState sets a consistent state for V logging.
// l.mu is held.
func (l *loggingT) setVState(verbosity level, filter []modulePat, setFilter bool) {
// Turn verbosity off so V will not fire while we are in transition.
logging.verbosity.set(0)
// Ditto for filter length.
atomic.StoreInt32(&logging.filterLength, 0)
// Set the new filters and wipe the pc->Level map if the filter has changed.
if setFilter {
logging.vmodule.filter = filter
logging.vmap = make(map[uintptr]level)
}
// Things are consistent now, so enable filtering and verbosity.
// They are enabled in order opposite to that in V.
atomic.StoreInt32(&logging.filterLength, int32(len(filter)))
logging.verbosity.set(verbosity)
}
// getBuffer returns a new, ready-to-use buffer.
func (l *loggingT) getBuffer() *buffer {
l.freeListMu.Lock()
b := l.freeList
if b != nil {
l.freeList = b.next
}
l.freeListMu.Unlock()
if b == nil {
b = new(buffer)
} else {
b.next = nil
b.Reset()
}
return b
}
// putBuffer returns a buffer to the free list.
func (l *loggingT) putBuffer(b *buffer) {
if b.Len() >= 256 {
// Let big buffers die a natural death.
return
}
l.freeListMu.Lock()
b.next = l.freeList
l.freeList = b
l.freeListMu.Unlock()
}
// ensureFile ensures that l.file is set and valid.
func (l *loggingT) ensureFile() error {
if l.file == nil {
return l.createFile()
}
return nil
}
// writeToFile writes to the file and applies the synchronization policy.
func (l *loggingT) writeToFile(data []byte) error {
if _, err := l.file.Write(data); err != nil {
return err
}
if l.syncWrites {
_ = l.file.Flush()
_ = l.file.Sync()
}
return nil
}
// outputLogEntry marshals a log entry proto into bytes, and writes
// the data to the log files. If a trace location is set, stack traces
// are added to the entry before marshaling.
func (l *loggingT) outputLogEntry(s Severity, file string, line int, msg string) {
// Set additional details in log entry.
now := timeutil.Now()
entry := MakeEntry(s, now.UnixNano(), file, line, msg)
if f, ok := l.interceptor.Load().(InterceptorFn); ok && f != nil {
f(entry)
return
}
// TODO(tschottdorf): this is a pretty horrible critical section.
l.mu.Lock()
var stacks []byte
var fatalTrigger chan struct{}
if s == Severity_FATAL {
// Close l.fatalCh if it is not already closed (note that we're
// holding l.mu to guard against concurrent closes).
select {
case <-l.fatalCh:
default:
close(l.fatalCh)
}
switch traceback {
case tracebackSingle:
stacks = getStacks(false)
case tracebackAll:
stacks = getStacks(true)
}
stacks = append(stacks, []byte(fatalErrorPostamble)...)
logExitFunc = func(error) {} // If we get a write error, we'll still exit.
// We don't want to hang forever writing our final log message. If
// things are broken (for example, if the disk fills up and there
// are cascading errors and our process manager has stopped
// reading from its side of a stderr pipe), it's more important to
// let the process exit than limp along.
//
// Note that we do not use os.File.SetWriteDeadline because not
// all files support this (for example, plain files on a network
// file system do not support deadlines but can block
// indefinitely).
//
// https://github.com/cockroachdb/cockroach/issues/23119
fatalTrigger = make(chan struct{})
exitFunc := os.Exit
if l.exitOverride.f != nil {
if l.exitOverride.hideStack {
stacks = []byte("stack trace omitted via SetExitFunc)\n")
}
exitFunc = l.exitOverride.f
}
exitCalled := make(chan struct{})
// This defer prevents outputLogEntry() from returning until the
// exit function has been called.
defer func() {
<-exitCalled
}()
go func() {
select {
case <-time.After(10 * time.Second):
case <-fatalTrigger:
}
exitFunc(255) // C++ uses -1, which is silly because it's anded with 255 anyway.
close(exitCalled)
}()
} else if l.traceLocation.isSet() {
if l.traceLocation.match(file, line) {
stacks = getStacks(false)
}
}
if s >= l.stderrThreshold.get() || (s == Severity_FATAL && stderrRedirected) {
// We force-copy FATAL messages to stderr, because the process is bound
// to terminate and the user will want to know why.
l.outputToStderr(entry, stacks)
}
if l.logDir.IsSet() && s >= l.fileThreshold.get() {
if err := l.ensureFile(); err != nil {
// Make sure the message appears somewhere.
l.outputToStderr(entry, stacks)
l.exitLocked(err)
l.mu.Unlock()
return
}
buf := l.processForFile(entry, stacks)
data := buf.Bytes()
if err := l.writeToFile(data); err != nil {
l.exitLocked(err)
l.mu.Unlock()
return
}
l.putBuffer(buf)
}
// Flush and exit on fatal logging.
if s == Severity_FATAL {
l.flushAndSync(true /*doSync*/)
close(fatalTrigger)
// Note: although it seems like the function is allowed to return
// below when s == Severity_FATAL, this is not so, because the
// anonymous function func() { <-exitCalled } is deferred
// above. That function ensures that outputLogEntry() will wait
// until the exit function has been called. If the exit function
// is os.Exit, it will never return, outputLogEntry()'s defer will
// never complete and all is well. If the exit function was
// overridden, then the client that has overridden the exit
// function is expecting log.Fatal to return and all is well too.
}
l.mu.Unlock()
}
// printPanicToFile copies the panic details to the log file. This is
// useful when the standard error is not redirected to the log file
// (!stderrRedirected), as the go runtime will only print panics to
// stderr.
func (l *loggingT) printPanicToFile(r interface{}) {
if !l.logDir.IsSet() {
// There's no log file. Nothing to do.
return
}
l.mu.Lock()
defer l.mu.Unlock()
if err := l.ensureFile(); err != nil {
fmt.Fprintf(OrigStderr, "log: %v", err)
return
}
panicBytes := []byte(fmt.Sprintf("%v\n\n%s\n", r, debug.Stack()))
if err := l.writeToFile(panicBytes); err != nil {
fmt.Fprintf(OrigStderr, "log: %v", err)
return
}
}
func (l *loggingT) outputToStderr(entry Entry, stacks []byte) {
buf := l.processForStderr(entry, stacks)
if _, err := OrigStderr.Write(buf.Bytes()); err != nil {
l.exitLocked(err)
}
l.putBuffer(buf)
}
// processForStderr formats a log entry for output to standard error.