1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 08:16:36 +00:00

debug: Add location to log, improve formatting

Before:

    2015/05/02 00:38:46 [restic] main []string{"./restic", "backup", "."}

After:

    2015/05/02 00:47:42 [  restic]   1 main.go:176    main []string{"./restic", "backup", "-f", "."}
This commit is contained in:
Alexander Neumann 2015-05-02 00:48:26 +02:00
parent ccdbc80d5b
commit 959da0a76a

View file

@ -7,6 +7,7 @@ import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
@ -122,6 +123,28 @@ func initDebugBreaks() {
fmt.Fprintf(os.Stderr, "debug breaks enabled for: %v\n", breaks)
}
// taken from https://github.com/VividCortex/trace
func goroutineNum() int {
b := make([]byte, 20)
runtime.Stack(b, false)
var num int
fmt.Sscanf(string(b), "goroutine %d ", &num)
return num
}
// taken from https://github.com/VividCortex/trace
func getPosition() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return ""
}
goroutine := goroutineNum()
return fmt.Sprintf("%3d %s:%3d", goroutine, filepath.Base(file), line)
}
func Log(tag string, f string, args ...interface{}) {
opts.m.Lock()
defer opts.m.Unlock()
@ -130,12 +153,14 @@ func Log(tag string, f string, args ...interface{}) {
f += "\n"
}
formatString := fmt.Sprintf("[% 25s] %-20s %s", tag, getPosition(), f)
dbgprint := func() {
fmt.Fprintf(os.Stderr, "DEBUG["+tag+"]: "+f, args...)
fmt.Fprintf(os.Stderr, formatString, args...)
}
if opts.logger != nil {
opts.logger.Printf("["+tag+"] "+f, args...)
opts.logger.Printf(formatString, args...)
}
// check if tag is enabled directly