forked from chunpu/min-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
118 lines (102 loc) · 2.65 KB
/
debug.js
File metadata and controls
118 lines (102 loc) · 2.65 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module.exports = exports = Debug
var colors = 'lightseagreen forestgreen goldenrod dodgerblue darkorchid crimson'.split(' ')
var colorIndex = 0
var prev
var inherit = 'color:inherit'
var console = global.console
var doc = global.document
var names = []
var skips = []
var debugElement
exports.prefix = ''
exports.init = init
exports.enable = enable
var logs = exports.logs = {}
function Debug(namespace) {
var color = 'color:' + getColor()
return enabled(namespace) ? function() {
exports.log(namespace, arguments, color)
} : noop
}
function init(key) {
key = key || 'debug'
var reg = new RegExp(key + '=(\\S+)')
var res = reg.exec(location.href)
if (res) {
enable(res[1])
exports.log = logs.html
} else if (global.localStorage && console) {
exports.log = logs.console
try {
enable(localStorage[key])
} catch (ignore) {}
}
}
function noop() {}
function enable(namespaces) {
if (!namespaces) return
skips = []
names = []
var split = namespaces.split(/[\s,]+/)
for (var i = 0; i < split.length; i++) {
if (!split[i]) continue
namespaces = split[i].replace(/\*/g, '.*?')
if ('-' == namespaces[0])
skips.push(new RegExp('^' + namespaces.substr(1) + '$'))
else
names.push(new RegExp('^' + namespaces + '$'))
}
}
function enabled(name) {
var i = 0, reg
for (i = 0; reg = skips[i++];) {
if (reg.test(name)) return false
}
for (i = 0; reg = names[i++];) {
if (reg.test(name)) return true
}
}
function getColor() {
return colors[colorIndex++ % colors.length]
}
logs.console = function(namespace, args, color) {
var curr = +new Date
var ms = curr - (prev || curr)
prev = curr
var label = exports.prefix + namespace
var main = '%c' + label + '%c'
var arr = [null, color, inherit]
for (var i = 0; i < args.length; i++) {
arr.push(args[i])
main += ' %o'
}
arr.push(color)
main += '%c +' + ms + 'ms'
arr[0] = main
console.debug.apply(console, arr)
}
logs.html = function(namespace, args, color) {
// init element when first log, cannot cancel after inited
debugElement = debugElement || initDebugElement()
var items = ['[' + namespace + ']']
var len = args.length
for (var i = 0; i < len; i++) {
var val = args[i]
try {
val = JSON.stringify(val, 0, 4)
} catch (e) {
val += ''
}
items.push(val)
}
debugElement.value += items.join(' ') + '\n'
debugElement.scrollTop = debugElement.scrollHeight
}
function initDebugElement() {
var elem = doc.createElement('textarea')
elem.style.cssText = 'z-index:999;width:100%;height:300px;overflow:auto;line-height:1.4;background:#333;color:#fff;font:16px Consolas;border:none;'
var box = doc.body || doc.documentElement
box.insertBefore(elem, box.firstChild)
return elem
}
init()