2015-01-14 21:08:48 +00:00
|
|
|
// +build debug
|
|
|
|
|
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-05-01 22:48:26 +00:00
|
|
|
"path/filepath"
|
2015-01-14 21:08:48 +00:00
|
|
|
"runtime"
|
2015-05-01 23:13:07 +00:00
|
|
|
"strconv"
|
2015-01-14 21:08:48 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-10-26 19:10:03 +00:00
|
|
|
"time"
|
2015-01-14 21:08:48 +00:00
|
|
|
)
|
|
|
|
|
2015-10-26 19:10:03 +00:00
|
|
|
type process struct {
|
|
|
|
tag string
|
|
|
|
goroutine int
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
var opts struct {
|
2015-04-30 05:05:58 +00:00
|
|
|
logger *log.Logger
|
2015-01-14 21:08:48 +00:00
|
|
|
tags map[string]bool
|
2015-10-26 19:10:03 +00:00
|
|
|
last map[process]time.Time
|
2015-01-14 21:08:48 +00:00
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-05-01 13:39:01 +00:00
|
|
|
// make sure that all the initialization happens before the init() functions
|
|
|
|
// are called, cf https://golang.org/ref/spec#Package_initialization
|
|
|
|
var _ = initDebug()
|
|
|
|
|
|
|
|
func initDebug() bool {
|
2015-04-30 05:05:58 +00:00
|
|
|
initDebugLogger()
|
|
|
|
initDebugTags()
|
2015-01-14 21:08:48 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "debug enabled\n")
|
2015-05-01 13:39:01 +00:00
|
|
|
|
|
|
|
return true
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
func initDebugLogger() {
|
2015-01-14 21:08:48 +00:00
|
|
|
debugfile := os.Getenv("DEBUG_LOG")
|
2015-04-30 05:05:58 +00:00
|
|
|
if debugfile == "" {
|
|
|
|
return
|
|
|
|
}
|
2015-02-08 13:46:29 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile)
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
f, err := os.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
_, err = f.Seek(2, 0)
|
2015-01-14 21:08:48 +00:00
|
|
|
if err != nil {
|
2015-04-30 05:05:58 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "unable to seek to the end of %v: %v\n", debugfile, err)
|
|
|
|
os.Exit(3)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
2015-04-30 05:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
f, err = os.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "unable to open debug log file: %v\n", err)
|
|
|
|
os.Exit(2)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
opts.logger = log.New(f, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDebugTags() {
|
|
|
|
opts.tags = make(map[string]bool)
|
2015-10-26 19:10:03 +00:00
|
|
|
opts.last = make(map[process]time.Time)
|
2015-04-30 05:05:58 +00:00
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
// defaults
|
|
|
|
opts.tags["break"] = true
|
|
|
|
|
|
|
|
// initialize tags
|
|
|
|
env := os.Getenv("DEBUG_TAGS")
|
2015-04-30 05:05:58 +00:00
|
|
|
if len(env) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := []string{}
|
|
|
|
|
|
|
|
for _, tag := range strings.Split(env, ",") {
|
|
|
|
t := strings.TrimSpace(tag)
|
|
|
|
val := true
|
|
|
|
if t[0] == '-' {
|
|
|
|
val = false
|
|
|
|
t = t[1:]
|
|
|
|
} else if t[0] == '+' {
|
|
|
|
val = true
|
|
|
|
t = t[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// test pattern
|
|
|
|
_, err := path.Match(t, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: invalid pattern %q: %v\n", t, err)
|
|
|
|
os.Exit(5)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
opts.tags[t] = val
|
|
|
|
tags = append(tags, tag)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "debug log enabled for: %v\n", tags)
|
|
|
|
}
|
2015-01-14 21:08:48 +00:00
|
|
|
|
2015-05-01 22:48:26 +00:00
|
|
|
// 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
|
2015-10-26 19:10:03 +00:00
|
|
|
func getPosition(goroutine int) string {
|
2015-05-01 22:48:26 +00:00
|
|
|
_, file, line, ok := runtime.Caller(2)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-10-25 16:06:20 +00:00
|
|
|
return fmt.Sprintf("%3d %s:%d", goroutine, filepath.Base(file), line)
|
2015-05-01 22:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 23:13:07 +00:00
|
|
|
var maxTagLen = 10
|
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
func Log(tag string, f string, args ...interface{}) {
|
|
|
|
opts.m.Lock()
|
|
|
|
defer opts.m.Unlock()
|
|
|
|
|
2015-10-26 19:10:03 +00:00
|
|
|
goroutine := goroutineNum()
|
|
|
|
|
|
|
|
last, ok := opts.last[process{tag, goroutine}]
|
|
|
|
if !ok {
|
|
|
|
last = time.Now()
|
|
|
|
}
|
|
|
|
current := time.Now()
|
|
|
|
opts.last[process{tag, goroutine}] = current
|
|
|
|
|
2015-02-07 16:04:20 +00:00
|
|
|
if f[len(f)-1] != '\n' {
|
|
|
|
f += "\n"
|
|
|
|
}
|
|
|
|
|
2015-05-01 23:13:07 +00:00
|
|
|
if len(tag) > maxTagLen {
|
|
|
|
maxTagLen = len(tag)
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:10:03 +00:00
|
|
|
formatStringTag := "%2.3f [%" + strconv.FormatInt(int64(maxTagLen), 10) + "s]"
|
|
|
|
formatString := fmt.Sprintf(formatStringTag+" %s %s", current.Sub(last).Seconds(), tag, getPosition(goroutine), f)
|
2015-05-01 22:48:26 +00:00
|
|
|
|
2015-01-14 21:08:48 +00:00
|
|
|
dbgprint := func() {
|
2015-05-01 22:48:26 +00:00
|
|
|
fmt.Fprintf(os.Stderr, formatString, args...)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 05:05:58 +00:00
|
|
|
if opts.logger != nil {
|
2015-05-01 22:48:26 +00:00
|
|
|
opts.logger.Printf(formatString, args...)
|
2015-01-14 21:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if tag is enabled directly
|
|
|
|
if v, ok := opts.tags[tag]; ok {
|
|
|
|
if v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for globbing
|
|
|
|
for k, v := range opts.tags {
|
|
|
|
if m, _ := path.Match(k, tag); m {
|
|
|
|
if v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if tag "all" is enabled
|
|
|
|
if v, ok := opts.tags["all"]; ok && v {
|
|
|
|
dbgprint()
|
|
|
|
}
|
|
|
|
}
|