Logger.js 5.8 KB

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