Logger.js
3.08 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
119
120
121
122
123
124
125
126
127
128
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const LogType = Object.freeze({
error: /** @type {"error"} */ ("error"), // message, c style arguments
warn: /** @type {"warn"} */ ("warn"), // message, c style arguments
info: /** @type {"info"} */ ("info"), // message, c style arguments
log: /** @type {"log"} */ ("log"), // message, c style arguments
debug: /** @type {"debug"} */ ("debug"), // message, c style arguments
trace: /** @type {"trace"} */ ("trace"), // no arguments
group: /** @type {"group"} */ ("group"), // [label]
groupCollapsed: /** @type {"groupCollapsed"} */ ("groupCollapsed"), // [label]
groupEnd: /** @type {"groupEnd"} */ ("groupEnd"), // [label]
profile: /** @type {"profile"} */ ("profile"), // [profileName]
profileEnd: /** @type {"profileEnd"} */ ("profileEnd"), // [profileName]
time: /** @type {"time"} */ ("time"), // name, time as [seconds, nanoseconds]
clear: /** @type {"clear"} */ ("clear"), // no arguments
status: /** @type {"status"} */ ("status") // message, arguments
});
exports.LogType = LogType;
/** @typedef {typeof LogType[keyof typeof LogType]} LogTypeEnum */
const LOG_SYMBOL = Symbol("webpack logger raw log method");
const TIMERS_SYMBOL = Symbol("webpack logger times");
class WebpackLogger {
/**
* @param {function(LogTypeEnum, any[]=): void} log log function
*/
constructor(log) {
this[LOG_SYMBOL] = log;
}
error(...args) {
this[LOG_SYMBOL](LogType.error, args);
}
warn(...args) {
this[LOG_SYMBOL](LogType.warn, args);
}
info(...args) {
this[LOG_SYMBOL](LogType.info, args);
}
log(...args) {
this[LOG_SYMBOL](LogType.log, args);
}
debug(...args) {
this[LOG_SYMBOL](LogType.debug, args);
}
assert(assertion, ...args) {
if (!assertion) {
this[LOG_SYMBOL](LogType.error, args);
}
}
trace() {
this[LOG_SYMBOL](LogType.trace, ["Trace"]);
}
clear() {
this[LOG_SYMBOL](LogType.clear);
}
status(...args) {
this[LOG_SYMBOL](LogType.status, args);
}
group(...args) {
this[LOG_SYMBOL](LogType.group, args);
}
groupCollapsed(...args) {
this[LOG_SYMBOL](LogType.groupCollapsed, args);
}
groupEnd(...args) {
this[LOG_SYMBOL](LogType.groupEnd, args);
}
profile(label) {
this[LOG_SYMBOL](LogType.profile, [label]);
}
profileEnd(label) {
this[LOG_SYMBOL](LogType.profileEnd, [label]);
}
time(label) {
this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
this[TIMERS_SYMBOL].set(label, process.hrtime());
}
timeLog(label) {
const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
if (!prev) {
throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
}
const time = process.hrtime(prev);
this[LOG_SYMBOL](LogType.time, [label, ...time]);
}
timeEnd(label) {
const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
if (!prev) {
throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
}
const time = process.hrtime(prev);
this[TIMERS_SYMBOL].delete(label);
this[LOG_SYMBOL](LogType.time, [label, ...time]);
}
}
exports.Logger = WebpackLogger;