-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
CustomConsole.ts
155 lines (125 loc) · 4.08 KB
/
CustomConsole.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
/**
* 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 assert = require('assert');
import {Console} from 'console';
import {InspectOptions, format, formatWithOptions, inspect} from 'util';
import chalk = require('chalk');
import {clearLine, formatTime} from 'jest-util';
import type {LogCounters, LogMessage, LogTimers, LogType} from './types';
type Formatter = (type: LogType, message: LogMessage) => string;
export default class CustomConsole extends Console {
private _stdout: NodeJS.WriteStream;
private _stderr: NodeJS.WriteStream;
private _formatBuffer: Formatter;
private _counters: LogCounters = {};
private _timers: LogTimers = {};
private _groupDepth = 0;
Console: typeof Console = Console;
constructor(
stdout: NodeJS.WriteStream,
stderr: NodeJS.WriteStream,
formatBuffer: Formatter = (_type, message) => message,
) {
super(stdout, stderr);
this._stdout = stdout;
this._stderr = stderr;
this._formatBuffer = formatBuffer;
}
private _log(type: LogType, message: string) {
clearLine(this._stdout);
super.log(
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
);
}
private _logError(type: LogType, message: string) {
clearLine(this._stderr);
super.error(
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
);
}
assert(value: unknown, message?: string | Error): asserts value {
try {
assert(value, message);
} catch (error: any) {
this._logError('assert', error.toString());
}
}
count(label: string = 'default'): void {
if (!this._counters[label]) {
this._counters[label] = 0;
}
this._log('count', format(`${label}: ${++this._counters[label]}`));
}
countReset(label: string = 'default'): void {
this._counters[label] = 0;
}
debug(firstArg: unknown, ...args: Array<unknown>): void {
this._log('debug', format(firstArg, ...args));
}
dir(firstArg: unknown, options: InspectOptions = {}): void {
const representation = inspect(firstArg, options);
this._log('dir', formatWithOptions(options, representation));
}
dirxml(firstArg: unknown, ...args: Array<unknown>): void {
this._log('dirxml', format(firstArg, ...args));
}
error(firstArg: unknown, ...args: Array<unknown>): void {
this._logError('error', format(firstArg, ...args));
}
group(title?: string, ...args: Array<unknown>): void {
this._groupDepth++;
if (title || args.length > 0) {
this._log('group', chalk.bold(format(title, ...args)));
}
}
groupCollapsed(title?: string, ...args: Array<unknown>): void {
this._groupDepth++;
if (title || args.length > 0) {
this._log('groupCollapsed', chalk.bold(format(title, ...args)));
}
}
groupEnd(): void {
if (this._groupDepth > 0) {
this._groupDepth--;
}
}
info(firstArg: unknown, ...args: Array<unknown>): void {
this._log('info', format(firstArg, ...args));
}
log(firstArg: unknown, ...args: Array<unknown>): void {
this._log('log', format(firstArg, ...args));
}
time(label: string = 'default'): void {
if (this._timers[label]) {
return;
}
this._timers[label] = new Date();
}
timeEnd(label: string = 'default'): void {
const startTime = this._timers[label];
if (startTime) {
const endTime = new Date().getTime();
const time = endTime - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`));
delete this._timers[label];
}
}
timeLog(label = 'default', ...data: Array<unknown>): void {
const startTime = this._timers[label];
if (startTime) {
const endTime = new Date();
const time = endTime.getTime() - startTime.getTime();
this._log('time', format(`${label}: ${formatTime(time)}`, ...data));
}
}
warn(firstArg: unknown, ...args: Array<unknown>): void {
this._logError('warn', format(firstArg, ...args));
}
getBuffer(): undefined {
return undefined;
}
}