debugLogger.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.debugLogger = exports.RecentLogsCollector = void 0;
  6. var _utilsBundle = require("../utilsBundle");
  7. var _fs = _interopRequireDefault(require("fs"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * Copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. const debugLoggerColorMap = {
  25. 'api': 45,
  26. // cyan
  27. 'protocol': 34,
  28. // green
  29. 'install': 34,
  30. // green
  31. 'download': 34,
  32. // green
  33. 'browser': 0,
  34. // reset
  35. 'socks': 92,
  36. // purple
  37. 'error': 160,
  38. // red,
  39. 'channel': 33,
  40. // blue
  41. 'server': 45,
  42. // cyan
  43. 'server:channel': 34,
  44. // green
  45. 'server:metadata': 33 // blue
  46. };
  47. class DebugLogger {
  48. constructor() {
  49. this._debuggers = new Map();
  50. if (process.env.DEBUG_FILE) {
  51. const ansiRegex = new RegExp(['[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'].join('|'), 'g');
  52. const stream = _fs.default.createWriteStream(process.env.DEBUG_FILE);
  53. _utilsBundle.debug.log = data => {
  54. stream.write(data.replace(ansiRegex, ''));
  55. stream.write('\n');
  56. };
  57. }
  58. }
  59. log(name, message) {
  60. let cachedDebugger = this._debuggers.get(name);
  61. if (!cachedDebugger) {
  62. cachedDebugger = (0, _utilsBundle.debug)(`pw:${name}`);
  63. this._debuggers.set(name, cachedDebugger);
  64. cachedDebugger.color = debugLoggerColorMap[name] || 0;
  65. }
  66. cachedDebugger(message);
  67. }
  68. isEnabled(name) {
  69. return _utilsBundle.debug.enabled(`pw:${name}`);
  70. }
  71. }
  72. const debugLogger = exports.debugLogger = new DebugLogger();
  73. const kLogCount = 150;
  74. class RecentLogsCollector {
  75. constructor() {
  76. this._logs = [];
  77. }
  78. log(message) {
  79. this._logs.push(message);
  80. if (this._logs.length === kLogCount * 2) this._logs.splice(0, kLogCount);
  81. }
  82. recentLogs() {
  83. if (this._logs.length > kLogCount) return this._logs.slice(-kLogCount);
  84. return this._logs;
  85. }
  86. }
  87. exports.RecentLogsCollector = RecentLogsCollector;