[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const LogType = Object.freeze({
|
---|
| 9 | error: /** @type {"error"} */ ("error"), // message, c style arguments
|
---|
| 10 | warn: /** @type {"warn"} */ ("warn"), // message, c style arguments
|
---|
| 11 | info: /** @type {"info"} */ ("info"), // message, c style arguments
|
---|
| 12 | log: /** @type {"log"} */ ("log"), // message, c style arguments
|
---|
| 13 | debug: /** @type {"debug"} */ ("debug"), // message, c style arguments
|
---|
| 14 |
|
---|
| 15 | trace: /** @type {"trace"} */ ("trace"), // no arguments
|
---|
| 16 |
|
---|
| 17 | group: /** @type {"group"} */ ("group"), // [label]
|
---|
| 18 | groupCollapsed: /** @type {"groupCollapsed"} */ ("groupCollapsed"), // [label]
|
---|
| 19 | groupEnd: /** @type {"groupEnd"} */ ("groupEnd"), // [label]
|
---|
| 20 |
|
---|
| 21 | profile: /** @type {"profile"} */ ("profile"), // [profileName]
|
---|
| 22 | profileEnd: /** @type {"profileEnd"} */ ("profileEnd"), // [profileName]
|
---|
| 23 |
|
---|
| 24 | time: /** @type {"time"} */ ("time"), // name, time as [seconds, nanoseconds]
|
---|
| 25 |
|
---|
| 26 | clear: /** @type {"clear"} */ ("clear"), // no arguments
|
---|
| 27 | status: /** @type {"status"} */ ("status") // message, arguments
|
---|
| 28 | });
|
---|
| 29 |
|
---|
| 30 | module.exports.LogType = LogType;
|
---|
| 31 |
|
---|
| 32 | /** @typedef {typeof LogType[keyof typeof LogType]} LogTypeEnum */
|
---|
| 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 | * @param {function(LogTypeEnum, EXPECTED_ANY[]=): void} log log function
|
---|
| 41 | * @param {function(string | function(): string): WebpackLogger} getChildLogger function to create child logger
|
---|
| 42 | */
|
---|
| 43 | constructor(log, getChildLogger) {
|
---|
| 44 | this[LOG_SYMBOL] = log;
|
---|
| 45 | this.getChildLogger = getChildLogger;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @param {...EXPECTED_ANY} args args
|
---|
| 50 | */
|
---|
| 51 | error(...args) {
|
---|
| 52 | this[LOG_SYMBOL](LogType.error, args);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * @param {...EXPECTED_ANY} args args
|
---|
| 57 | */
|
---|
| 58 | warn(...args) {
|
---|
| 59 | this[LOG_SYMBOL](LogType.warn, args);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * @param {...EXPECTED_ANY} args args
|
---|
| 64 | */
|
---|
| 65 | info(...args) {
|
---|
| 66 | this[LOG_SYMBOL](LogType.info, args);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * @param {...EXPECTED_ANY} args args
|
---|
| 71 | */
|
---|
| 72 | log(...args) {
|
---|
| 73 | this[LOG_SYMBOL](LogType.log, args);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * @param {...EXPECTED_ANY} args args
|
---|
| 78 | */
|
---|
| 79 | debug(...args) {
|
---|
| 80 | this[LOG_SYMBOL](LogType.debug, args);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * @param {EXPECTED_ANY} assertion assertion
|
---|
| 85 | * @param {...EXPECTED_ANY} args args
|
---|
| 86 | */
|
---|
| 87 | assert(assertion, ...args) {
|
---|
| 88 | if (!assertion) {
|
---|
| 89 | this[LOG_SYMBOL](LogType.error, args);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | trace() {
|
---|
| 94 | this[LOG_SYMBOL](LogType.trace, ["Trace"]);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | clear() {
|
---|
| 98 | this[LOG_SYMBOL](LogType.clear);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * @param {...EXPECTED_ANY} args args
|
---|
| 103 | */
|
---|
| 104 | status(...args) {
|
---|
| 105 | this[LOG_SYMBOL](LogType.status, args);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * @param {...EXPECTED_ANY} args args
|
---|
| 110 | */
|
---|
| 111 | group(...args) {
|
---|
| 112 | this[LOG_SYMBOL](LogType.group, args);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * @param {...EXPECTED_ANY} args args
|
---|
| 117 | */
|
---|
| 118 | groupCollapsed(...args) {
|
---|
| 119 | this[LOG_SYMBOL](LogType.groupCollapsed, args);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | groupEnd() {
|
---|
| 123 | this[LOG_SYMBOL](LogType.groupEnd);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * @param {string=} label label
|
---|
| 128 | */
|
---|
| 129 | profile(label) {
|
---|
| 130 | this[LOG_SYMBOL](LogType.profile, [label]);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @param {string=} label label
|
---|
| 135 | */
|
---|
| 136 | profileEnd(label) {
|
---|
| 137 | this[LOG_SYMBOL](LogType.profileEnd, [label]);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * @param {string} label label
|
---|
| 142 | */
|
---|
| 143 | time(label) {
|
---|
| 144 | /** @type {Map<string | undefined, [number, number]>} */
|
---|
| 145 | this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
|
---|
| 146 | this[TIMERS_SYMBOL].set(label, process.hrtime());
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * @param {string=} label label
|
---|
| 151 | */
|
---|
| 152 | timeLog(label) {
|
---|
| 153 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
---|
| 154 | if (!prev) {
|
---|
| 155 | throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
|
---|
| 156 | }
|
---|
| 157 | const time = process.hrtime(prev);
|
---|
| 158 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * @param {string=} label label
|
---|
| 163 | */
|
---|
| 164 | timeEnd(label) {
|
---|
| 165 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
---|
| 166 | if (!prev) {
|
---|
| 167 | throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
|
---|
| 168 | }
|
---|
| 169 | const time = process.hrtime(prev);
|
---|
| 170 | /** @type {Map<string | undefined, [number, number]>} */
|
---|
| 171 | (this[TIMERS_SYMBOL]).delete(label);
|
---|
| 172 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * @param {string=} label label
|
---|
| 177 | */
|
---|
| 178 | timeAggregate(label) {
|
---|
| 179 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
---|
| 180 | if (!prev) {
|
---|
| 181 | throw new Error(
|
---|
| 182 | `No such label '${label}' for WebpackLogger.timeAggregate()`
|
---|
| 183 | );
|
---|
| 184 | }
|
---|
| 185 | const time = process.hrtime(prev);
|
---|
| 186 | /** @type {Map<string | undefined, [number, number]>} */
|
---|
| 187 | (this[TIMERS_SYMBOL]).delete(label);
|
---|
| 188 | /** @type {Map<string | undefined, [number, number]>} */
|
---|
| 189 | this[TIMERS_AGGREGATES_SYMBOL] =
|
---|
| 190 | this[TIMERS_AGGREGATES_SYMBOL] || new Map();
|
---|
| 191 | const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
---|
| 192 | if (current !== undefined) {
|
---|
| 193 | if (time[1] + current[1] > 1e9) {
|
---|
| 194 | time[0] += current[0] + 1;
|
---|
| 195 | time[1] = time[1] - 1e9 + current[1];
|
---|
| 196 | } else {
|
---|
| 197 | time[0] += current[0];
|
---|
| 198 | time[1] += current[1];
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
| 205 | * @param {string=} label label
|
---|
| 206 | */
|
---|
| 207 | timeAggregateEnd(label) {
|
---|
| 208 | if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
|
---|
| 209 | const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
---|
| 210 | if (time === undefined) return;
|
---|
| 211 | this[TIMERS_AGGREGATES_SYMBOL].delete(label);
|
---|
| 212 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | module.exports.Logger = WebpackLogger;
|
---|