1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package testlog provides a back-channel communication path
6 // between tests and package os, so that cmd/go can see which
7 // environment variables and files a test consults.
12 // Interface is the interface required of test loggers.
13 // The os package will invoke the interface's methods to indicate that
14 // it is inspecting the given environment variables or files.
15 // Multiple goroutines may call these methods simultaneously.
16 type Interface
interface {
23 // logger is the current logger Interface.
24 // We use an atomic.Value in case test startup
25 // is racing with goroutines started during init.
26 // That must not cause a race detector failure,
27 // although it will still result in limited visibility
28 // into exactly what those goroutines do.
29 var logger atomic
.Value
31 // SetLogger sets the test logger implementation for the current process.
32 // It must be called only once, at process startup.
33 func SetLogger(impl Interface
) {
34 if logger
.Load() != nil {
35 panic("testlog: SetLogger must be called only once")
40 // Logger returns the current test logger implementation.
41 // It returns nil if there is no logger.
42 func Logger() Interface
{
47 return *impl
.(*Interface
)
50 // Getenv calls Logger().Getenv, if a logger has been set.
51 func Getenv(name
string) {
52 if log
:= Logger(); log
!= nil {
57 // Open calls Logger().Open, if a logger has been set.
58 func Open(name
string) {
59 if log
:= Logger(); log
!= nil {
64 // Stat calls Logger().Stat, if a logger has been set.
65 func Stat(name
string) {
66 if log
:= Logger(); log
!= nil {