Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/node/extHostConsoleForwarder.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { AbstractExtHostConsoleForwarder } from '../common/extHostConsoleForwarder.js';
7
import { IExtHostInitDataService } from '../common/extHostInitDataService.js';
8
import { IExtHostRpcService } from '../common/extHostRpcService.js';
9
import { NativeLogMarkers } from '../../services/extensions/common/extensionHostProtocol.js';
10
11
const MAX_STREAM_BUFFER_LENGTH = 1024 * 1024;
12
13
export class ExtHostConsoleForwarder extends AbstractExtHostConsoleForwarder {
14
15
private _isMakingConsoleCall: boolean = false;
16
17
constructor(
18
@IExtHostRpcService extHostRpc: IExtHostRpcService,
19
@IExtHostInitDataService initData: IExtHostInitDataService,
20
) {
21
super(extHostRpc, initData);
22
23
this._wrapStream('stderr', 'error');
24
this._wrapStream('stdout', 'log');
25
}
26
27
protected override _nativeConsoleLogMessage(method: 'log' | 'info' | 'warn' | 'error' | 'debug', original: (...args: any[]) => void, args: IArguments) {
28
const stream = method === 'error' || method === 'warn' ? process.stderr : process.stdout;
29
this._isMakingConsoleCall = true;
30
stream.write(`\n${NativeLogMarkers.Start}\n`);
31
original.apply(console, args as any);
32
stream.write(`\n${NativeLogMarkers.End}\n`);
33
this._isMakingConsoleCall = false;
34
}
35
36
/**
37
* Wraps process.stderr/stdout.write() so that it is transmitted to the
38
* renderer or CLI. It both calls through to the original method as well
39
* as to console.log with complete lines so that they're made available
40
* to the debugger/CLI.
41
*/
42
private _wrapStream(streamName: 'stdout' | 'stderr', severity: 'log' | 'warn' | 'error') {
43
const stream = process[streamName];
44
const original = stream.write;
45
46
let buf = '';
47
48
Object.defineProperty(stream, 'write', {
49
set: () => { },
50
get: () => (chunk: Uint8Array | string, encoding?: BufferEncoding, callback?: (err?: Error) => void) => {
51
if (!this._isMakingConsoleCall) {
52
buf += (chunk as any).toString(encoding);
53
const eol = buf.length > MAX_STREAM_BUFFER_LENGTH ? buf.length : buf.lastIndexOf('\n');
54
if (eol !== -1) {
55
console[severity](buf.slice(0, eol));
56
buf = buf.slice(eol + 1);
57
}
58
}
59
60
original.call(stream, chunk, encoding, callback);
61
},
62
});
63
}
64
}
65
66