Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/log/browser/log.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 { mainWindow } from '../../../base/browser/window.js';
7
import { relativePath } from '../../../base/common/resources.js';
8
import { URI } from '../../../base/common/uri.js';
9
import { IEnvironmentService } from '../../environment/common/environment.js';
10
import { IFileService } from '../../files/common/files.js';
11
import { AdapterLogger, DEFAULT_LOG_LEVEL, ILogger, LogLevel } from '../common/log.js';
12
13
export interface IAutomatedWindow {
14
codeAutomationLog(type: string, args: any[]): void;
15
codeAutomationExit(code: number, logs: Array<ILogFile>): void;
16
}
17
18
export interface ILogFile {
19
readonly relativePath: string;
20
readonly contents: string;
21
}
22
23
/**
24
* Only used in browser contexts where the log files are not stored on disk
25
* but in IndexedDB. A method to get all logs with their contents so that
26
* CI automation can persist them.
27
*/
28
export async function getLogs(fileService: IFileService, environmentService: IEnvironmentService): Promise<ILogFile[]> {
29
const result: ILogFile[] = [];
30
31
await doGetLogs(fileService, result, environmentService.logsHome, environmentService.logsHome);
32
33
return result;
34
}
35
36
async function doGetLogs(fileService: IFileService, logs: ILogFile[], curFolder: URI, logsHome: URI): Promise<void> {
37
const stat = await fileService.resolve(curFolder);
38
39
for (const { resource, isDirectory } of stat.children || []) {
40
if (isDirectory) {
41
await doGetLogs(fileService, logs, resource, logsHome);
42
} else {
43
const contents = (await fileService.readFile(resource)).value.toString();
44
if (contents) {
45
const path = relativePath(logsHome, resource);
46
if (path) {
47
logs.push({ relativePath: path, contents });
48
}
49
}
50
}
51
}
52
}
53
54
function logLevelToString(level: LogLevel): string {
55
switch (level) {
56
case LogLevel.Trace: return 'trace';
57
case LogLevel.Debug: return 'debug';
58
case LogLevel.Info: return 'info';
59
case LogLevel.Warning: return 'warn';
60
case LogLevel.Error: return 'error';
61
}
62
return 'info';
63
}
64
65
/**
66
* A logger that is used when VSCode is running in the web with
67
* an automation such as playwright. We expect a global codeAutomationLog
68
* to be defined that we can use to log to.
69
*/
70
export class ConsoleLogInAutomationLogger extends AdapterLogger implements ILogger {
71
72
declare codeAutomationLog: any;
73
74
constructor(logLevel: LogLevel = DEFAULT_LOG_LEVEL) {
75
super({ log: (level, args) => this.consoleLog(logLevelToString(level), args) }, logLevel);
76
}
77
78
private consoleLog(type: string, args: any[]): void {
79
const automatedWindow = mainWindow as unknown as IAutomatedWindow;
80
if (typeof automatedWindow.codeAutomationLog === 'function') {
81
try {
82
automatedWindow.codeAutomationLog(type, args);
83
} catch (err) {
84
// see https://github.com/microsoft/vscode-test-web/issues/69
85
console.error('Problems writing to codeAutomationLog', err);
86
}
87
}
88
}
89
}
90
91