|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const util = require('util') |
| 4 | +const allows = [] |
| 5 | +const ignores = [] |
| 6 | +const colors = [2, 3, 4, 5, 6] |
| 7 | +let counter = 0 |
| 8 | + |
| 9 | +function ms(ms) { |
| 10 | + if (ms >= 86400000) return Math.round(ms / 86400000) + 'd' |
| 11 | + if (ms >= 3600000) return Math.round(ms / 3600000) + 'h' |
| 12 | + if (ms >= 60000) return Math.round(ms / 60000) + 'm' |
| 13 | + if (ms >= 1000) return Math.round(ms / 1000) + 's' |
| 14 | + return ms + 'ms' |
| 15 | +} |
| 16 | + |
| 17 | +function fillColor(str, c = 0) { |
| 18 | + return '\u001b[3' + c + ';1m' + str + '\u001b[0m' |
| 19 | +} |
| 20 | + |
| 21 | +function getColor() { |
| 22 | + let c = (counter % colors.length) |
| 23 | + counter++ |
| 24 | + return (c === 0) ? 2 : 2+c |
| 25 | +} |
| 26 | + |
| 27 | +function formatError(e) { |
| 28 | + return e.stack.split('\n').map((str, i) => { |
| 29 | + if (i == 0) return e.message |
| 30 | + if (i == 1) return fillColor(str.replace('at', '@'), 0).replace(/\((.*)\)/, fillColor('($1)', 1)) |
| 31 | + return fillColor(str.replace('at', '@'), 0) |
| 32 | + }).join('\n') |
| 33 | +} |
| 34 | + |
| 35 | +const formatters = { |
| 36 | + o: (v) => util.inspect(v, {colors: true}).replace(/\s*\n\s*/g, ' '), //.split('\n').map(str => str.trim()).join(' '), |
| 37 | + O: (v) => (v instanceof Error) ? formatError(v) : util.inspect(v, {colors: true}) |
| 38 | +} |
| 39 | + |
| 40 | +function getState(ns) { |
| 41 | + if (process.env.DEBUG === '*') { |
| 42 | + return true |
| 43 | + } |
| 44 | + |
| 45 | + for (let elm of ignores) { |
| 46 | + if (elm.test(ns)) return false |
| 47 | + } |
| 48 | + |
| 49 | + for (let elm of allows) { |
| 50 | + if (elm.test(ns)) return true |
| 51 | + } |
| 52 | + |
| 53 | + return false |
| 54 | +} |
| 55 | + |
| 56 | +function load(ns) { |
| 57 | + if (!ns) ns = process.env.DEBUG |
| 58 | + |
| 59 | + let ary = (typeof ns === 'string' ? ns : '').split(/[\s,]+/) |
| 60 | + for (let elm of ary) { |
| 61 | + if (!elm) continue |
| 62 | + ns = elm.replace(/\*/g, '.*?') |
| 63 | + if (ns[0] === '-') { |
| 64 | + ignores.push(new RegExp('^' + ns.slice(1) + '$')) |
| 65 | + } else { |
| 66 | + allows.push(new RegExp('^' + ns + '$')) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function Debug(namespace) { |
| 72 | + this._namespace = namespace |
| 73 | + this._color = getColor() |
| 74 | + this._previous = null |
| 75 | + this._enabled = getState(namespace) |
| 76 | + return this.debug.bind(this) |
| 77 | +} |
| 78 | + |
| 79 | +Debug.prototype.debug = function(...args) { |
| 80 | + if (!this._enabled || args.length === 0) return |
| 81 | + |
| 82 | + const now = Date.now() |
| 83 | + const duration = now - (this._previous || now) |
| 84 | + this._previous = now |
| 85 | + |
| 86 | + // Parse and reformat messages |
| 87 | + if (typeof args[0] !== 'string') { |
| 88 | + args.unshift('%O') |
| 89 | + } else if (args[1] instanceof Error && args[0].indexOf('%O') == -1) { |
| 90 | + args[0] = args[0].trim() + ' %O' |
| 91 | + } |
| 92 | + |
| 93 | + let idx = 0 |
| 94 | + args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, flag) => { |
| 95 | + if (match === '%%') return match |
| 96 | + idx++ |
| 97 | + if (typeof formatters[flag] == 'function' && args[idx]) { |
| 98 | + match = formatters[flag](args[idx]) |
| 99 | + args.splice(idx, 1) |
| 100 | + idx-- |
| 101 | + } |
| 102 | + return match |
| 103 | + }) |
| 104 | + |
| 105 | + const prefix = fillColor(this._namespace, this._color) |
| 106 | + const suffix = '\u001b[3' + this._color + 'm' + '+' + ms(duration) + '\u001b[0m' |
| 107 | + |
| 108 | + args[0] = ' ' + prefix + ' ' +args[0].split('\n').join('\n ' + prefix) |
| 109 | + args.push(suffix) |
| 110 | + |
| 111 | + // Output |
| 112 | + process.stdout.write(util.format(...args) + '\n'); |
| 113 | +} |
| 114 | + |
| 115 | +load() |
| 116 | + |
| 117 | +module.exports = (namespace) => new Debug(namespace) |
0 commit comments