import { mainWindow } from '../../../base/browser/window.js';
import { relativePath } from '../../../base/common/resources.js';
import { URI } from '../../../base/common/uri.js';
import { IEnvironmentService } from '../../environment/common/environment.js';
import { IFileService } from '../../files/common/files.js';
import { AdapterLogger, DEFAULT_LOG_LEVEL, ILogger, LogLevel } from '../common/log.js';
export interface IAutomatedWindow {
codeAutomationLog(type: string, args: any[]): void;
codeAutomationExit(code: number, logs: Array<ILogFile>): void;
}
export interface ILogFile {
readonly relativePath: string;
readonly contents: string;
}
export async function getLogs(fileService: IFileService, environmentService: IEnvironmentService): Promise<ILogFile[]> {
const result: ILogFile[] = [];
await doGetLogs(fileService, result, environmentService.logsHome, environmentService.logsHome);
return result;
}
async function doGetLogs(fileService: IFileService, logs: ILogFile[], curFolder: URI, logsHome: URI): Promise<void> {
const stat = await fileService.resolve(curFolder);
for (const { resource, isDirectory } of stat.children || []) {
if (isDirectory) {
await doGetLogs(fileService, logs, resource, logsHome);
} else {
const contents = (await fileService.readFile(resource)).value.toString();
if (contents) {
const path = relativePath(logsHome, resource);
if (path) {
logs.push({ relativePath: path, contents });
}
}
}
}
}
function logLevelToString(level: LogLevel): string {
switch (level) {
case LogLevel.Trace: return 'trace';
case LogLevel.Debug: return 'debug';
case LogLevel.Info: return 'info';
case LogLevel.Warning: return 'warn';
case LogLevel.Error: return 'error';
}
return 'info';
}
export class ConsoleLogInAutomationLogger extends AdapterLogger implements ILogger {
declare codeAutomationLog: any;
constructor(logLevel: LogLevel = DEFAULT_LOG_LEVEL) {
super({ log: (level, args) => this.consoleLog(logLevelToString(level), args) }, logLevel);
}
private consoleLog(type: string, args: any[]): void {
const automatedWindow = mainWindow as unknown as IAutomatedWindow;
if (typeof automatedWindow.codeAutomationLog === 'function') {
try {
automatedWindow.codeAutomationLog(type, args);
} catch (err) {
console.error('Problems writing to codeAutomationLog', err);
}
}
}
}