buffered_console.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _assert;
  6. function _load_assert() {
  7. return _assert = _interopRequireDefault(require('assert'));
  8. }
  9. var _console;
  10. function _load_console() {
  11. return _console = require('console');
  12. }
  13. var _util;
  14. function _load_util() {
  15. return _util = require('util');
  16. }
  17. var _chalk;
  18. function _load_chalk() {
  19. return _chalk = _interopRequireDefault(require('chalk'));
  20. }
  21. var _get_callsite;
  22. function _load_get_callsite() {
  23. return _get_callsite = _interopRequireDefault(require('./get_callsite'));
  24. }
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. class BufferedConsole extends (_console || _load_console()).Console {
  27. constructor(getSourceMaps) {
  28. const buffer = [];
  29. super({
  30. write: message => BufferedConsole.write(buffer, 'log', message, null, getSourceMaps())
  31. });
  32. this._getSourceMaps = getSourceMaps;
  33. this._buffer = buffer;
  34. this._counters = {};
  35. this._timers = {};
  36. this._groupDepth = 0;
  37. }
  38. static write(buffer, type, message, level, sourceMaps) {
  39. const callsite = (0, (_get_callsite || _load_get_callsite()).default)(level != null ? level : 2, sourceMaps);
  40. const origin = callsite.getFileName() + ':' + callsite.getLineNumber();
  41. buffer.push({
  42. message,
  43. origin,
  44. type
  45. });
  46. return buffer;
  47. }
  48. _log(type, message) {
  49. BufferedConsole.write(this._buffer, type, ' '.repeat(this._groupDepth) + message, 3, this._getSourceMaps());
  50. }
  51. assert() {
  52. try {
  53. (_assert || _load_assert()).default.apply(undefined, arguments);
  54. } catch (error) {
  55. this._log('assert', error.toString());
  56. }
  57. }
  58. count() {
  59. let label = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
  60. if (!this._counters[label]) {
  61. this._counters[label] = 0;
  62. }
  63. this._log('count', (0, (_util || _load_util()).format)(`${label}: ${++this._counters[label]}`));
  64. }
  65. countReset() {
  66. let label = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
  67. this._counters[label] = 0;
  68. }
  69. debug() {
  70. this._log('debug', (_util || _load_util()).format.apply(undefined, arguments));
  71. }
  72. dir() {
  73. this._log('dir', (_util || _load_util()).format.apply(undefined, arguments));
  74. }
  75. dirxml() {
  76. this._log('dirxml', (_util || _load_util()).format.apply(undefined, arguments));
  77. }
  78. error() {
  79. this._log('error', (_util || _load_util()).format.apply(undefined, arguments));
  80. }
  81. group() {
  82. this._groupDepth++;
  83. if (arguments.length > 0) {
  84. this._log('group', (_chalk || _load_chalk()).default.bold((_util || _load_util()).format.apply(undefined, arguments)));
  85. }
  86. }
  87. groupCollapsed() {
  88. this._groupDepth++;
  89. if (arguments.length > 0) {
  90. this._log('groupCollapsed', (_chalk || _load_chalk()).default.bold((_util || _load_util()).format.apply(undefined, arguments)));
  91. }
  92. }
  93. groupEnd() {
  94. if (this._groupDepth > 0) {
  95. this._groupDepth--;
  96. }
  97. }
  98. info() {
  99. this._log('info', (_util || _load_util()).format.apply(undefined, arguments));
  100. }
  101. log() {
  102. this._log('log', (_util || _load_util()).format.apply(undefined, arguments));
  103. }
  104. time() {
  105. let label = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
  106. if (this._timers[label]) {
  107. return;
  108. }
  109. this._timers[label] = new Date();
  110. }
  111. timeEnd() {
  112. let label = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
  113. const startTime = this._timers[label];
  114. if (startTime) {
  115. const endTime = new Date();
  116. const time = (endTime - startTime) / 1000;
  117. this._log('time', (0, (_util || _load_util()).format)(`${label}: ${time}ms`));
  118. delete this._timers[label];
  119. }
  120. }
  121. warn() {
  122. this._log('warn', (_util || _load_util()).format.apply(undefined, arguments));
  123. }
  124. getBuffer() {
  125. return this._buffer;
  126. }
  127. }
  128. exports.default = BufferedConsole; /**
  129. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  130. *
  131. * This source code is licensed under the MIT license found in the
  132. * LICENSE file in the root directory of this source tree.
  133. *
  134. *
  135. */