Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
2fc97ad8
Commit
2fc97ad8
authored
Jan 14, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #564 from jbenet/feat/eventlogBeginDone
feat(eventlog): e := EventBegin; e.Done()
parents
f70c57d8
b252d045
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
0 deletions
+56
-0
util/eventlog/example_test.go
util/eventlog/example_test.go
+16
-0
util/eventlog/log.go
util/eventlog/log.go
+34
-0
util/eventlog/loggable.go
util/eventlog/loggable.go
+6
-0
No files found.
util/eventlog/example_test.go
0 → 100644
View file @
2fc97ad8
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
}
}
util/eventlog/log.go
View file @
2fc97ad8
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
}
util/eventlog/loggable.go
View file @
2fc97ad8
...
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment