Logger.js 4.8 KB

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