| [9af201e] | 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 | /** @typedef {Map<string | undefined, [number, number]>} TimersMap */
|
|---|
| 34 |
|
|---|
| 35 | const LOG_SYMBOL = Symbol("webpack logger raw log method");
|
|---|
| 36 | const TIMERS_SYMBOL = Symbol("webpack logger times");
|
|---|
| 37 | const TIMERS_AGGREGATES_SYMBOL = Symbol("webpack logger aggregated times");
|
|---|
| 38 |
|
|---|
| 39 | /** @typedef {EXPECTED_ANY[]} Args */
|
|---|
| 40 | /** @typedef {(type: LogTypeEnum, args?: Args) => void} LogFn */
|
|---|
| 41 | /** @typedef {(name: string | (() => string)) => WebpackLogger} GetChildLogger */
|
|---|
| 42 |
|
|---|
| 43 | class WebpackLogger {
|
|---|
| 44 | /**
|
|---|
| 45 | * Creates an instance of WebpackLogger.
|
|---|
| 46 | * @param {LogFn} log log function
|
|---|
| 47 | * @param {GetChildLogger} getChildLogger function to create child logger
|
|---|
| 48 | */
|
|---|
| 49 | constructor(log, getChildLogger) {
|
|---|
| 50 | /** @type {LogFn} */
|
|---|
| 51 | this[LOG_SYMBOL] = log;
|
|---|
| 52 | /** @type {GetChildLogger} */
|
|---|
| 53 | this.getChildLogger = getChildLogger;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Processes the provided arg.
|
|---|
| 58 | * @param {Args} args args
|
|---|
| 59 | */
|
|---|
| 60 | error(...args) {
|
|---|
| 61 | this[LOG_SYMBOL](LogType.error, args);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Processes the provided arg.
|
|---|
| 66 | * @param {Args} args args
|
|---|
| 67 | */
|
|---|
| 68 | warn(...args) {
|
|---|
| 69 | this[LOG_SYMBOL](LogType.warn, args);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Processes the provided arg.
|
|---|
| 74 | * @param {Args} args args
|
|---|
| 75 | */
|
|---|
| 76 | info(...args) {
|
|---|
| 77 | this[LOG_SYMBOL](LogType.info, args);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Processes the provided arg.
|
|---|
| 82 | * @param {Args} args args
|
|---|
| 83 | */
|
|---|
| 84 | log(...args) {
|
|---|
| 85 | this[LOG_SYMBOL](LogType.log, args);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Processes the provided arg.
|
|---|
| 90 | * @param {Args} args args
|
|---|
| 91 | */
|
|---|
| 92 | debug(...args) {
|
|---|
| 93 | this[LOG_SYMBOL](LogType.debug, args);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Processes the provided condition.
|
|---|
| 98 | * @param {boolean=} condition condition
|
|---|
| 99 | * @param {Args} args args
|
|---|
| 100 | */
|
|---|
| 101 | assert(condition, ...args) {
|
|---|
| 102 | if (!condition) {
|
|---|
| 103 | this[LOG_SYMBOL](LogType.error, args);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | trace() {
|
|---|
| 108 | this[LOG_SYMBOL](LogType.trace, ["Trace"]);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | clear() {
|
|---|
| 112 | this[LOG_SYMBOL](LogType.clear);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Processes the provided arg.
|
|---|
| 117 | * @param {Args} args args
|
|---|
| 118 | */
|
|---|
| 119 | status(...args) {
|
|---|
| 120 | this[LOG_SYMBOL](LogType.status, args);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * Processes the provided arg.
|
|---|
| 125 | * @param {Args} args args
|
|---|
| 126 | */
|
|---|
| 127 | group(...args) {
|
|---|
| 128 | this[LOG_SYMBOL](LogType.group, args);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Processes the provided arg.
|
|---|
| 133 | * @param {Args} args args
|
|---|
| 134 | */
|
|---|
| 135 | groupCollapsed(...args) {
|
|---|
| 136 | this[LOG_SYMBOL](LogType.groupCollapsed, args);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | groupEnd() {
|
|---|
| 140 | this[LOG_SYMBOL](LogType.groupEnd);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Processes the provided label.
|
|---|
| 145 | * @param {string=} label label
|
|---|
| 146 | */
|
|---|
| 147 | profile(label) {
|
|---|
| 148 | this[LOG_SYMBOL](LogType.profile, [label]);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * Processes the provided label.
|
|---|
| 153 | * @param {string=} label label
|
|---|
| 154 | */
|
|---|
| 155 | profileEnd(label) {
|
|---|
| 156 | this[LOG_SYMBOL](LogType.profileEnd, [label]);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Processes the provided label.
|
|---|
| 161 | * @param {string} label label
|
|---|
| 162 | */
|
|---|
| 163 | time(label) {
|
|---|
| 164 | /** @type {TimersMap} */
|
|---|
| 165 | this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
|
|---|
| 166 | this[TIMERS_SYMBOL].set(label, process.hrtime());
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Processes the provided label.
|
|---|
| 171 | * @param {string=} label label
|
|---|
| 172 | */
|
|---|
| 173 | timeLog(label) {
|
|---|
| 174 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
|---|
| 175 | if (!prev) {
|
|---|
| 176 | throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
|
|---|
| 177 | }
|
|---|
| 178 | const time = process.hrtime(prev);
|
|---|
| 179 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Processes the provided label.
|
|---|
| 184 | * @param {string=} label label
|
|---|
| 185 | */
|
|---|
| 186 | timeEnd(label) {
|
|---|
| 187 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
|---|
| 188 | if (!prev) {
|
|---|
| 189 | throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
|
|---|
| 190 | }
|
|---|
| 191 | const time = process.hrtime(prev);
|
|---|
| 192 | /** @type {TimersMap} */
|
|---|
| 193 | (this[TIMERS_SYMBOL]).delete(label);
|
|---|
| 194 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Processes the provided label.
|
|---|
| 199 | * @param {string=} label label
|
|---|
| 200 | */
|
|---|
| 201 | timeAggregate(label) {
|
|---|
| 202 | const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
|
|---|
| 203 | if (!prev) {
|
|---|
| 204 | throw new Error(
|
|---|
| 205 | `No such label '${label}' for WebpackLogger.timeAggregate()`
|
|---|
| 206 | );
|
|---|
| 207 | }
|
|---|
| 208 | const time = process.hrtime(prev);
|
|---|
| 209 | /** @type {TimersMap} */
|
|---|
| 210 | (this[TIMERS_SYMBOL]).delete(label);
|
|---|
| 211 | /** @type {TimersMap} */
|
|---|
| 212 | this[TIMERS_AGGREGATES_SYMBOL] =
|
|---|
| 213 | this[TIMERS_AGGREGATES_SYMBOL] || new Map();
|
|---|
| 214 | const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
|---|
| 215 | if (current !== undefined) {
|
|---|
| 216 | if (time[1] + current[1] > 1e9) {
|
|---|
| 217 | time[0] += current[0] + 1;
|
|---|
| 218 | time[1] = time[1] - 1e9 + current[1];
|
|---|
| 219 | } else {
|
|---|
| 220 | time[0] += current[0];
|
|---|
| 221 | time[1] += current[1];
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 | this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Time aggregate end.
|
|---|
| 229 | * @param {string=} label label
|
|---|
| 230 | */
|
|---|
| 231 | timeAggregateEnd(label) {
|
|---|
| 232 | if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
|
|---|
| 233 | const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
|
|---|
| 234 | if (time === undefined) return;
|
|---|
| 235 | this[TIMERS_AGGREGATES_SYMBOL].delete(label);
|
|---|
| 236 | this[LOG_SYMBOL](LogType.time, [label, ...time]);
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | module.exports.Logger = WebpackLogger;
|
|---|