[6a3a178] | 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 | 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, 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 | 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[LOG_SYMBOL](LogType.time, [label, ...time]);
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | exports.Logger = WebpackLogger;
|
---|