-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
67 lines (56 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var slice = [].slice
module.exports = function (app) {
// trace something
app.context.trace = function trace(event) {
var args = []
switch (arguments.length) {
case 0:
throw new Error('No event defined!')
case 1: break
case 2:
args = [arguments[1]]
break
default:
args = slice.call(arguments, 1)
}
dispatch(this, event, args)
return this
}
// attach a listener
app.instrument = instrument
// log all events to the debugger
app.debug = function () {
var Debug = require('debug')
app.instrument(function (context, event, date, args) {
var id = context.id
if (!id) throw new Error('you need to set `this.id` to debug')
if (Buffer.isBuffer(id)) id = id.toString('base64')
var debug = context.debug
if (!debug) debug = context.debug = Debug('koa-trace:' + id)
var output = ''
switch (args.length) {
case 0: break
case 1:
output = args[0]
break
default:
output = args
}
debug(event, output)
})
return this
}
// attach a listener
var listeners = []
function instrument(fn) {
listeners.push(fn)
return this
}
// dispatch an event to all listeners
function dispatch(context, event, args) {
var date = new Date()
for (var i = 0; i < listeners.length; i++)
listeners[i](context, event, date, args)
}
return app
}