Logger.js 5.2 KB

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