Console.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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) {
  26. return obj && obj.__esModule ? obj : {default: obj};
  27. }
  28. /**
  29. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  30. *
  31. * This source code is licensed under the MIT license found in the
  32. * LICENSE file in the root directory of this source tree.
  33. *
  34. *
  35. */
  36. /* global stream$Writable */
  37. class CustomConsole extends (_console || _load_console()).Console {
  38. constructor(stdout, stderr, formatBuffer) {
  39. super(stdout, stderr);
  40. this._formatBuffer = formatBuffer || ((type, message) => message);
  41. this._counters = {};
  42. this._timers = {};
  43. this._groupDepth = 0;
  44. }
  45. _logToParentConsole(message) {
  46. super.log(message);
  47. }
  48. _log(type, message) {
  49. (0, (_clear_line || _load_clear_line()).default)(this._stdout);
  50. this._logToParentConsole(
  51. this._formatBuffer(type, ' '.repeat(this._groupDepth) + message)
  52. );
  53. }
  54. assert() {
  55. try {
  56. (_assert || _load_assert()).default.apply(undefined, arguments);
  57. } catch (error) {
  58. this._log('assert', error.toString());
  59. }
  60. }
  61. count() {
  62. let label =
  63. arguments.length > 0 && arguments[0] !== undefined
  64. ? arguments[0]
  65. : 'default';
  66. if (!this._counters[label]) {
  67. this._counters[label] = 0;
  68. }
  69. this._log(
  70. 'count',
  71. (0, (_util || _load_util()).format)(
  72. `${label}: ${++this._counters[label]}`
  73. )
  74. );
  75. }
  76. countReset() {
  77. let label =
  78. arguments.length > 0 && arguments[0] !== undefined
  79. ? arguments[0]
  80. : 'default';
  81. this._counters[label] = 0;
  82. }
  83. debug() {
  84. this._log(
  85. 'debug',
  86. (_util || _load_util()).format.apply(undefined, arguments)
  87. );
  88. }
  89. dir() {
  90. this._log(
  91. 'dir',
  92. (_util || _load_util()).format.apply(undefined, arguments)
  93. );
  94. }
  95. dirxml() {
  96. this._log(
  97. 'dirxml',
  98. (_util || _load_util()).format.apply(undefined, arguments)
  99. );
  100. }
  101. error() {
  102. this._log(
  103. 'error',
  104. (_util || _load_util()).format.apply(undefined, arguments)
  105. );
  106. }
  107. group() {
  108. this._groupDepth++;
  109. if (arguments.length > 0) {
  110. this._log(
  111. 'group',
  112. (_chalk || _load_chalk()).default.bold(
  113. (_util || _load_util()).format.apply(undefined, arguments)
  114. )
  115. );
  116. }
  117. }
  118. groupCollapsed() {
  119. this._groupDepth++;
  120. if (arguments.length > 0) {
  121. this._log(
  122. 'groupCollapsed',
  123. (_chalk || _load_chalk()).default.bold(
  124. (_util || _load_util()).format.apply(undefined, arguments)
  125. )
  126. );
  127. }
  128. }
  129. groupEnd() {
  130. if (this._groupDepth > 0) {
  131. this._groupDepth--;
  132. }
  133. }
  134. info() {
  135. this._log(
  136. 'info',
  137. (_util || _load_util()).format.apply(undefined, arguments)
  138. );
  139. }
  140. log() {
  141. this._log(
  142. 'log',
  143. (_util || _load_util()).format.apply(undefined, arguments)
  144. );
  145. }
  146. time() {
  147. let label =
  148. arguments.length > 0 && arguments[0] !== undefined
  149. ? arguments[0]
  150. : 'default';
  151. if (this._timers[label]) {
  152. return;
  153. }
  154. this._timers[label] = new Date();
  155. }
  156. timeEnd() {
  157. let label =
  158. arguments.length > 0 && arguments[0] !== undefined
  159. ? arguments[0]
  160. : 'default';
  161. const startTime = this._timers[label];
  162. if (startTime) {
  163. const endTime = new Date();
  164. const time = endTime - startTime;
  165. this._log(
  166. 'time',
  167. (0, (_util || _load_util()).format)(`${label}: ${time}ms`)
  168. );
  169. delete this._timers[label];
  170. }
  171. }
  172. warn() {
  173. this._log(
  174. 'warn',
  175. (_util || _load_util()).format.apply(undefined, arguments)
  176. );
  177. }
  178. getBuffer() {
  179. return null;
  180. }
  181. }
  182. exports.default = CustomConsole;