forked from jestjs/jest
/
BufferedConsole.ts
187 lines (152 loc) · 4.72 KB
/
BufferedConsole.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {AssertionError, strict as assert} from 'assert';
import {Console} from 'console';
import {InspectOptions, format, formatWithOptions, inspect} from 'util';
import chalk = require('chalk');
import {ErrorWithStack, formatTime} from 'jest-util';
import type {
ConsoleBuffer,
LogCounters,
LogMessage,
LogTimers,
LogType,
} from './types';
export default class BufferedConsole extends Console {
private readonly _buffer: ConsoleBuffer = [];
private _counters: LogCounters = {};
private _timers: LogTimers = {};
private _groupDepth = 0;
override Console: typeof Console = Console;
constructor() {
super({
write: (message: string) => {
BufferedConsole.write(this._buffer, 'log', message, null);
return true;
},
} as NodeJS.WritableStream);
}
static write(
this: void,
buffer: ConsoleBuffer,
type: LogType,
message: LogMessage,
level?: number | null,
): ConsoleBuffer {
const stackLevel = level != null ? level : 2;
const rawStack = new ErrorWithStack(undefined, BufferedConsole.write).stack;
invariant(rawStack != null, 'always have a stack trace');
const origin = rawStack
.split('\n')
.slice(stackLevel)
.filter(Boolean)
.join('\n');
buffer.push({
message,
origin,
type,
});
return buffer;
}
private _log(type: LogType, message: LogMessage) {
BufferedConsole.write(
this._buffer,
type,
' '.repeat(this._groupDepth) + message,
3,
);
}
override assert(value: unknown, message?: string | Error): void {
try {
assert(value, message);
} catch (error) {
if (!(error instanceof AssertionError)) {
throw error;
}
this._log('assert', error.toString().replace(/:\n\n.*\n/gs, ''));
}
}
override count(label = 'default'): void {
if (!this._counters[label]) {
this._counters[label] = 0;
}
this._log('count', format(`${label}: ${++this._counters[label]}`));
}
override countReset(label = 'default'): void {
this._counters[label] = 0;
}
override debug(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('debug', format(firstArg, ...rest));
}
override dir(firstArg: unknown, options: InspectOptions = {}): void {
const representation = inspect(firstArg, options);
this._log('dir', formatWithOptions(options, representation));
}
override dirxml(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('dirxml', format(firstArg, ...rest));
}
override error(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('error', format(firstArg, ...rest));
}
override group(title?: string, ...rest: Array<unknown>): void {
this._groupDepth++;
if (title != null || rest.length > 0) {
this._log('group', chalk.bold(format(title, ...rest)));
}
}
override groupCollapsed(title?: string, ...rest: Array<unknown>): void {
this._groupDepth++;
if (title != null || rest.length > 0) {
this._log('groupCollapsed', chalk.bold(format(title, ...rest)));
}
}
override groupEnd(): void {
if (this._groupDepth > 0) {
this._groupDepth--;
}
}
override info(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('info', format(firstArg, ...rest));
}
override log(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('log', format(firstArg, ...rest));
}
override time(label = 'default'): void {
if (this._timers[label] != null) {
return;
}
this._timers[label] = new Date();
}
override timeEnd(label = 'default'): void {
const startTime = this._timers[label];
if (startTime != null) {
const endTime = new Date();
const time = endTime.getTime() - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`));
delete this._timers[label];
}
}
override timeLog(label = 'default', ...data: Array<unknown>): void {
const startTime = this._timers[label];
if (startTime != null) {
const endTime = new Date();
const time = endTime.getTime() - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`, ...data));
}
}
override warn(firstArg: unknown, ...rest: Array<unknown>): void {
this._log('warn', format(firstArg, ...rest));
}
getBuffer(): ConsoleBuffer | undefined {
return this._buffer.length ? this._buffer : undefined;
}
}
function invariant(condition: boolean, message?: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}