-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathstderr_redirect.go
167 lines (148 loc) · 5.23 KB
/
stderr_redirect.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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package log
import (
"fmt"
"os"
"runtime/debug"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
)
// OrigStderr points to the original stderr stream when the process
// started.
// Note that it is generally not sound to restore os.Stderr
// and fd 2 from here as long as a stack of loggers (especially test
// log scopes) are active, as the loggers keep track of what they are
// redirecting themselves in a stack structure.
var OrigStderr = func() *os.File {
fd, err := dupFD(os.Stderr.Fd())
if err != nil {
panic(err)
}
return os.NewFile(fd, os.Stderr.Name())
}()
// LoggingToStderr returns true if log messages of the given severity
// sent to the main logger are also visible on stderr. This is used
// e.g. by the startup code to announce server details both on the
// external stderr and to the log file.
//
// This is also the logic used by Shout calls.
func LoggingToStderr(s Severity) bool {
return s >= mainLog.stderrThreshold.get()
}
// hijackStderr replaces stderr with the given file descriptor.
//
// A client that wishes to use the original stderr (the process'
// external stderr stream) must use OrigStderr defined above.
func hijackStderr(f *os.File) error {
return redirectStderr(f)
}
// osStderrMu ensures that concurrent redirects of stderr don't
// overwrite each other.
var osStderrMu syncutil.Mutex
// takeOverInternalStderr tells the given logger that it is to take over
// direct writes to fd 2 by e.g. the Go runtime, or direct writes to
// os.Stderr.
//
// This also enforces that at most one logger can redirect stderr in
// this way. It also returns an error if stderr has already been
// taken over in this way. It also errors if the target logger has no
// valid output directory and no output file has been created (or
// could be created).
func (l *loggerT) takeOverInternalStderr() error {
takeOverStderrMu.Lock()
defer takeOverStderrMu.Unlock()
if anyLoggerHasInternalStderrOwnership() {
return errors.AssertionFailedf(
"can't take over stderr; first takeover:\n%s",
takeOverStderrMu.previousStderrTakeover)
}
l.mu.Lock()
defer l.mu.Unlock()
// Ensure there's a file to work with.
if err := l.ensureFileLocked(); err != nil {
return errors.NewAssertionErrorWithWrappedErrf(err, "can't take over stderr without a file")
}
// Ensure there's a _real_ file to work with.
sb, ok := l.mu.file.(*syncBuffer)
if !ok {
return errors.AssertionFailedf("can't take over stderr with a non-file writer")
}
// Take over stderr with this writer.
if err := hijackStderr(sb.file); err != nil {
return errors.Wrap(err, "unable to take over stderr")
}
// Mark the stderr as taken over.
l.mu.currentlyOwnsInternalStderr = true
// Request internal stderr redirection for future file rotations.
l.mu.redirectInternalStderrWrites = true
// Success: remember who called us, in case the next
// caller comes along with the wrong call sequence.
takeOverStderrMu.previousStderrTakeover = string(debug.Stack())
return nil
}
// relinquishInternalStderr relinquishes a takeover by
// takeOverInternalStderr(). It returns an error if the
// logger did not take over internal stderr writes already.
func (l *loggerT) relinquishInternalStderr() error {
l.mu.Lock()
defer l.mu.Unlock()
if !l.mu.redirectInternalStderrWrites {
const basemsg = "can't relinquish stderr writes - this logger is not owner%s"
// Try to help the caller a bit.
takeOverStderrMu.Lock()
defer takeOverStderrMu.Unlock()
var extra string
if anyLoggerHasInternalStderrOwnership() {
extra = fmt.Sprintf("; previous take over:\n%s", takeOverStderrMu.previousStderrTakeover)
}
return errors.AssertionFailedf(basemsg, extra)
}
// If stderr actually redirected, restore it.
if l.mu.currentlyOwnsInternalStderr {
if err := hijackStderr(OrigStderr); err != nil {
return errors.Wrap(err, "unable to restore internal stderr")
}
}
// Remove the ownership.
l.mu.currentlyOwnsInternalStderr = false
l.mu.redirectInternalStderrWrites = false
return nil
}
// anyLoggerHasInternalStderrOwnership returns true iff any of the
// loggers currently has redirectInternalStderrWrites set.
//
// Used by takeOverInternalStderr() to enforce its invariant.
func anyLoggerHasInternalStderrOwnership() bool {
mainLog.mu.Lock()
mainLogHasOwnership := mainLog.mu.redirectInternalStderrWrites
mainLog.mu.Unlock()
if mainLogHasOwnership {
return true
}
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for _, secL := range secondaryLogRegistry.mu.loggers {
secL.logger.mu.Lock()
hasOwnership := secL.logger.mu.redirectInternalStderrWrites
secL.logger.mu.Unlock()
if hasOwnership {
return true
}
}
return false
}
var takeOverStderrMu struct {
syncutil.Mutex
// previousStderrTakeover is the stack trace of the previous call to
// takeOverStderrInternal(). This can be used to troubleshoot
// invalid call sequences.
previousStderrTakeover string
}