@cryptotaxi247 / kubo / commits / 8f16a4eff

feat(eventlog) add support for e := log.Begin... e.Append(Loggable)

Brian Tiger Chow committed Jan 29, 2015 at 00:38 UTC 8f16a4effff5c77d59bacdc0b2894ca5851fcc4d
1 file changed +29 -19
thirdparty/eventlog/log.go
+29 -19
@@ -2,7 +2,6 @@ package eventlog
2
3 import (
4 "fmt"
5 - "io"
5 "time"
6
7 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
@@ -50,14 +49,7 @@ type EventLogger interface {
49 // the metadata is logged.
50 Event(ctx context.Context, event string, m ...Loggable)
51
53 - EventBegin(ctx context.Context, event string, m ...Loggable) DoneCloser
54 -}
55 -
56 -type DoneCloser interface {
57 - // Done ends the event
58 - Done()
59 - // io.Closer is a convenience-alias for Done
60 - io.Closer
52 + EventBegin(ctx context.Context, event string, m ...Loggable) EventInProgress
53 }
54
55 // Logger retrieves an event logger by name
@@ -76,14 +68,21 @@ type eventLogger struct {
68 // TODO add log-level
69 }
70
79 -func (el *eventLogger) EventBegin(ctx context.Context, event string, metadata ...Loggable) DoneCloser {
71 +func (el *eventLogger) EventBegin(ctx context.Context, event string, metadata ...Loggable) EventInProgress {
72 start := time.Now()
73 el.Event(ctx, fmt.Sprintf("%sBegin", event), metadata...)
82 - return doneCloserFunc(func() {
83 - el.Event(ctx, event, append(metadata, LoggableMap(map[string]interface{}{
74 +
75 + eip := EventInProgress{}
76 + eip.doneFunc = func(additional []Loggable) {
77 +
78 + metadata = append(metadata, additional...) // anything added during the operation
79 + metadata = append(metadata, LoggableMap(map[string]interface{}{ // finally, duration of event
80 "duration": time.Now().Sub(start),
85 - }))...)
86 - })
81 + }))
82 +
83 + el.Event(ctx, event, metadata...)
84 + }
85 + return eip
86 }
87
88 func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Loggable) {
@@ -111,13 +110,24 @@ func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Logg
110 e.Log() // TODO replace this when leveled-logs have been implemented
111 }
112
114 -type doneCloserFunc func()
113 +type EventInProgress struct {
114 + loggables []Loggable
115 + doneFunc func([]Loggable)
116 +}
117 +
118 +// Append adds loggables to be included in the call to Done
119 +func (eip EventInProgress) Append(l Loggable) {
120 + eip.loggables = append(eip.loggables, l)
121 +}
122
116 -func (f doneCloserFunc) Done() {
117 - f()
123 +// Done creates a new Event entry that includes the duration and appended
124 +// loggables.
125 +func (eip EventInProgress) Done() {
126 + eip.doneFunc(eip.loggables) // create final event with extra data
127 }
128
120 -func (f doneCloserFunc) Close() error {
121 - f.Done()
129 +// Close is an alias for done
130 +func (eip EventInProgress) Close() error {
131 + eip.Done()
132 return nil
133 }