-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (100 loc) · 3.69 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
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
119
120
121
122
123
124
125
126
127
import {shouldLog as _shouldLog, validateScope, getColor, format, stripAnsi} from './src/util.js';
import {EventEmitter} from 'events';
import defaultConfig from './src/default-config.js';
const RESET_CODE = '\x1b[0m';
let config = defaultConfig;
const debugs = [];
const color = getColor(config)();
const shouldLog = _shouldLog(config);
function logHandler(scope, debugs, debugId, kolor) {
const emit = (what, ...args) => ret.events.emit(what, ...args);
let lastCallTime = new Date().getTime();
function ret(...message) {
const now = new Date().getTime();
const msDiff = now - lastCallTime;
const msDiffString = `${kolor}+${msDiff}ms${RESET_CODE}`;
const text = format(config.inspectOptions, ...message);
if (shouldLog(scope)) {
const debugPrefix = debugs[debugId];
text.split('\n').forEach(
(line, index) => console.debug(
!index ? debugPrefix : ' '.repeat(stripAnsi(debugPrefix).length),
line,
!index ? msDiffString : ''
)
);
emit('log', scope, text);
} else emit('logskip', scope, text);
}
ret.extend = nestedDebug(debugId, debugs);
ret.events = new EventEmitter();
ret.enabled = () => shouldLog(scope);
/* Time handling */
let timerCallTime = null;
let timerMarkTime = null;
ret.time = text => {
if (shouldLog(scope)) {
timerCallTime = new Date().getTime();
text.split('\n').forEach(
(line, index) => console.debug(
!index ? debugs[debugId] + ' [begin time]' : ' '.repeat(stripAnsi(debugs[debugId]).length + 13),
line
)
);
emit('timestart', scope, text, timerCallTime);
} else emit('timestartskip', scope, text, timerCallTime);
};
ret.markTime = text => {
if (timerCallTime === null) return;
if (shouldLog(scope)) {
const prevMarkTime = timerMarkTime;
timerMarkTime = new Date().getTime();
const previousTimeDiff = timerMarkTime - timerCallTime;
const previousMarkDiff = prevMarkTime && (timerMarkTime - prevMarkTime);
text.split('\n').forEach(
(line, index) => console.debug(
!index ? debugs[debugId] + ' [mark time]' : ' '.repeat(stripAnsi(debugs[debugId]).length + 12),
line,
!index ? `${kolor}+${previousTimeDiff}ms total${previousMarkDiff ? `, +${previousMarkDiff}ms since last mark` : ''}${RESET_CODE}` : ''
)
);
emit('timemark', scope, text, timerMarkTime);
} else emit('timemarkskip', scope, text, timerMarkTime);
}
ret.endTime = text => {
if (timerCallTime === null) return;
let endTime = new Date().getTime();
if (shouldLog(scope)) {
const prevMarkTime = timerMarkTime;
const firstCallTime = timerCallTime;
timerMarkTime = timerCallTime = null;
const previousTimeDiff = endTime - firstCallTime;
const previousMarkDiff = prevMarkTime && (endTime - prevMarkTime);
text.split('\n').forEach(
(line, index) => console.debug(
!index ? debugs[debugId] + ' [end time]' : ' '.repeat(stripAnsi(debugs[debugId]).length + 11),
line,
!index ? `${kolor}+${previousTimeDiff}ms total${previousMarkDiff ? `, +${previousMarkDiff}ms since last mark` : ''}${RESET_CODE}` : ''
)
);
emit('timeend', scope, text, endTime);
} else emit('timeendskip', scope, text, endTime);
}
return ret;
}
function createDebug(array, prefix = '') {
return function (scope) {
validateScope(scope);
const kolor = config.useColor ? color.next().value : '';
array.push(prefix + kolor + scope + RESET_CODE);
const debugId = array.length - 1;
return logHandler(prefix + scope, array, debugId, kolor);
}
}
function nestedDebug(debugId, debugs) {
const prefix = debugs[debugId] + ':';
const moreDebugs = [];
return createDebug(moreDebugs, prefix);
}
export default createDebug(debugs);
export const configure = conf => config = Object.assign(config, conf);