選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Logger.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @enum {string}
  8. */
  9. const LogType = Object.freeze({
  10. error: "error", // message, c style arguments
  11. warn: "warn", // message, c style arguments
  12. info: "info", // message, c style arguments
  13. log: "log", // message, c style arguments
  14. debug: "debug", // message, c style arguments
  15. trace: "trace", // no arguments
  16. group: "group", // [label]
  17. groupCollapsed: "groupCollapsed", // [label]
  18. groupEnd: "groupEnd", // [label]
  19. profile: "profile", // [profileName]
  20. profileEnd: "profileEnd", // [profileName]
  21. time: "time", // name, time as [seconds, nanoseconds]
  22. clear: "clear", // no arguments
  23. status: "status" // message, arguments
  24. });
  25. exports.LogType = LogType;
  26. /** @typedef {keyof LogType} LogTypeEnum */
  27. const LOG_SYMBOL = Symbol("webpack logger raw log method");
  28. const TIMERS_SYMBOL = Symbol("webpack logger times");
  29. class WebpackLogger {
  30. /**
  31. * @param {function(LogTypeEnum, any[]=): void} log log function
  32. */
  33. constructor(log) {
  34. this[LOG_SYMBOL] = log;
  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. }
  102. exports.Logger = WebpackLogger;