Logger.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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, EXPECTED_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. /**
  37. * @param {...EXPECTED_ANY} args args
  38. */
  39. error(...args) {
  40. this[LOG_SYMBOL](LogType.error, args);
  41. }
  42. /**
  43. * @param {...EXPECTED_ANY} args args
  44. */
  45. warn(...args) {
  46. this[LOG_SYMBOL](LogType.warn, args);
  47. }
  48. /**
  49. * @param {...EXPECTED_ANY} args args
  50. */
  51. info(...args) {
  52. this[LOG_SYMBOL](LogType.info, args);
  53. }
  54. /**
  55. * @param {...EXPECTED_ANY} args args
  56. */
  57. log(...args) {
  58. this[LOG_SYMBOL](LogType.log, args);
  59. }
  60. /**
  61. * @param {...EXPECTED_ANY} args args
  62. */
  63. debug(...args) {
  64. this[LOG_SYMBOL](LogType.debug, args);
  65. }
  66. /**
  67. * @param {EXPECTED_ANY} assertion assertion
  68. * @param {...EXPECTED_ANY} args args
  69. */
  70. assert(assertion, ...args) {
  71. if (!assertion) {
  72. this[LOG_SYMBOL](LogType.error, args);
  73. }
  74. }
  75. trace() {
  76. this[LOG_SYMBOL](LogType.trace, ["Trace"]);
  77. }
  78. clear() {
  79. this[LOG_SYMBOL](LogType.clear);
  80. }
  81. /**
  82. * @param {...EXPECTED_ANY} args args
  83. */
  84. status(...args) {
  85. this[LOG_SYMBOL](LogType.status, args);
  86. }
  87. /**
  88. * @param {...EXPECTED_ANY} args args
  89. */
  90. group(...args) {
  91. this[LOG_SYMBOL](LogType.group, args);
  92. }
  93. /**
  94. * @param {...EXPECTED_ANY} args args
  95. */
  96. groupCollapsed(...args) {
  97. this[LOG_SYMBOL](LogType.groupCollapsed, args);
  98. }
  99. groupEnd() {
  100. this[LOG_SYMBOL](LogType.groupEnd);
  101. }
  102. /**
  103. * @param {string=} label label
  104. */
  105. profile(label) {
  106. this[LOG_SYMBOL](LogType.profile, [label]);
  107. }
  108. /**
  109. * @param {string=} label label
  110. */
  111. profileEnd(label) {
  112. this[LOG_SYMBOL](LogType.profileEnd, [label]);
  113. }
  114. /**
  115. * @param {string} label label
  116. */
  117. time(label) {
  118. /** @type {Map<string | undefined, [number, number]>} */
  119. this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
  120. this[TIMERS_SYMBOL].set(label, process.hrtime());
  121. }
  122. /**
  123. * @param {string=} label label
  124. */
  125. timeLog(label) {
  126. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  127. if (!prev) {
  128. throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
  129. }
  130. const time = process.hrtime(prev);
  131. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  132. }
  133. /**
  134. * @param {string=} label label
  135. */
  136. timeEnd(label) {
  137. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  138. if (!prev) {
  139. throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
  140. }
  141. const time = process.hrtime(prev);
  142. /** @type {Map<string | undefined, [number, number]>} */
  143. (this[TIMERS_SYMBOL]).delete(label);
  144. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  145. }
  146. /**
  147. * @param {string=} label label
  148. */
  149. timeAggregate(label) {
  150. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  151. if (!prev) {
  152. throw new Error(
  153. `No such label '${label}' for WebpackLogger.timeAggregate()`
  154. );
  155. }
  156. const time = process.hrtime(prev);
  157. /** @type {Map<string | undefined, [number, number]>} */
  158. (this[TIMERS_SYMBOL]).delete(label);
  159. /** @type {Map<string | undefined, [number, number]>} */
  160. this[TIMERS_AGGREGATES_SYMBOL] =
  161. this[TIMERS_AGGREGATES_SYMBOL] || new Map();
  162. const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
  163. if (current !== undefined) {
  164. if (time[1] + current[1] > 1e9) {
  165. time[0] += current[0] + 1;
  166. time[1] = time[1] - 1e9 + current[1];
  167. } else {
  168. time[0] += current[0];
  169. time[1] += current[1];
  170. }
  171. }
  172. this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
  173. }
  174. /**
  175. * @param {string=} label label
  176. */
  177. timeAggregateEnd(label) {
  178. if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
  179. const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
  180. if (time === undefined) return;
  181. this[TIMERS_AGGREGATES_SYMBOL].delete(label);
  182. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  183. }
  184. }
  185. module.exports.Logger = WebpackLogger;