Logger.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. profile(label) {
  75. this[LOG_SYMBOL](LogType.profile, [label]);
  76. }
  77. profileEnd(label) {
  78. this[LOG_SYMBOL](LogType.profileEnd, [label]);
  79. }
  80. time(label) {
  81. this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
  82. this[TIMERS_SYMBOL].set(label, process.hrtime());
  83. }
  84. timeLog(label) {
  85. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  86. if (!prev) {
  87. throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
  88. }
  89. const time = process.hrtime(prev);
  90. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  91. }
  92. timeEnd(label) {
  93. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  94. if (!prev) {
  95. throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
  96. }
  97. const time = process.hrtime(prev);
  98. this[TIMERS_SYMBOL].delete(label);
  99. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  100. }
  101. timeAggregate(label) {
  102. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  103. if (!prev) {
  104. throw new Error(
  105. `No such label '${label}' for WebpackLogger.timeAggregate()`
  106. );
  107. }
  108. const time = process.hrtime(prev);
  109. this[TIMERS_SYMBOL].delete(label);
  110. this[TIMERS_AGGREGATES_SYMBOL] =
  111. this[TIMERS_AGGREGATES_SYMBOL] || new Map();
  112. const current = this[TIMERS_AGGREGATES_SYMBOL].get(label);
  113. if (current !== undefined) {
  114. if (time[1] + current[1] > 1e9) {
  115. time[0] += current[0] + 1;
  116. time[1] = time[1] - 1e9 + current[1];
  117. } else {
  118. time[0] += current[0];
  119. time[1] += current[1];
  120. }
  121. }
  122. this[TIMERS_AGGREGATES_SYMBOL].set(label, time);
  123. }
  124. timeAggregateEnd(label) {
  125. if (this[TIMERS_AGGREGATES_SYMBOL] === undefined) return;
  126. const time = this[TIMERS_AGGREGATES_SYMBOL].get(label);
  127. if (time === undefined) return;
  128. this[TIMERS_AGGREGATES_SYMBOL].delete(label);
  129. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  130. }
  131. }
  132. exports.Logger = WebpackLogger;