Skip to content

Commit 99c91c5

Browse files
author
Yanjiie
committed
feat(console): hack in process.stdout and process.stderr
1 parent 2995ef5 commit 99c91c5

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

lib/core/__test__/runtime/console.hack.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ afterAll(() => {
1313
consoleRestore();
1414
});
1515

16+
afterEach(() => {
17+
jest.clearAllMocks();
18+
});
19+
1620
describe("console hack test", () => {
1721
test("ensure console contains all origin functions", () => {
1822
expect(typeof console.originDebug).toBe("function");
@@ -44,6 +48,19 @@ describe("console hack test", () => {
4448
(getCurrentContext as jest.Mock).mockClear();
4549
});
4650

51+
test("console.debug should be logged by log111ger", () => {
52+
(getCurrentContext as jest.Mock).mockImplementation(() => true);
53+
const mockedWriteLog = logger.writeLog as jest.Mock;
54+
55+
expect(mockedWriteLog.mock.calls.length).toEqual(0);
56+
process.stdout.write("test_log");
57+
expect(mockedWriteLog.mock.calls.length).toEqual(1);
58+
process.stderr.write("test_log");
59+
expect(mockedWriteLog.mock.calls.length).toEqual(2);
60+
61+
(getCurrentContext as jest.Mock).mockClear();
62+
});
63+
4764
test("multi consoleRestore() should not have side effect", () => {
4865
consoleRestore();
4966
consoleRestore();

lib/core/logger/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ export class Logger {
258258
}
259259

260260
private static fillStdout(str: string): void {
261-
process.stdout.write(`${str}\n`);
261+
// console hacking origin write, so use originWrite
262+
const stdout = (process.stdout as any).originWrite || process.stdout.write;
263+
stdout.call(process.stdout, `${str}\n`);
262264
}
263265
}
264266

lib/core/runtime/console.hack.ts

+44
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,44 @@ export const consoleHack = (): void => {
110110
`${util.format(message, ...optionalParams)}`
111111
);
112112
};
113+
114+
// hack process._stdout
115+
(process.stdout as any).originWrite = process.stdout.write;
116+
process.stdout.write = (
117+
data: Buffer | string,
118+
encodingOrCallback?: string | ((err?: Error) => void) | undefined
119+
): boolean => {
120+
let encoding: BufferEncoding;
121+
if (typeof encodingOrCallback !== "function") {
122+
encoding = encodingOrCallback as BufferEncoding;
123+
}
124+
125+
logger.writeLog(
126+
"DEBUG",
127+
data.toString(encoding).replace(/\n$/, "") // 去掉换行符
128+
);
129+
130+
return true;
131+
};
132+
133+
// hack process._stderr
134+
(process.stderr as any).originWrite = process.stderr.write;
135+
process.stderr.write = (
136+
data: Buffer | string,
137+
encodingOrCallback?: string | ((err?: Error) => void) | undefined
138+
): boolean => {
139+
let encoding: BufferEncoding;
140+
if (typeof encodingOrCallback !== "function") {
141+
encoding = encodingOrCallback as BufferEncoding;
142+
}
143+
144+
logger.writeLog(
145+
"ERROR",
146+
data.toString(encoding).replace(/\n$/, "") // 去掉换行符
147+
);
148+
149+
return true;
150+
};
113151
}
114152
};
115153

@@ -124,11 +162,17 @@ export const consoleRestore = (): void => {
124162
console.error = console.originError;
125163
console.dir = console.originDir;
126164

165+
process.stdout.write = (process.stdout as any).originWrite;
166+
process.stderr.write = (process.stderr as any).originWrite;
167+
127168
delete console.originDebug;
128169
delete console.originInfo;
129170
delete console.originLog;
130171
delete console.originWarn;
131172
delete console.originError;
132173
delete console.originDir;
174+
175+
delete (process.stdout as any).originWrite;
176+
delete (process.stderr as any).originWrite;
133177
}
134178
};

0 commit comments

Comments
 (0)