Path: blob/main/src/vs/workbench/api/node/extHostConsoleForwarder.ts
5221 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { AbstractExtHostConsoleForwarder } from '../common/extHostConsoleForwarder.js';6import { IExtHostInitDataService } from '../common/extHostInitDataService.js';7import { IExtHostRpcService } from '../common/extHostRpcService.js';8import { NativeLogMarkers } from '../../services/extensions/common/extensionHostProtocol.js';910const MAX_STREAM_BUFFER_LENGTH = 1024 * 1024;1112export class ExtHostConsoleForwarder extends AbstractExtHostConsoleForwarder {1314private _isMakingConsoleCall: boolean = false;1516constructor(17@IExtHostRpcService extHostRpc: IExtHostRpcService,18@IExtHostInitDataService initData: IExtHostInitDataService,19) {20super(extHostRpc, initData);2122this._wrapStream('stderr', 'error');23this._wrapStream('stdout', 'log');24}2526protected override _nativeConsoleLogMessage(method: 'log' | 'info' | 'warn' | 'error' | 'debug', original: (...args: any[]) => void, args: IArguments) {27const stream = method === 'error' || method === 'warn' ? process.stderr : process.stdout;28this._isMakingConsoleCall = true;29stream.write(`\n${NativeLogMarkers.Start}\n`);30// eslint-disable-next-line local/code-no-any-casts31original.apply(console, args as any);32stream.write(`\n${NativeLogMarkers.End}\n`);33this._isMakingConsoleCall = false;34}3536/**37* Wraps process.stderr/stdout.write() so that it is transmitted to the38* renderer or CLI. It both calls through to the original method as well39* as to console.log with complete lines so that they're made available40* to the debugger/CLI.41*/42private _wrapStream(streamName: 'stdout' | 'stderr', severity: 'log' | 'warn' | 'error') {43const stream = process[streamName];44const original = stream.write;4546let buf = '';4748Object.defineProperty(stream, 'write', {49set: () => { },50get: () => (chunk: Uint8Array | string, encoding?: BufferEncoding, callback?: (err?: Error | null) => void) => {51if (!this._isMakingConsoleCall) {52// eslint-disable-next-line local/code-no-any-casts53buf += (chunk as any).toString(encoding);54const eol = buf.length > MAX_STREAM_BUFFER_LENGTH ? buf.length : buf.lastIndexOf('\n');55if (eol !== -1) {56console[severity](buf.slice(0, eol));57buf = buf.slice(eol + 1);58}59}6061original.call(stream, chunk, encoding, callback);62},63});64}65}666768