1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-19 14:57:04 +00:00

Introduce a trace wrapper

This patch introduces a wrapper to golang.org/x/net/trace with a new method
that will be useful later on, making logging and tracing less verbose.
This commit is contained in:
Alberto Bertogli
2015-11-06 03:45:36 +00:00
parent 58de5a6200
commit fbf1060b71
3 changed files with 47 additions and 4 deletions

44
internal/trace/trace.go Normal file
View File

@@ -0,0 +1,44 @@
// Package trace extends golang.org/x/net/trace.
package trace
import (
"fmt"
"github.com/golang/glog"
nettrace "golang.org/x/net/trace"
)
type Trace struct {
family string
title string
t nettrace.Trace
}
func New(family, title string) *Trace {
return &Trace{family, title, nettrace.New(family, title)}
}
func (t *Trace) LazyPrintf(format string, a ...interface{}) {
t.t.LazyPrintf(format, a...)
if glog.V(2) {
msg := fmt.Sprintf("%p %s %s: %+q", t, t.family, t.title,
fmt.Sprintf(format, a...))
glog.InfoDepth(1, msg)
}
}
func (t *Trace) SetError() {
t.t.SetError()
}
func (t *Trace) Errorf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
t.t.SetError()
t.LazyPrintf("Error: %v", err)
return err
}
func (t *Trace) Finish() {
t.t.Finish()
}