pax_global_header00006660000000000000000000000064132427230250014512gustar00rootroot0000000000000052 comment=569c97477ae8837e053e5a50bc739e15172b8ebe golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/000077500000000000000000000000001324272302500224775ustar00rootroot00000000000000golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/.gitignore000066400000000000000000000004121324272302500244640ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test *.prof golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/.travis.yml000066400000000000000000000004221324272302500246060ustar00rootroot00000000000000language: go sudo: false go: - 1.6 - 1.7 - tip install: - go get -t -v ./... - go get -t -tags debug0 -v ./... script: - go test -v ./... - go test -tags debug ./... - PDEBUG_TRACE=1 go test -tags debug ./... - go test -tags debug0 ./... golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/LICENSE000066400000000000000000000020631324272302500235050ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 lestrrat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/README.md000066400000000000000000000045151324272302500237630ustar00rootroot00000000000000# go-pdebug [![Build Status](https://travis-ci.org/lestrrat/go-pdebug.svg?branch=master)](https://travis-ci.org/lestrrat/go-pdebug) [![GoDoc](https://godoc.org/github.com/lestrrat/go-pdebug?status.svg)](https://godoc.org/github.com/lestrrat/go-pdebug) Utilities for my print debugging fun. YMMV # WARNING This repository has been moved to [github.com/lestrrat-go/pdebug](https://github.com/lestrrat-go/pdebug). This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path. # Synopsis ![optimized](https://pbs.twimg.com/media/CbiqhzLUUAIN_7o.png) # Description Building with `pdebug` declares a constant, `pdebug.Enabled` which you can use to easily compile in/out depending on the presence of a build tag. ```go func Foo() { // will only be available if you compile with `-tags debug` if pdebug.Enabled { pdebug.Printf("Starting Foo()! } } ``` Note that using `github.com/lestrrat/go-pdebug` and `-tags debug` only compiles in the code. In order to actually show the debug trace, you need to specify an environment variable: ```shell # For example, to show debug code during testing: PDEBUG_TRACE=1 go test -tags debug ``` If you want to forcefully show the trace (which is handy when you're debugging/testing), you can use the `debug0` tag instead: ```shell go test -tags debug0 ``` # Markers When you want to print debug a chain of function calls, you can use the `Marker` functions: ```go func Foo() { if pdebug.Enabled { g := pdebug.Marker("Foo") defer g.End() } pdebug.Printf("Inside Foo()!") } ``` This will cause all of the `Printf` calls to automatically indent the output so it's visually easier to see where a certain trace log is being generated. By default it will print something like: ``` |DEBUG| START Foo |DEBUG| Inside Foo()! |DEBUG| END Foo (1.23μs) ``` If you want to automatically show the error value you are returning (but only if there is an error), you can use the `BindError` method: ```go func Foo() (err error) { if pdebug.Enabled { g := pdebug.Marker("Foo").BindError(&err) defer g.End() } pdebug.Printf("Inside Foo()!") return errors.New("boo") } ``` This will print something like: ``` |DEBUG| START Foo |DEBUG| Inside Foo()! |DEBUG| END Foo (1.23μs): ERROR boo ``` golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/autoflag_off.go000066400000000000000000000002771324272302500254700ustar00rootroot00000000000000// +build debug package pdebug import ( "os" "strconv" ) var Trace = false func init() { if b, err := strconv.ParseBool(os.Getenv("PDEBUG_TRACE")); err == nil && b { Trace = true } }golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/autoflag_on.go000066400000000000000000000000641324272302500253240ustar00rootroot00000000000000// +build debug0 package pdebug var Trace = true golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/common.go000066400000000000000000000007771324272302500243310ustar00rootroot00000000000000package pdebug import ( "io" "os" "sync" "time" ) type pdctx struct { mutex sync.Mutex indentL int LogTime bool Prefix string Writer io.Writer } var emptyMarkerGuard = &markerg{} type markerg struct { indentg guard ctx *pdctx f string args []interface{} start time.Time errptr *error } var DefaultCtx = &pdctx{ LogTime: true, Prefix: "|DEBUG| ", Writer: os.Stdout, } type guard struct { cb func() } func (g *guard) End() { if cb := g.cb; cb != nil { cb() } } golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/common_test.go000066400000000000000000000044021324272302500253550ustar00rootroot00000000000000package pdebug import ( "bytes" "errors" "io" "regexp" "testing" "github.com/stretchr/testify/assert" ) func setw(ctx *pdctx, w io.Writer) func() { oldw := ctx.Writer ctx.Writer = w return func() { ctx.Writer = oldw } } func TestPrintf(t *testing.T) { buf := &bytes.Buffer{} wg := setw(DefaultCtx, buf) defer wg() Printf("Hello, World!") if Enabled && Trace { re := regexp.MustCompile(`\|DEBUG\| \d+\.\d+ Hello, World!\n`) if !assert.True(t, re.MatchString(buf.String()), "Simple Printf works") { return } } else { if !assert.Equal(t, "", buf.String(), "Simple Printf should be suppressed") { return } } } func TestMarker(t *testing.T) { buf := &bytes.Buffer{} wg := setw(DefaultCtx, buf) defer wg() f2 := func() (err error) { g := Marker("f2").BindError(&err) defer g.End() Printf("Hello, World!") return errors.New("dummy error") } f1 := func() { g := Marker("f1") defer g.End() f2() } f1() if Enabled && Trace { re := regexp.MustCompile(`\|DEBUG\| \d+\.\d+ START f1\n\|DEBUG\| \d+\.\d+ START f2\n\|DEBUG\| \d+\.\d+ Hello, World!\n\|DEBUG\| \d+\.\d+ END f2 \(`) if !assert.True(t, re.MatchString(buf.String()), "Markers should work") { t.Logf("Expected '%v'", re) t.Logf("Actual '%v'", buf.String()) return } } else { if !assert.Equal(t, "", buf.String(), "Markers should work") { return } } } func TestLegacyMarker(t *testing.T) { buf := &bytes.Buffer{} wg := setw(DefaultCtx, buf) defer wg() f2 := func() (err error) { g := IPrintf("START f2") defer func() { if err == nil { g.IRelease("END f2") } else { g.IRelease("END f2: %s", err) } }() Printf("Hello, World!") return errors.New("dummy error") } f1 := func() { g := IPrintf("START f1") defer g.IRelease("END f1") f2() } f1() if Enabled && Trace { re := regexp.MustCompile(`\|DEBUG\| \d+\.\d+ START f1\n\|DEBUG\| \d+\.\d+ START f2\n\|DEBUG\| \d+\.\d+ Hello, World!\n\|DEBUG\| \d+\.\d+ END f2`) if !assert.True(t, re.MatchString(buf.String()), "Markers should work") { t.Logf("Expected '%v'", re) t.Logf("Actual '%v'", buf.String()) return } // TODO: check for error and timestamp } else { if !assert.Equal(t, "", buf.String(), "Markers should work") { return } } } golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/debug0_test.go000066400000000000000000000002721324272302500252340ustar00rootroot00000000000000//+build debug0,!debug package pdebug import ( "testing" "github.com/stretchr/testify/assert" ) func TestDebug0Enabled(t *testing.T) { assert.True(t, Enabled, "Enable is true") } golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/debug_off.go000066400000000000000000000030051324272302500247440ustar00rootroot00000000000000//+build !debug,!debug0 package pdebug // Enabled is true if `-tags debug` or `-tags debug0` is used // during compilation. Use this to "ifdef-out" debug blocks. const Enabled = false // Trace is true if `-tags debug` is used AND the environment // variable `PDEBUG_TRACE` is set to a `true` value (i.e., // 1, true, etc), or `-tags debug0` is used. This allows you to // compile-in the trace logs, but only show them when you // set the environment variable const Trace = false // IRelease is deprecated. Use Marker()/End() instead func (g guard) IRelease(f string, args ...interface{}) {} // IPrintf is deprecated. Use Marker()/End() instead func IPrintf(f string, args ...interface{}) guard { return guard{} } // Printf prints to standard out, just like a normal fmt.Printf, // but respects the indentation level set by IPrintf/IRelease. // Printf is no op unless you compile with the `debug` tag. func Printf(f string, args ...interface{}) {} // Dump dumps the objects using go-spew. // Dump is a no op unless you compile with the `debug` tag. func Dump(v ...interface{}) {} // Marker marks the beginning of an indented block. The message // you specify in the arguments is prefixed witha "START", and // subsequent calls to Printf will be indented one level more. // // To reset this, you must call End() on the guard object that // gets returned by Marker(). func Marker(f string, args ...interface{}) *markerg { return emptyMarkerGuard } func (g *markerg) BindError(_ *error) *markerg { return g } func (g *markerg) End() {} golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/debug_on.go000066400000000000000000000055551324272302500246220ustar00rootroot00000000000000// +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...) } golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/debug_test.go000066400000000000000000000007561324272302500251630ustar00rootroot00000000000000//+build debug,!debug0 package pdebug import ( "os" "strconv" "testing" "github.com/stretchr/testify/assert" ) func TestDebugEnabled(t *testing.T) { if !assert.True(t, Enabled, "Enable is true") { return } b, err := strconv.ParseBool(os.Getenv("PDEBUG_TRACE")) if err == nil && b { if !assert.True(t, Trace, "Trace is true") { return } t.Logf("Trace is enabled") } else { if !assert.False(t, Trace, "Trace is false") { return } t.Logf("Trace is disabled") } }golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/doc.go000066400000000000000000000013521324272302500235740ustar00rootroot00000000000000// Package pdebug provides tools to produce debug logs the way the author // (Daisuke Maki a.k.a. lestrrat) likes. All of the functions are no-ops // unless you compile with the `-tags debug` option. // // When you compile your program with `-tags debug`, no trace is displayed, // but the code enclosed within `if pdebug.Enabled { ... }` is compiled in. // To show the debug trace, set the PDEBUG_TRACE environment variable to // true (or 1, or whatever `strconv.ParseBool` parses to true) // // If you want to show the debug trace regardless of an environment variable, // for example, perhaps while you are debugging or running tests, use the // `-tags debug0` build tag instead. This will enable the debug trace // forcefully package pdebug golang-github-lestrrat-go-pdebug_0.0~git20180220.0.569c974/nodebug_test.go000066400000000000000000000003411324272302500255060ustar00rootroot00000000000000//+build !debug,!debug0 package pdebug import ( "testing" "github.com/stretchr/testify/assert" ) func TestDisabled(t *testing.T) { assert.False(t, Enabled, "Enable is false") assert.False(t, Trace, "Trace is false") }