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
|
# go-pdebug
[](https://travis-ci.org/lestrrat/go-pdebug)
[](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

# 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
```
|