Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/browser/mainThreadLogService.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 { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js';
7
import { ILoggerOptions, ILoggerResource, ILoggerService, ILogService, isLogLevel, log, LogLevel, LogLevelToString, parseLogLevel } from '../../../platform/log/common/log.js';
8
import { DisposableStore } from '../../../base/common/lifecycle.js';
9
import { ExtHostContext, MainThreadLoggerShape, MainContext } from '../common/extHost.protocol.js';
10
import { UriComponents, URI, UriDto } from '../../../base/common/uri.js';
11
import { ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';
12
import { CommandsRegistry } from '../../../platform/commands/common/commands.js';
13
import { IEnvironmentService } from '../../../platform/environment/common/environment.js';
14
15
@extHostNamedCustomer(MainContext.MainThreadLogger)
16
export class MainThreadLoggerService implements MainThreadLoggerShape {
17
18
private readonly disposables = new DisposableStore();
19
20
constructor(
21
extHostContext: IExtHostContext,
22
@ILoggerService private readonly loggerService: ILoggerService,
23
) {
24
const proxy = extHostContext.getProxy(ExtHostContext.ExtHostLogLevelServiceShape);
25
this.disposables.add(loggerService.onDidChangeLogLevel(arg => {
26
if (isLogLevel(arg)) {
27
proxy.$setLogLevel(arg);
28
} else {
29
proxy.$setLogLevel(arg[1], arg[0]);
30
}
31
}));
32
}
33
34
$log(file: UriComponents, messages: [LogLevel, string][]): void {
35
const logger = this.loggerService.getLogger(URI.revive(file));
36
if (!logger) {
37
throw new Error('Create the logger before logging');
38
}
39
for (const [level, message] of messages) {
40
log(logger, level, message);
41
}
42
}
43
44
async $createLogger(file: UriComponents, options?: ILoggerOptions): Promise<void> {
45
this.loggerService.createLogger(URI.revive(file), options);
46
}
47
48
async $registerLogger(logResource: UriDto<ILoggerResource>): Promise<void> {
49
this.loggerService.registerLogger({
50
...logResource,
51
resource: URI.revive(logResource.resource)
52
});
53
}
54
55
async $deregisterLogger(resource: UriComponents): Promise<void> {
56
this.loggerService.deregisterLogger(URI.revive(resource));
57
}
58
59
async $setVisibility(resource: UriComponents, visible: boolean): Promise<void> {
60
this.loggerService.setVisibility(URI.revive(resource), visible);
61
}
62
63
$flush(file: UriComponents): void {
64
const logger = this.loggerService.getLogger(URI.revive(file));
65
if (!logger) {
66
throw new Error('Create the logger before flushing');
67
}
68
logger.flush();
69
}
70
71
dispose(): void {
72
this.disposables.dispose();
73
}
74
}
75
76
// --- Internal commands to improve extension test runs
77
78
CommandsRegistry.registerCommand('_extensionTests.setLogLevel', function (accessor: ServicesAccessor, level: string) {
79
const loggerService = accessor.get(ILoggerService);
80
const environmentService = accessor.get(IEnvironmentService);
81
82
if (environmentService.isExtensionDevelopment && !!environmentService.extensionTestsLocationURI) {
83
const logLevel = parseLogLevel(level);
84
if (logLevel !== undefined) {
85
loggerService.setLogLevel(logLevel);
86
}
87
}
88
});
89
90
CommandsRegistry.registerCommand('_extensionTests.getLogLevel', function (accessor: ServicesAccessor) {
91
const logService = accessor.get(ILogService);
92
93
return LogLevelToString(logService.getLevel());
94
});
95
96