Debug
A tiny golang debugging utility based on https://github.com/visionmedia/debug principles.
Installation
go get github.com/AlexisVisco/Debug
Usage
debug expose some simple functions like Register, Get, Delete to manage debug.
Example http_debug.go
package main
import (
"fmt"
"log"
"net/http"
debug "github.com/AlexisVisco/debug"
)
var httpdeb, _ = debug.Register("http")
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
httpdeb.Log(fmt.Sprintf("%s %s", r.Method, r.URL.String()))
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Example infinite_debug.go
package main
import (
debug "github.com/AlexisVisco/debug"
"time"
"strconv"
"sync"
)
var fivesec, _ = debug.Register("5 times")
var nivesec, _ = debug.Register("9 times")
var wait sync.WaitGroup
var five = 0
var nine = 0
func main() {
wait.Add(1)
go doEvery(5 * time.Second, func(i time.Time) {
fivesec.Log("5 = " + strconv.Itoa(five))
five++
})
go doEvery(9 * time.Second, func(i time.Time) {
nivesec.Log("9 = " + strconv.Itoa(nine))
nine++
})
wait.Wait()
}
func doEvery(d time.Duration, f func(time.Time)) {
for x := range time.Tick(d) {
f(x)
}
}
The DEBUG environment variable is then used to enable these based on space or comma-delimited names.
Wildcards
The *
character may be used as a wildcard. Suppose for example your library has debuggers named "connect:bodyParser", "connect:compress", "connect:session", instead of listing all three with DEBUG=connect:bodyParser,connect:compress,connect:session, you may simply do DEBUG=connect:*.
Exclusion
The -
prefix character may be used to exclude a debugger.
Example DEBUG=*,-test
=> atest OK, hello OK, test NOT OK
You can combine with wildcard obviously !
Environment Variables
You can set a few environment variables that will change the behavior of the debug logging:
Name | Purpose |
---|
DEBUG | Enables/disables specific debugging namespaces. |
DEBUG_HIDE_DATE | Hide date from debug output (non-TTY). |
DEBUG_COLORS | Whether or not to use colors in the debug output. |
DEBUG_HIDE_LATENCY | Hide latency at the end of a tty output. |
Documentation
Functions:
Methods:
Functions
NewDebug
Prototype: NewDebug(name string) *Debug
Description:
Create a debug structure without registering it. Cannot be accessible with Get
.
Generate a random color from 31 to 37 and 91 to 97 as ainsi code.
debug := debug.NewDebug("woaw")
Register
Prototype: Register(name string) (*Debug, Err)
Description:
Create a debug and registering it. Can be accessible with Get
.
NewDebug
is used to create the structure.
Error:
Return an error if name is already in the registry.
debug, err := debug.Create("woaw")
if err {
fmt.Printf("name %s already used !", "woaw")
}
Get
Prototype: Get(name string) (*Debug, Err)
Description:
Get a debug structure from it name.
Error:
Return an error if name is not in the registry.
debug, err := debug.Get("woaw")
if err {
fmt.Printf("name %s has not been created !", "woaw")
}
Delete
Prototype: Delete(name string) Err
Description:
Delete a debug structure from the registry.
Error:
Return an error if name is not in the registry.
err := debug.Delete("woaw")
if err {
fmt.Printf("name %s has not been created !", "woaw")
}
Enable
Prototype: Enable()
Description:
Enable printing with debug.
debug.Enable()
Disable
Prototype: Disable()
Description:
Disable printing with debug.
debug.Disable()
Methods
Log
Prototype: (d *Debug) Log(message string
Description:
Print if debug is active the message with the name of the debug and the latency between the last call if it was activated.
woaw, _ := debug.Create("woaw")
woaw.Log("Hola !")
woaw.Log("Hola 2 !")
Sprint
Prototype: (d *Debug) Sprint(message string)
Description:
Return the full string that should be printed.
woaw, _ := debug.Create("woaw")
str := waw.Sprint("Hola !")
SetWriter
Prototype: (d *Debug) SetWriter(writer io.Writer, tty bool) *Debug
Description:
Set the writer, if it's a terminal set to true the next parameter.
woaw, _ := debug.Create("woaw")
woaw.SetWriter(os.Stdout, true)
SetFdWriter
Prototype: (d *Debug) SetFdWriter(file *os.File) *Debug
Description:
This function will set the writer and determine if the file.Fd()
is a terminal.
woaw, _ := debug.Create("woaw")
woaw.SetFdWriter(os.Stdout)