github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/log/stderr_redirect.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package log 12 13 import "os" 14 15 // OrigStderr points to the original stderr stream. 16 var OrigStderr = func() *os.File { 17 fd, err := dupFD(os.Stderr.Fd()) 18 if err != nil { 19 panic(err) 20 } 21 22 return os.NewFile(fd, os.Stderr.Name()) 23 }() 24 25 // LoggingToStderr returns true if log messages of the given severity 26 // sent to the main logger are also visible on stderr. 27 func LoggingToStderr(s Severity) bool { 28 return s >= logging.stderrThreshold.get() 29 } 30 31 // stderrRedirected returns true if and only if logging to this logger 32 // captures stderr output to the log file. This is used e.g. by 33 // Shout() to determine whether to report to standard error in 34 // addition to logs. 35 func (l *loggerT) stderrRedirected() bool { 36 return logging.stderrThreshold > Severity_INFO && !l.noStderrRedirect 37 } 38 39 // hijackStderr replaces stderr with the given file descriptor. 40 // 41 // A client that wishes to use the original stderr must use 42 // OrigStderr defined above. 43 func hijackStderr(f *os.File) error { 44 return redirectStderr(f) 45 } 46 47 // restoreStderr cancels the effect of hijackStderr(). 48 func restoreStderr() error { 49 return redirectStderr(OrigStderr) 50 }