1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// +build debug OR debug0
package pdebug
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
)
const Enabled = true
type Guard interface {
End()
}
var emptyGuard = &guard{}
func (ctx *pdctx) Unindent() {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
ctx.indentL--
}
func (ctx *pdctx) Indent() guard {
ctx.mutex.Lock()
ctx.indentL++
ctx.mutex.Unlock()
return guard{cb: ctx.Unindent}
}
func (ctx *pdctx) preamble(buf *bytes.Buffer) {
if p := ctx.Prefix; len(p) > 0 {
buf.WriteString(p)
}
if ctx.LogTime {
fmt.Fprintf(buf, "%0.5f ", float64(time.Now().UnixNano()) / 1000000.0)
}
for i := 0; i < ctx.indentL; i++ {
buf.WriteString(" ")
}
}
func (ctx *pdctx) Printf(f string, args ...interface{}) {
if !strings.HasSuffix(f, "\n") {
f = f + "\n"
}
buf := bytes.Buffer{}
ctx.preamble(&buf)
fmt.Fprintf(&buf, f, args...)
buf.WriteTo(ctx.Writer)
}
func Marker(f string, args ...interface{}) *markerg {
return DefaultCtx.Marker(f, args...)
}
func (ctx *pdctx) Marker(f string, args ...interface{}) *markerg {
if !Trace {
return emptyMarkerGuard
}
buf := &bytes.Buffer{}
ctx.preamble(buf)
buf.WriteString("START ")
fmt.Fprintf(buf, f, args...)
if buf.Len() > 0 {
if b := buf.Bytes(); b[buf.Len()-1] != '\n' {
buf.WriteRune('\n')
}
}
buf.WriteTo(ctx.Writer)
g := ctx.Indent()
return &markerg{
indentg: g,
ctx: ctx,
f: f,
args: args,
start: time.Now(),
errptr: nil,
}
}
func (g *markerg) BindError(errptr *error) *markerg {
if g.ctx == nil {
return g
}
g.ctx.mutex.Lock()
defer g.ctx.mutex.Unlock()
g.errptr = errptr
return g
}
func (g *markerg) End() {
if g.ctx == nil {
return
}
g.indentg.End() // unindent
buf := &bytes.Buffer{}
g.ctx.preamble(buf)
fmt.Fprint(buf, "END ")
fmt.Fprintf(buf, g.f, g.args...)
fmt.Fprintf(buf, " (%s)", time.Since(g.start))
if errptr := g.errptr; errptr != nil && *errptr != nil {
fmt.Fprintf(buf, ": ERROR: %s", *errptr)
}
if buf.Len() > 0 {
if b := buf.Bytes(); b[buf.Len()-1] != '\n' {
buf.WriteRune('\n')
}
}
buf.WriteTo(g.ctx.Writer)
}
type legacyg struct {
guard
start time.Time
}
var emptylegacyg = legacyg{}
func (g legacyg) IRelease(f string, args ...interface{}) {
if !Trace {
return
}
g.End()
dur := time.Since(g.start)
Printf("%s (%s)", fmt.Sprintf(f, args...), dur)
}
// IPrintf indents and then prints debug messages. Execute the callback
// to undo the indent
func IPrintf(f string, args ...interface{}) legacyg {
if !Trace {
return emptylegacyg
}
DefaultCtx.Printf(f, args...)
g := legacyg{
guard: DefaultCtx.Indent(),
start: time.Now(),
}
return g
}
// Printf prints debug messages. Only available if compiled with "debug" tag
func Printf(f string, args ...interface{}) {
if !Trace {
return
}
DefaultCtx.Printf(f, args...)
}
func Dump(v ...interface{}) {
if !Trace {
return
}
spew.Dump(v...)
}
|