-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (85 loc) · 2.86 KB
/
index.js
File metadata and controls
107 lines (85 loc) · 2.86 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
'use strict'
const { inspect, format } = require('util')
const allows = []
const ignores = []
const colors = [2, 3, 4, 5, 6]
let instanceCounter = 0
function ms(ms) {
if (ms >= 86400000) return Math.round(ms / 86400000) + 'd'
if (ms >= 3600000) return Math.round(ms / 3600000) + 'h'
if (ms >= 60000) return Math.round(ms / 60000) + 'm'
if (ms >= 1000) return Math.round(ms / 1000) + 's'
return ms + 'ms'
}
function getColor() {
const c = (instanceCounter % colors.length)
instanceCounter++
return (c === 0) ? 2 : 2+c
}
function getState(ns) {
if (process.env.DEBUG === '*') {
return true
}
for (let elm of ignores) {
if (elm.test(ns)) return false
}
for (let elm of allows) {
if (elm.test(ns)) return true
}
return false
}
function start(ns = '') {
const ary = ns.split(/[\s,]+/)
for (let elm of ary) {
if (!elm) continue
ns = elm.replace(/\*/g, '.*?')
if (ns[0] === '-') {
ignores.push(new RegExp('^' + ns.slice(1) + '$'))
} else {
allows.push(new RegExp('^' + ns + '$'))
}
}
}
start(process.env.DEBUG)
module.exports = exports = (namespace, color) => {
let previous
debug.namespace = namespace
debug.color = color ? color : getColor()
debug.enabled = getState(namespace)
debug.sub = sub
function debug(...args) {
if (!debug.enabled) return
const self = debug
const c = self.color
const now = Date.now()
const duration = now - (previous || now)
previous = now
// Parse and reformat messages
if (typeof args[0] !== 'string') {
args.unshift('%s')
} else {
args[0] = args[0].replace(/%([oO])/g, '%s')
}
for(let i = 1; i < args.length; i++) {
switch(typeof args[i]) {
case 'function':
args[i] = args[i].toString()
break
case 'object':
args[i] = (args[i] instanceof Error) ? args[i].stack || args[i].message : inspect(args[i], false, null, true)
break
}
}
const prefix = `\u001b[3${c};1m${self.namespace} \u001b[0m`
let str = ` ${prefix}`
str += format(...args).split('\n').join('\n ' + prefix)
str += ` \u001b[3${c}m+` + ms(duration) + `\u001b[0m`
process.stderr.write(str + '\n')
}
return debug
}
//Support colors: white|grey|black|blue|cyan|green|magenta|red|yellow
function sub(namespace, color = '') {
color = (color === '') ? '3'+this.color : inspect.colors[color][0]
return exports(`${this.namespace}\u001b[${color};1m:${namespace}\u001b[0m`, this.color);
}