Commit 2fc97ad8 authored by Brian Tiger Chow's avatar Brian Tiger Chow

Merge pull request #564 from jbenet/feat/eventlogBeginDone

feat(eventlog): e := EventBegin; e.Done()
parents f70c57d8 b252d045
package eventlog
import "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
func ExampleEventLogger() {
{
log := EventLogger(nil)
e := log.EventBegin(context.Background(), "dial")
e.Done()
}
{
log := EventLogger(nil)
e := log.EventBegin(context.Background(), "dial")
_ = e.Close() // implements io.Closer for convenience
}
}
package eventlog
import (
"fmt"
"io"
"time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
prelog "github.com/jbenet/go-ipfs/util/prefixlog"
......@@ -30,6 +34,15 @@ type EventLogger interface {
// Finally the timestamp and package name are added to the accumulator and
// the metadata is logged.
Event(ctx context.Context, event string, m ...Loggable)
EventBegin(ctx context.Context, event string, m ...Loggable) DoneCloser
}
type DoneCloser interface {
// Done ends the event
Done()
// io.Closer is a convenience-alias for Done
io.Closer
}
// Logger retrieves an event logger by name
......@@ -53,6 +66,16 @@ func (el *eventLogger) Prefix(fmt string, args ...interface{}) EventLogger {
return &eventLogger{system: el.system, PrefixLogger: l}
}
func (el *eventLogger) EventBegin(ctx context.Context, event string, metadata ...Loggable) DoneCloser {
start := time.Now()
el.Event(ctx, fmt.Sprintln(event, "Begin"), metadata...)
return doneCloserFunc(func() {
el.Event(ctx, event, append(metadata, LoggableMap(map[string]interface{}{
"duration": time.Now().Sub(start),
}))...)
})
}
func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Loggable) {
// Collect loggables for later logging
......@@ -77,3 +100,14 @@ func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Logg
e.Log() // TODO replace this when leveled-logs have been implemented
}
type doneCloserFunc func()
func (f doneCloserFunc) Done() {
f()
}
func (f doneCloserFunc) Close() error {
f.Done()
return nil
}
......@@ -4,3 +4,9 @@ package eventlog
type Loggable interface {
Loggable() map[string]interface{}
}
type LoggableMap map[string]interface{}
func (l LoggableMap) Loggable() map[string]interface{} {
return l
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment