Logger.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. class WebpackLogger {
  27. /**
  28. * @param {function(LogTypeEnum, any[]=): void} log log function
  29. */
  30. constructor(log) {
  31. this[LOG_SYMBOL] = log;
  32. }
  33. error(...args) {
  34. this[LOG_SYMBOL](LogType.error, args);
  35. }
  36. warn(...args) {
  37. this[LOG_SYMBOL](LogType.warn, args);
  38. }
  39. info(...args) {
  40. this[LOG_SYMBOL](LogType.info, args);
  41. }
  42. log(...args) {
  43. this[LOG_SYMBOL](LogType.log, args);
  44. }
  45. debug(...args) {
  46. this[LOG_SYMBOL](LogType.debug, args);
  47. }
  48. assert(assertion, ...args) {
  49. if (!assertion) {
  50. this[LOG_SYMBOL](LogType.error, args);
  51. }
  52. }
  53. trace() {
  54. this[LOG_SYMBOL](LogType.trace, ["Trace"]);
  55. }
  56. clear() {
  57. this[LOG_SYMBOL](LogType.clear);
  58. }
  59. status(...args) {
  60. this[LOG_SYMBOL](LogType.status, args);
  61. }
  62. group(...args) {
  63. this[LOG_SYMBOL](LogType.group, args);
  64. }
  65. groupCollapsed(...args) {
  66. this[LOG_SYMBOL](LogType.groupCollapsed, args);
  67. }
  68. groupEnd(...args) {
  69. this[LOG_SYMBOL](LogType.groupEnd, args);
  70. }
  71. profile(label) {
  72. this[LOG_SYMBOL](LogType.profile, [label]);
  73. }
  74. profileEnd(label) {
  75. this[LOG_SYMBOL](LogType.profileEnd, [label]);
  76. }
  77. time(label) {
  78. this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
  79. this[TIMERS_SYMBOL].set(label, process.hrtime());
  80. }
  81. timeLog(label) {
  82. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  83. if (!prev) {
  84. throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
  85. }
  86. const time = process.hrtime(prev);
  87. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  88. }
  89. timeEnd(label) {
  90. const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
  91. if (!prev) {
  92. throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
  93. }
  94. const time = process.hrtime(prev);
  95. this[TIMERS_SYMBOL].delete(label);
  96. this[LOG_SYMBOL](LogType.time, [label, ...time]);
  97. }
  98. }
  99. exports.Logger = WebpackLogger;