goutils/dbg/dbg.go

77 lines
1.5 KiB
Go
Raw Permalink Normal View History

2018-12-18 02:12:51 +00:00
// Package dbg implements a debug printer.
package dbg
import (
"fmt"
"io"
"os"
)
// A DebugPrinter is a drop-in replacement for fmt.Print*, and also acts as
// an io.WriteCloser when enabled.
type DebugPrinter struct {
// If Enabled is false, the print statements won't do anything.
Enabled bool
out io.WriteCloser
}
// Close satisfies the Closer interface.
func (dbg *DebugPrinter) Close() error {
return dbg.out.Close()
}
// Write satisfies the Writer interface.
func (dbg *DebugPrinter) Write(p []byte) (int, error) {
if dbg.Enabled {
return dbg.out.Write(p)
}
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)
if err != nil {
return nil, err
}
return &DebugPrinter{
out: file,
}, nil
}
// To sets up a new DebugPrint to an io.WriteCloser.
func To(w io.WriteCloser) *DebugPrinter {
return &DebugPrinter{
out: w,
}
}
// Print calls fmt.Print if Enabled is true.
2023-05-05 06:21:07 +00:00
func (dbg *DebugPrinter) Print(v ...interface{}) {
2018-12-18 02:12:51 +00:00
if dbg.Enabled {
fmt.Fprint(dbg.out, v...)
}
}
// Println calls fmt.Println if Enabled is true.
2023-05-05 06:21:07 +00:00
func (dbg *DebugPrinter) Println(v ...interface{}) {
2018-12-18 02:12:51 +00:00
if dbg.Enabled {
fmt.Fprintln(dbg.out, v...)
}
}
// Printf calls fmt.Printf if Enabled is true.
2023-05-05 06:21:07 +00:00
func (dbg *DebugPrinter) Printf(format string, v ...interface{}) {
2018-12-18 02:12:51 +00:00
if dbg.Enabled {
fmt.Fprintf(dbg.out, format, v...)
}
}