1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const LogType = Object.freeze({
|
9 | error: ("error"),
|
10 | warn: ("warn"),
|
11 | info: ("info"),
|
12 | log: ("log"),
|
13 | debug: ("debug"),
|
14 |
|
15 | trace: ("trace"),
|
16 |
|
17 | group: ("group"),
|
18 | groupCollapsed: ("groupCollapsed"),
|
19 | groupEnd: ("groupEnd"),
|
20 |
|
21 | profile: ("profile"),
|
22 | profileEnd: ("profileEnd"),
|
23 |
|
24 | time: ("time"),
|
25 |
|
26 | clear: ("clear"),
|
27 | status: ("status")
|
28 | });
|
29 |
|
30 | exports.LogType = LogType;
|
31 |
|
32 |
|
33 |
|
34 | const LOG_SYMBOL = Symbol("webpack logger raw log method");
|
35 | const TIMERS_SYMBOL = Symbol("webpack logger times");
|
36 | const TIMERS_AGGREGATES_SYMBOL = Symbol("webpack logger aggregated times");
|
37 |
|
38 | class WebpackLogger {
|
39 | |
40 |
|
41 |
|
42 |
|
43 | constructor(log, getChildLogger) {
|
44 | this[LOG_SYMBOL] = log;
|
45 | this.getChildLogger = getChildLogger;
|
46 | }
|
47 |
|
48 | error(...args) {
|
49 | this[LOG_SYMBOL](LogType.error, args);
|
50 | }
|
51 |
|
52 | warn(...args) {
|
53 | this[LOG_SYMBOL](LogType.warn, args);
|
54 | }
|
55 |
|
56 | info(...args) {
|
57 | this[LOG_SYMBOL](LogType.info, args);
|
58 | }
|
59 |
|
60 | log(...args) {
|
61 | this[LOG_SYMBOL](LogType.log, args);
|
62 | }
|
63 |
|
64 | debug(...args) {
|
65 | this[LOG_SYMBOL](LogType.debug, args);
|
66 | }
|
67 |
|
68 | assert(assertion, ...args) {
|
69 | if (!assertion) {
|
70 | this[LOG_SYMBOL](LogType.error, args);
|
71 | }
|
72 | }
|
73 |
|
74 | trace() {
|
75 | this[LOG_SYMBOL](LogType.trace, ["Trace"]);
|
76 | }
|
77 |
|
78 | clear() {
|
79 | this[LOG_SYMBOL](LogType.clear);
|
80 | }
|
81 |
|
82 | status(...args) {
|
83 | this[LOG_SYMBOL](LogType.status, args);
|
84 | }
|
85 |
|
86 | group(...args) {
|
87 | this[LOG_SYMBOL](LogType.group, args);
|
88 | }
|
89 |
|
90 | groupCollapsed(...args) {
|
91 | this[LOG_SYMBOL](LogType.groupCollapsed, args);
|
92 | }
|
93 |
|
94 | groupEnd(...args) {
|
95 | this[LOG_SYMBOL](LogType.groupEnd, args);
|
96 | }
|
97 |
|
98 | profile(label) {
|
99 | this[LOG_SYMBOL](LogType.profile, [label]);
|
100 | }
|
101 |
|
102 | profileEnd(label) {
|
103 | this[LOG_SYMBOL](LogType.profileEnd, [label]);
|
104 | }
|
105 |
|
106 | time(label) {
|
107 | this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
|
108 | this[TIMERS_SYMBOL].set(label, process.hrtime());
|
109 | }
|
110 |
|
111 | timeLog(label) {
|
112 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
113 | if (!prev) {
|
114 | throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
|
115 | }
|
116 | const time = process.hrtime(prev);
|
117 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
118 | }
|
119 |
|
120 | timeEnd(label) {
|
121 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
122 | if (!prev) {
|
123 | throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
|
124 | }
|
125 | const time = process.hrtime(prev);
|
126 | this[TIMERS_SYMBOL].delete(label);
|
127 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
128 | }
|
129 |
|
130 | timeAggregate(label) {
|
131 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
132 | if (!prev) {
|
133 | throw new Error(
|
134 | `No such label '${label}' for WebpackLogger.timeAggregate()`
|
135 | );
|
136 | }
|
137 | const time = process.hrtime(prev);
|
138 | this[TIMERS_SYMBOL].delete(label);
|
139 | this[TIMERS_AGGREGATES_SYMBOL] =
|
140 | this[TIMERS_AGGREGATES_SYMBOL] || new Map();
|
141 | const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
142 | if (current !== undefined) {
|
143 | if (time[1] + current[1] > 1e9) {
|
144 | time[0] += current[0] + 1;
|
145 | time[1] = time[1] - 1e9 + current[1];
|
146 | } else {
|
147 | time[0] += current[0];
|
148 | time[1] += current[1];
|
149 | }
|
150 | }
|
151 | this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
|
152 | }
|
153 |
|
154 | timeAggregateEnd(label) {
|
155 | if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
|
156 | const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
157 | if (time === undefined) return;
|
158 | this[TIMERS_AGGREGATES_SYMBOL].delete(label);
|
159 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
160 | }
|
161 | }
|
162 |
|
163 | exports.Logger = WebpackLogger;
|