You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.1 KiB

4 years ago
  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;