Add trace() for debugging

This commit is contained in:
Eugene Bujak 2018-09-25 18:34:34 +03:00
parent 620212ad37
commit 119d38fa8e
1 changed files with 15 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"path"
@ -246,3 +247,17 @@ func _Func() string {
f := runtime.FuncForPC(pc[0])
return path.Base(f.Name())
}
func trace(format string, args ...interface{}) {
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, args...)
buf.WriteString(text)
if len(text) == 0 || text[len(text)-1] != '\n' {
buf.WriteRune('\n')
}
fmt.Print(buf.String())
}