Remove timing, simplify function matching

This commit is contained in:
Alexander Neumann 2016-09-28 20:10:40 +02:00
parent 4eddcb344e
commit feed54caef

View file

@ -10,25 +10,15 @@ import (
"path/filepath" "path/filepath"
"restic/fs" "restic/fs"
"runtime" "runtime"
"strconv"
"strings" "strings"
"sync"
"time"
"restic/errors" "restic/errors"
) )
type process struct {
fn string
goroutine int
}
var opts struct { var opts struct {
logger *log.Logger logger *log.Logger
funcs map[string]bool funcs map[string]bool
files map[string]bool files map[string]bool
last map[process]time.Time
m sync.Mutex
} }
// make sure that all the initialization happens before the init() functions // make sure that all the initialization happens before the init() functions
@ -111,10 +101,6 @@ func padFunc(s string) string {
return s return s
} }
if !strings.Contains(s, "/") {
s = "*/" + s
}
return s return s
} }
@ -135,8 +121,6 @@ func padFile(s string) string {
} }
func initDebugTags() { func initDebugTags() {
opts.last = make(map[process]time.Time)
opts.funcs = parseFilter("DEBUG_FUNCS", padFunc) opts.funcs = parseFilter("DEBUG_FUNCS", padFunc)
opts.files = parseFilter("DEBUG_FILES", padFile) opts.files = parseFilter("DEBUG_FILES", padFile)
} }
@ -162,7 +146,7 @@ func getPosition() (fn, dir, file string, line int) {
Func := runtime.FuncForPC(pc) Func := runtime.FuncForPC(pc)
return Func.Name(), dirname, filename, line return path.Base(Func.Name()), dirname, filename, line
} }
func checkFilter(filter map[string]bool, key string) bool { func checkFilter(filter map[string]bool, key string) bool {
@ -186,34 +170,18 @@ func checkFilter(filter map[string]bool, key string) bool {
return false return false
} }
var maxPosLen = 10
// Log prints a message to the debug log (if debug is enabled). // Log prints a message to the debug log (if debug is enabled).
func Log(f string, args ...interface{}) { func Log(f string, args ...interface{}) {
opts.m.Lock()
defer opts.m.Unlock()
fn, dir, file, line := getPosition() fn, dir, file, line := getPosition()
goroutine := goroutineNum() goroutine := goroutineNum()
last, ok := opts.last[process{fn, goroutine}]
if !ok {
last = time.Now()
}
current := time.Now()
opts.last[process{fn, goroutine}] = current
if len(f) == 0 || f[len(f)-1] != '\n' { if len(f) == 0 || f[len(f)-1] != '\n' {
f += "\n" f += "\n"
} }
pos := fmt.Sprintf("%s/%s:%d", dir, file, line) pos := fmt.Sprintf("%s/%s:%d", dir, file, line)
if len(pos) > maxPosLen {
maxPosLen = len(pos)
}
formatStringTag := "%2.3f [%" + strconv.FormatInt(int64(maxPosLen), 10) + "s]" formatString := fmt.Sprintf("%s\t%s\t%d\t%s", pos, fn, goroutine, f)
formatString := fmt.Sprintf(formatStringTag+" %s", current.Sub(last).Seconds(), pos, f)
dbgprint := func() { dbgprint := func() {
fmt.Fprintf(os.Stderr, formatString, args...) fmt.Fprintf(os.Stderr, formatString, args...)