Logger.js 5.3 KB

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