-
Notifications
You must be signed in to change notification settings - Fork 268
/
troubleshooting.ts
112 lines (98 loc) · 4.24 KB
/
troubleshooting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import AdmZip from 'adm-zip';
import moment from 'moment';
import * as os from 'node:os';
import * as fs from 'node:fs';
import type { ApiSenderType } from './api.js';
const SYSTEM_FILENAME = 'system';
export interface TroubleshootingFileMap {
fileName: string;
content: string;
}
export type LogType = 'log' | 'warn' | 'trace' | 'debug' | 'error';
export class Troubleshooting {
constructor(private apiSender: ApiSenderType) {}
// The "main" function that is exposes that is used to gather
// all the logs and save them to a zip file.
// this also takes in the console logs and adds them to the zip file (see preload/src/index.ts) regarding memoryLogs
async saveLogs(console: { logType: LogType; message: string }[], destination: string): Promise<string[]> {
const systemLogs = await this.getSystemLogs();
const consoleLogs = this.getConsoleLogs(console);
const fileMaps = [...systemLogs, ...consoleLogs];
await this.saveLogsToZip(fileMaps, destination);
return fileMaps.map(fileMap => fileMap.fileName);
}
async saveLogsToZip(fileMaps: TroubleshootingFileMap[], destination: string): Promise<void> {
if (fileMaps.length === 0) {
return;
}
const zip = new AdmZip();
fileMaps.forEach(fileMap => {
zip.addFile(fileMap.fileName, Buffer.from(fileMap.content, 'utf8'));
});
zip.writeZip(destination);
}
getConsoleLogs(consoleLogs: { logType: LogType; message: string }[]): TroubleshootingFileMap[] {
const content = consoleLogs.map(log => `${log.logType} : ${log.message}`).join('\n');
return [{ fileName: this.generateLogFileName('console'), content }];
}
async getSystemLogs(): Promise<TroubleshootingFileMap[]> {
switch (os.platform()) {
case 'darwin':
return this.getLogsFromFiles(
['launchd-stdout.log', 'launchd-stderr.log'],
`${os.homedir()}/Library/Logs/Podman Desktop`,
);
case 'win32':
return this.getLogsFromFiles(['podman-desktop'], `${os.homedir()}/AppData/Roaming/Podman Desktop/logs`);
default:
// Unsupported platform, so do not return anything
return [];
}
}
private async getFileSystemContent(filePath: string, logName: string): Promise<TroubleshootingFileMap> {
const content = await fs.promises.readFile(filePath, 'utf-8');
return { fileName: this.generateLogFileName(SYSTEM_FILENAME + '-' + logName), content };
}
private async getLogsFromFiles(logFiles: string[], logDir: string): Promise<TroubleshootingFileMap[]> {
const logs: TroubleshootingFileMap[] = [];
for (const file of logFiles) {
try {
const filePath = `${logDir}/${file}`;
// Check if the file exists, if not, skip it.
if (!fs.existsSync(filePath)) {
continue;
}
const fileMap = await this.getFileSystemContent(filePath, file);
logs.push(fileMap);
} catch (error) {
console.error(`Error reading ${file}: `, error);
}
}
return logs;
}
generateLogFileName(filename: string, extension?: string): string {
// If the filename has an extension like .log, move it to the end ofthe generated name
// otherwise just use .txt
const filenameParts = filename.split('.');
// Use the file extension if it's provided, otherwise use the one from the file name, or default to txt
const fileExtension = extension ? extension : filenameParts.length > 1 ? filenameParts[1] : 'txt';
return `${filenameParts[0]}-${moment().format('YYYYMMDDHHmmss')}.${fileExtension}`;
}
}