Console.js 3.8 KB

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