From 7463e54258c1b8a0ee6b8557081d2e00ef81b826 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Sat, 29 Dec 2018 17:23:08 +0300 Subject: [PATCH] Implement a log wrapper --- log/log.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 log/log.go diff --git a/log/log.go b/log/log.go new file mode 100644 index 00000000..db6132e7 --- /dev/null +++ b/log/log.go @@ -0,0 +1,35 @@ +// Wrapper for standard library log, with the only difference is that it has extra function Tracef() and optional verbose flag to enable output from that. +package log + +import ( + "fmt" + "log" + "os" + "path" + "runtime" + "strings" +) + +var Verbose = false + +// Printf calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Printf. +func Printf(format string, v ...interface{}) { + log.Printf(format, v...) +} + +func Tracef(format string, v ...interface{}) { + if Verbose { + pc := make([]uintptr, 10) // at least 1 entry needed + runtime.Callers(2, pc) + f := runtime.FuncForPC(pc[0]) + var buf strings.Builder + buf.WriteString(fmt.Sprintf("%s(): ", path.Base(f.Name()))) + text := fmt.Sprintf(format, v...) + buf.WriteString(text) + if len(text) == 0 || text[len(text)-1] != '\n' { + buf.WriteRune('\n') + } + fmt.Fprint(os.Stderr, buf.String()) + } +}