Source File
log.go
Belonging Package
internal/testlog
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package testlog provides a back-channel communication path
// between tests and package os, so that cmd/go can see which
// environment variables and files a test consults.
package testlog
import
// Interface is the interface required of test loggers.
// The os package will invoke the interface's methods to indicate that
// it is inspecting the given environment variables or files.
// Multiple goroutines may call these methods simultaneously.
type Interface interface {
Getenv(key string)
Stat(file string)
Open(file string)
Chdir(dir string)
}
// logger is the current logger Interface.
// We use an atomic.Pointer in case test startup
// is racing with goroutines started during init.
// That must not cause a race detector failure,
// although it will still result in limited visibility
// into exactly what those goroutines do.
var logger atomic.Pointer[Interface]
// SetLogger sets the test logger implementation for the current process.
// It must be called only once, at process startup.
func ( Interface) {
if !logger.CompareAndSwap(nil, &) {
panic("testlog: SetLogger must be called only once")
}
}
// Logger returns the current test logger implementation.
// It returns nil if there is no logger.
func () Interface {
:= logger.Load()
if == nil {
return nil
}
return *
}
// Getenv calls Logger().Getenv, if a logger has been set.
func ( string) {
if := Logger(); != nil {
.Getenv()
}
}
// Open calls Logger().Open, if a logger has been set.
func ( string) {
if := Logger(); != nil {
.Open()
}
}
// Stat calls Logger().Stat, if a logger has been set.
func ( string) {
if := Logger(); != nil {
.Stat()
}
}
The pages are generated with Golds v0.7.3. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |