Add tooling to enable strict TLS.
This commit is contained in:
52
dbg/dbg.go
52
dbg/dbg.go
@@ -1,12 +1,34 @@
|
||||
// Package dbg implements a debug printer.
|
||||
// Package dbg implements a simple debug printer.
|
||||
//
|
||||
// There are two main ways to use it:
|
||||
// - By using one of the constructors and calling flag.BoolVar(&debug.Enabled...)
|
||||
// - By setting the environment variable GOUTILS_ENABLE_DEBUG to true or false and
|
||||
// calling NewFromEnv().
|
||||
//
|
||||
// If enabled, any of the print statements will be written to stdout. Otherwise,
|
||||
// nothing will be emitted.
|
||||
package dbg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DebugEnvKey = "GOUTILS_ENABLE_DEBUG"
|
||||
|
||||
var enabledValues = map[string]bool{
|
||||
"1": true,
|
||||
"true": true,
|
||||
"yes": true,
|
||||
"on": true,
|
||||
"y": true,
|
||||
"enable": true,
|
||||
"enabled": true,
|
||||
}
|
||||
|
||||
// A DebugPrinter is a drop-in replacement for fmt.Print*, and also acts as
|
||||
// an io.WriteCloser when enabled.
|
||||
type DebugPrinter struct {
|
||||
@@ -15,6 +37,23 @@ type DebugPrinter struct {
|
||||
out io.WriteCloser
|
||||
}
|
||||
|
||||
// New returns a new DebugPrinter on os.Stdout.
|
||||
func New() *DebugPrinter {
|
||||
return &DebugPrinter{
|
||||
out: os.Stderr,
|
||||
}
|
||||
}
|
||||
|
||||
// NewFromEnv returns a new DebugPrinter based on the value of the environment
|
||||
// variable GOUTILS_ENABLE_DEBUG.
|
||||
func NewFromEnv() *DebugPrinter {
|
||||
enabled := strings.ToLower(os.Getenv(DebugEnvKey))
|
||||
return &DebugPrinter{
|
||||
out: os.Stderr,
|
||||
Enabled: enabledValues[enabled],
|
||||
}
|
||||
}
|
||||
|
||||
// Close satisfies the Closer interface.
|
||||
func (dbg *DebugPrinter) Close() error {
|
||||
return dbg.out.Close()
|
||||
@@ -28,13 +67,6 @@ func (dbg *DebugPrinter) Write(p []byte) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// New returns a new DebugPrinter on os.Stdout.
|
||||
func New() *DebugPrinter {
|
||||
return &DebugPrinter{
|
||||
out: os.Stdout,
|
||||
}
|
||||
}
|
||||
|
||||
// ToFile sets up a new DebugPrinter to a file, truncating it if it exists.
|
||||
func ToFile(path string) (*DebugPrinter, error) {
|
||||
file, err := os.Create(path)
|
||||
@@ -74,3 +106,7 @@ func (dbg *DebugPrinter) Printf(format string, v ...any) {
|
||||
fmt.Fprintf(dbg.out, format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
func (dbg *DebugPrinter) StackTrace() {
|
||||
dbg.Write(debug.Stack())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user