Skip to content

Commit

Permalink
feat(eventlog): e := EventBegin; e.Done()
Browse files Browse the repository at this point in the history
```Go
e := log.EventBegin(ctx, "provide")
e.Done()

e := log.EventBegin(ctx, "provide")
e.Close() // implements io.Closer in case you want to register with some lifecycle management system.
```
  • Loading branch information
Brian Tiger Chow committed Jan 15, 2015
1 parent 47701aa commit 1aad832
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions util/eventlog/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
}
}
34 changes: 34 additions & 0 deletions util/eventlog/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions util/eventlog/loggable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1aad832

Please sign in to comment.