buffered_console.js 4.6 KB

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