|
| 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 { IStackArgument } from 'vs/base/common/console'; |
| 7 | +import { safeStringify } from 'vs/base/common/objects'; |
| 8 | +import { MainContext, MainThreadConsoleShape } from 'vs/workbench/api/common/extHost.protocol'; |
| 9 | +import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService'; |
| 10 | +import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService'; |
| 11 | + |
| 12 | +export abstract class AbstractExtHostConsoleForwarder { |
| 13 | + |
| 14 | + private readonly _mainThreadConsole: MainThreadConsoleShape; |
| 15 | + private readonly _includeStack: boolean; |
| 16 | + private readonly _logNative: boolean; |
| 17 | + |
| 18 | + constructor( |
| 19 | + @IExtHostRpcService extHostRpc: IExtHostRpcService, |
| 20 | + @IExtHostInitDataService initData: IExtHostInitDataService, |
| 21 | + ) { |
| 22 | + this._mainThreadConsole = extHostRpc.getProxy(MainContext.MainThreadConsole); |
| 23 | + this._includeStack = initData.consoleForward.includeStack; |
| 24 | + this._logNative = initData.consoleForward.logNative; |
| 25 | + |
| 26 | + // Pass console logging to the outside so that we have it in the main side if told so |
| 27 | + this._wrapConsoleMethod('info', 'log'); |
| 28 | + this._wrapConsoleMethod('log', 'log'); |
| 29 | + this._wrapConsoleMethod('warn', 'warn'); |
| 30 | + this._wrapConsoleMethod('error', 'error'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Wraps a console message so that it is transmitted to the renderer. If |
| 35 | + * native logging is turned on, the original console message will be written |
| 36 | + * as well. This is needed since the console methods are "magic" in V8 and |
| 37 | + * are the only methods that allow later introspection of logged variables. |
| 38 | + * |
| 39 | + * The wrapped property is not defined with `writable: false` to avoid |
| 40 | + * throwing errors, but rather a no-op setting. See https://github.com/microsoft/vscode-extension-telemetry/issues/88 |
| 41 | + */ |
| 42 | + private _wrapConsoleMethod(method: 'log' | 'info' | 'warn' | 'error', severity: 'log' | 'warn' | 'error') { |
| 43 | + const that = this; |
| 44 | + const original = console[method]; |
| 45 | + |
| 46 | + Object.defineProperty(console, method, { |
| 47 | + set: () => { }, |
| 48 | + get: () => function () { |
| 49 | + that._handleConsoleCall(method, severity, original, arguments); |
| 50 | + }, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private _handleConsoleCall(method: 'log' | 'info' | 'warn' | 'error', severity: 'log' | 'warn' | 'error', original: (...args: any[]) => void, args: IArguments): void { |
| 55 | + this._mainThreadConsole.$logExtensionHostMessage({ |
| 56 | + type: '__$console', |
| 57 | + severity, |
| 58 | + arguments: safeStringifyArgumentsToArray(args, this._includeStack) |
| 59 | + }); |
| 60 | + if (this._logNative) { |
| 61 | + this._nativeConsoleLogMessage(method, original, args); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + protected abstract _nativeConsoleLogMessage(method: 'log' | 'info' | 'warn' | 'error', original: (...args: any[]) => void, args: IArguments): void; |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +const MAX_LENGTH = 100000; |
| 70 | + |
| 71 | +/** |
| 72 | + * Prevent circular stringify and convert arguments to real array |
| 73 | + */ |
| 74 | +function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean): string { |
| 75 | + const argsArray = []; |
| 76 | + |
| 77 | + // Massage some arguments with special treatment |
| 78 | + if (args.length) { |
| 79 | + for (let i = 0; i < args.length; i++) { |
| 80 | + let arg = args[i]; |
| 81 | + |
| 82 | + // Any argument of type 'undefined' needs to be specially treated because |
| 83 | + // JSON.stringify will simply ignore those. We replace them with the string |
| 84 | + // 'undefined' which is not 100% right, but good enough to be logged to console |
| 85 | + if (typeof arg === 'undefined') { |
| 86 | + arg = 'undefined'; |
| 87 | + } |
| 88 | + |
| 89 | + // Any argument that is an Error will be changed to be just the error stack/message |
| 90 | + // itself because currently cannot serialize the error over entirely. |
| 91 | + else if (arg instanceof Error) { |
| 92 | + const errorObj = arg; |
| 93 | + if (errorObj.stack) { |
| 94 | + arg = errorObj.stack; |
| 95 | + } else { |
| 96 | + arg = errorObj.toString(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + argsArray.push(arg); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Add the stack trace as payload if we are told so. We remove the message and the 2 top frames |
| 105 | + // to start the stacktrace where the console message was being written |
| 106 | + if (includeStack) { |
| 107 | + const stack = new Error().stack; |
| 108 | + if (stack) { |
| 109 | + argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') } as IStackArgument); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + const res = safeStringify(argsArray); |
| 115 | + |
| 116 | + if (res.length > MAX_LENGTH) { |
| 117 | + return 'Output omitted for a large object that exceeds the limits'; |
| 118 | + } |
| 119 | + |
| 120 | + return res; |
| 121 | + } catch (error) { |
| 122 | + return `Output omitted for an object that cannot be inspected ('${error.toString()}')`; |
| 123 | + } |
| 124 | +} |
0 commit comments