This repository has been archived by the owner on Oct 30, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
85 lines (68 loc) · 2.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var nanologger = require('nanologger')
var assert = require('assert')
var ChooInstrument = require('./lib/instrument')
module.exports = logger
function logger (opts) {
opts = opts || {}
var colors = opts.colors || {}
var initialRender = true
assert.equal(typeof opts, 'object', 'choo-log: opts should be type object')
return function (state, emitter) {
var hook = ChooInstrument(emitter)
var log = nanologger('choo', { colors: colors })
hook.on('log:trace', log.trace.bind(log))
hook.on('log:debug', log.debug.bind(log))
hook.on('log:info', log.info.bind(log))
hook.on('log:warn', log.warn.bind(log))
hook.on('log:error', log.error.bind(log))
hook.on('log:fatal', log.fatal.bind(log))
hook.on('service-worker', function (data) {
log.info('Service worker installed')
})
hook.on('event', function (eventName, data, timing) {
if (timing) {
var duration = timing.duration.toFixed()
var level = duration < 50 ? 'info' : 'warn'
if (data !== undefined) log[level](eventName, data, duration + 'ms')
else log[level](eventName, duration + 'ms')
} else {
if (data !== undefined) log.info(eventName, data)
else log.info(eventName)
}
})
hook.on('use', function (count, duration) {
log.debug('use', { count: count }, duration + 'ms')
})
hook.on('unhandled', function (eventName, data) {
log.error('No listeners for ' + eventName)
})
hook.on('DOMContentLoaded', function (timing) {
if (!timing) return log.info('DOMContentLoaded')
var level = timing.interactive < 1000 ? 'info' : 'warn'
log[level]('DOMContentLoaded', timing.interactive + 'ms to interactive')
})
hook.on('render', function (timings) {
if (!timings) return log.info('render')
var duration = timings.render.duration.toFixed()
var msg = 'render'
if (initialRender) {
initialRender = false
msg = 'Initial ' + msg
}
// each frame has 10ms available for userland stuff
var fps = Math.min((600 / duration).toFixed(), 60)
if (fps === 60) {
log.info(msg, fps + 'fps', duration + 'ms')
} else {
log.warn(msg, fps + 'fps', duration + 'ms', {
render: timings.render.duration.toFixed() + 'ms',
morph: timings.morph.duration.toFixed() + 'ms'
})
}
})
hook.on('resource-timing-buffer-full', function () {
log.error("The browser's Resource Resource timing buffer is full. Cannot store any more timing information")
})
hook.start()
}
}