2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 22:50:48 +00:00

debug: log timing

This commit is contained in:
Alexander Neumann 2015-10-26 20:10:03 +01:00
parent 00e05ae3c9
commit ae46674cd3

View File

@ -12,11 +12,18 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
type process struct {
tag string
goroutine int
}
var opts struct { var opts struct {
logger *log.Logger logger *log.Logger
tags map[string]bool tags map[string]bool
last map[process]time.Time
m sync.Mutex m sync.Mutex
} }
@ -65,6 +72,7 @@ func initDebugLogger() {
func initDebugTags() { func initDebugTags() {
opts.tags = make(map[string]bool) opts.tags = make(map[string]bool)
opts.last = make(map[process]time.Time)
// defaults // defaults
opts.tags["break"] = true opts.tags["break"] = true
@ -113,14 +121,12 @@ func goroutineNum() int {
} }
// taken from https://github.com/VividCortex/trace // taken from https://github.com/VividCortex/trace
func getPosition() string { func getPosition(goroutine int) string {
_, file, line, ok := runtime.Caller(2) _, file, line, ok := runtime.Caller(2)
if !ok { if !ok {
return "" return ""
} }
goroutine := goroutineNum()
return fmt.Sprintf("%3d %s:%d", goroutine, filepath.Base(file), line) return fmt.Sprintf("%3d %s:%d", goroutine, filepath.Base(file), line)
} }
@ -130,6 +136,15 @@ func Log(tag string, f string, args ...interface{}) {
opts.m.Lock() opts.m.Lock()
defer opts.m.Unlock() defer opts.m.Unlock()
goroutine := goroutineNum()
last, ok := opts.last[process{tag, goroutine}]
if !ok {
last = time.Now()
}
current := time.Now()
opts.last[process{tag, goroutine}] = current
if f[len(f)-1] != '\n' { if f[len(f)-1] != '\n' {
f += "\n" f += "\n"
} }
@ -138,8 +153,8 @@ func Log(tag string, f string, args ...interface{}) {
maxTagLen = len(tag) maxTagLen = len(tag)
} }
formatStringTag := "[%" + strconv.FormatInt(int64(maxTagLen), 10) + "s]" formatStringTag := "%2.3f [%" + strconv.FormatInt(int64(maxTagLen), 10) + "s]"
formatString := fmt.Sprintf(formatStringTag+" %s %s", tag, getPosition(), f) formatString := fmt.Sprintf(formatStringTag+" %s %s", current.Sub(last).Seconds(), tag, getPosition(goroutine), f)
dbgprint := func() { dbgprint := func() {
fmt.Fprintf(os.Stderr, formatString, args...) fmt.Fprintf(os.Stderr, formatString, args...)