2014-11-16 14:30:08 +00:00
|
|
|
// +build debug
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-11-16 14:30:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var debugLogger = initDebugLogger()
|
|
|
|
|
|
|
|
func initDebugLogger() *log.Logger {
|
|
|
|
// create new log file
|
2014-12-05 20:45:49 +00:00
|
|
|
filename := fmt.Sprintf("restic-lib-debug-%d-%s",
|
2014-11-16 14:30:08 +00:00
|
|
|
os.Getpid(), time.Now().Format("20060201-150405"))
|
2014-11-16 14:40:28 +00:00
|
|
|
path := filepath.Join(os.TempDir(), filename)
|
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
|
2014-11-16 14:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "unable to create debug log file: %v", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// open logger
|
|
|
|
l := log.New(io.MultiWriter(os.Stderr, f), "DEBUG: ", log.LstdFlags)
|
2014-12-05 20:45:49 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "debug log for restic library activated, writing log file %s\n", path)
|
2014-11-16 14:30:08 +00:00
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func debug(fmt string, args ...interface{}) {
|
|
|
|
debugLogger.Printf(fmt, args...)
|
|
|
|
}
|