Path: blob/main/src/vs/workbench/api/browser/mainThreadLogService.ts
3296 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 { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js';6import { ILoggerOptions, ILoggerResource, ILoggerService, ILogService, isLogLevel, log, LogLevel, LogLevelToString, parseLogLevel } from '../../../platform/log/common/log.js';7import { DisposableStore } from '../../../base/common/lifecycle.js';8import { ExtHostContext, MainThreadLoggerShape, MainContext } from '../common/extHost.protocol.js';9import { UriComponents, URI, UriDto } from '../../../base/common/uri.js';10import { ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';11import { CommandsRegistry } from '../../../platform/commands/common/commands.js';12import { IEnvironmentService } from '../../../platform/environment/common/environment.js';1314@extHostNamedCustomer(MainContext.MainThreadLogger)15export class MainThreadLoggerService implements MainThreadLoggerShape {1617private readonly disposables = new DisposableStore();1819constructor(20extHostContext: IExtHostContext,21@ILoggerService private readonly loggerService: ILoggerService,22) {23const proxy = extHostContext.getProxy(ExtHostContext.ExtHostLogLevelServiceShape);24this.disposables.add(loggerService.onDidChangeLogLevel(arg => {25if (isLogLevel(arg)) {26proxy.$setLogLevel(arg);27} else {28proxy.$setLogLevel(arg[1], arg[0]);29}30}));31}3233$log(file: UriComponents, messages: [LogLevel, string][]): void {34const logger = this.loggerService.getLogger(URI.revive(file));35if (!logger) {36throw new Error('Create the logger before logging');37}38for (const [level, message] of messages) {39log(logger, level, message);40}41}4243async $createLogger(file: UriComponents, options?: ILoggerOptions): Promise<void> {44this.loggerService.createLogger(URI.revive(file), options);45}4647async $registerLogger(logResource: UriDto<ILoggerResource>): Promise<void> {48this.loggerService.registerLogger({49...logResource,50resource: URI.revive(logResource.resource)51});52}5354async $deregisterLogger(resource: UriComponents): Promise<void> {55this.loggerService.deregisterLogger(URI.revive(resource));56}5758async $setVisibility(resource: UriComponents, visible: boolean): Promise<void> {59this.loggerService.setVisibility(URI.revive(resource), visible);60}6162$flush(file: UriComponents): void {63const logger = this.loggerService.getLogger(URI.revive(file));64if (!logger) {65throw new Error('Create the logger before flushing');66}67logger.flush();68}6970dispose(): void {71this.disposables.dispose();72}73}7475// --- Internal commands to improve extension test runs7677CommandsRegistry.registerCommand('_extensionTests.setLogLevel', function (accessor: ServicesAccessor, level: string) {78const loggerService = accessor.get(ILoggerService);79const environmentService = accessor.get(IEnvironmentService);8081if (environmentService.isExtensionDevelopment && !!environmentService.extensionTestsLocationURI) {82const logLevel = parseLogLevel(level);83if (logLevel !== undefined) {84loggerService.setLogLevel(logLevel);85}86}87});8889CommandsRegistry.registerCommand('_extensionTests.getLogLevel', function (accessor: ServicesAccessor) {90const logService = accessor.get(ILogService);9192return LogLevelToString(logService.getLevel());93});949596