2
2
mirror of https://github.com/octoleo/restic.git synced 2025-01-11 10:18:10 +00:00

Merge branch 'debug_refactor'

This commit is contained in:
Alexander Neumann 2015-05-01 15:40:47 +02:00
commit bf900bb6bc

View File

@ -14,28 +14,37 @@ import (
)
var opts struct {
logger *log.Logger
tags map[string]bool
breaks map[string]bool
m sync.Mutex
}
var debugLogger = initDebugLogger()
// make sure that all the initialization happens before the init() functions
// are called, cf https://golang.org/ref/spec#Package_initialization
var _ = initDebug()
func initDebugLogger() (lgr *log.Logger) {
opts.tags = make(map[string]bool)
opts.breaks = make(map[string]bool)
func initDebug() bool {
initDebugLogger()
initDebugTags()
initDebugBreaks()
fmt.Fprintf(os.Stderr, "debug enabled\n")
return true
}
func initDebugLogger() {
debugfile := os.Getenv("DEBUG_LOG")
if debugfile != "" {
if debugfile == "" {
return
}
fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile)
// open logfile
f, err := os.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600)
if err == nil {
// seek to the end
_, err = f.Seek(2, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to seek to the end of %v: %v\n", debugfile, err)
@ -44,7 +53,6 @@ func initDebugLogger() (lgr *log.Logger) {
}
if err != nil && os.IsNotExist(err) {
// create logfile
f, err = os.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
}
@ -53,16 +61,21 @@ func initDebugLogger() (lgr *log.Logger) {
os.Exit(2)
}
// open logger
lgr = log.New(f, "", log.LstdFlags)
}
opts.logger = log.New(f, "", log.LstdFlags)
}
func initDebugTags() {
opts.tags = make(map[string]bool)
// defaults
opts.tags["break"] = true
// initialize tags
env := os.Getenv("DEBUG_TAGS")
if len(env) > 0 {
if len(env) == 0 {
return
}
tags := []string{}
for _, tag := range strings.Split(env, ",") {
@ -88,11 +101,16 @@ func initDebugLogger() (lgr *log.Logger) {
}
fmt.Fprintf(os.Stderr, "debug log enabled for: %v\n", tags)
}
func initDebugBreaks() {
opts.breaks = make(map[string]bool)
env := os.Getenv("DEBUG_BREAK")
if len(env) == 0 {
return
}
// initialize break tags
env = os.Getenv("DEBUG_BREAK")
if len(env) > 0 {
breaks := []string{}
for _, tag := range strings.Split(env, ",") {
@ -102,13 +120,9 @@ func initDebugLogger() (lgr *log.Logger) {
}
fmt.Fprintf(os.Stderr, "debug breaks enabled for: %v\n", breaks)
}
return
}
func Log(tag string, f string, args ...interface{}) {
// synchronize log writes
opts.m.Lock()
defer opts.m.Unlock()
@ -120,8 +134,8 @@ func Log(tag string, f string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "DEBUG["+tag+"]: "+f, args...)
}
if debugLogger != nil {
debugLogger.Printf("["+tag+"] "+f, args...)
if opts.logger != nil {
opts.logger.Printf("["+tag+"] "+f, args...)
}
// check if tag is enabled directly