2020-07-18 05:28:12 +00:00
|
|
|
package dbg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-11-17 01:19:46 +00:00
|
|
|
"strings"
|
2020-07-18 05:28:12 +00:00
|
|
|
)
|
|
|
|
|
2020-11-17 01:19:46 +00:00
|
|
|
// Verbose is a flag for whether or not verbose logging should be activated
|
|
|
|
var Verbose bool
|
|
|
|
|
|
|
|
// Debug does not truncate byte strings
|
2020-07-18 05:28:12 +00:00
|
|
|
var Debug bool
|
|
|
|
|
2020-11-17 01:19:46 +00:00
|
|
|
// VerboseVerbose does not truncate strings
|
|
|
|
var VerboseVerbose bool
|
|
|
|
|
|
|
|
// OutFile is the output path for StdOut
|
|
|
|
var OutFile *os.File
|
|
|
|
|
|
|
|
// ErrFile is the output path for StdErr
|
|
|
|
var ErrFile *os.File
|
|
|
|
|
|
|
|
type out int
|
|
|
|
|
|
|
|
const (
|
|
|
|
stdout out = iota
|
|
|
|
stderr
|
|
|
|
)
|
|
|
|
|
|
|
|
type output struct {
|
|
|
|
out out
|
|
|
|
msg string
|
|
|
|
other []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var log chan output
|
2020-07-18 05:28:12 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-11-17 01:19:46 +00:00
|
|
|
log = make(chan output)
|
|
|
|
|
2020-07-18 05:28:12 +00:00
|
|
|
Init()
|
2020-11-17 01:19:46 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
o := <-log
|
|
|
|
// because OutFile and ErrFile may be the same
|
|
|
|
msg := strings.TrimSuffix(o.msg, "\n") + "\n"
|
|
|
|
b := []byte(fmt.Sprintf(msg, o.other...))
|
|
|
|
if stdout == o.out {
|
|
|
|
OutFile.Write(b)
|
|
|
|
} else {
|
|
|
|
ErrFile.Write(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf will print to log.Printf
|
|
|
|
func Printf(tpl string, other ...interface{}) {
|
|
|
|
log <- output{
|
|
|
|
out: stdout,
|
|
|
|
msg: tpl,
|
|
|
|
other: other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf will print to fmt.Fprintf(stderr)
|
|
|
|
func Warnf(tpl string, other ...interface{}) {
|
|
|
|
log <- output{
|
|
|
|
out: stderr,
|
|
|
|
msg: "[warn] " + tpl,
|
|
|
|
other: other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf will print to fmt.Fprintf(std)
|
|
|
|
func Debugf(tpl string, other ...interface{}) {
|
|
|
|
log <- output{
|
|
|
|
out: stderr,
|
|
|
|
msg: "[debug] " + tpl,
|
|
|
|
other: other,
|
|
|
|
}
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init will set debug vars from ENVs and print out whatever is set
|
|
|
|
func Init() {
|
2020-11-17 01:19:46 +00:00
|
|
|
if nil == OutFile {
|
|
|
|
OutFile = os.Stdout
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
2020-11-17 01:19:46 +00:00
|
|
|
if nil == ErrFile {
|
|
|
|
ErrFile = os.Stderr
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 01:19:46 +00:00
|
|
|
if !Verbose {
|
|
|
|
if "true" == os.Getenv("VERBOSE") {
|
|
|
|
Printf("VERBOSE=true")
|
|
|
|
Verbose = true
|
|
|
|
}
|
|
|
|
if Verbose {
|
|
|
|
fmt.Fprintf(os.Stderr, "VERBOSE: extra logging enabled\n")
|
|
|
|
}
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
2020-11-17 01:19:46 +00:00
|
|
|
|
|
|
|
if !VerboseVerbose {
|
|
|
|
if "true" == strings.ToLower(os.Getenv("VERBOSE_VERBOSE")) {
|
|
|
|
Printf("VERBOSE_VERBOSE=true")
|
|
|
|
VerboseVerbose = true
|
|
|
|
} else if "true" == strings.ToLower(os.Getenv("VERBOSE_RAW")) {
|
|
|
|
Printf("VERBOSE_RAW=true # Deprecated: Use VERBOSE_VERBOSE=true")
|
|
|
|
VerboseVerbose = true
|
|
|
|
}
|
|
|
|
if VerboseVerbose {
|
|
|
|
fmt.Fprintf(os.Stderr, "VERBOSE_VERBOSE: output will NOT be truncated\n")
|
|
|
|
}
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
2020-11-17 01:19:46 +00:00
|
|
|
|
|
|
|
if !Debug {
|
|
|
|
if "true" == strings.ToLower(os.Getenv("DEBUG")) {
|
|
|
|
Printf("DEBUG=true")
|
|
|
|
Debug = true
|
|
|
|
} else if "true" == strings.ToLower(os.Getenv("VERBOSE_BYTES")) {
|
|
|
|
Printf("VERBOSE_BYTES=true # Deprecated: Use DEBUG=true")
|
|
|
|
Debug = true
|
|
|
|
}
|
|
|
|
if Debug {
|
|
|
|
fmt.Fprintf(os.Stderr, "DEBUG: byte output will be printed as hex\n")
|
|
|
|
}
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trunc will take up to the first and last 20 bytes of the input to product 80 char hex output
|
|
|
|
func Trunc(b []byte, n int) string {
|
|
|
|
bin := b[:n]
|
2020-11-17 01:19:46 +00:00
|
|
|
if Debug || VerboseVerbose {
|
|
|
|
if Debug {
|
|
|
|
return hex.EncodeToString(bin)
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
2020-11-17 01:19:46 +00:00
|
|
|
return string(bin)
|
2020-07-18 05:28:12 +00:00
|
|
|
}
|
|
|
|
if n > 40 {
|
|
|
|
return hex.EncodeToString(bin[:19]) + ".." + hex.EncodeToString(bin[n-19:])
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bin)
|
|
|
|
}
|