fix: copy log file when saving to desktop

pull/3145/head
yougotwill 8 months ago
parent e6cf7ba488
commit 86df66621b

@ -29,7 +29,7 @@ window.getCommitHash = () => configAny.commitHash;
window.getNodeVersion = () => configAny.node_version; window.getNodeVersion = () => configAny.node_version;
window.getOSRelease = () => window.getOSRelease = () =>
`${os.type()} ${os.release()}, Node.js ${config.node_version} ${os.platform()} ${os.arch()}`; `${os.type()} ${os.release()}, Node.js ${config.node_version} ${os.platform()} ${os.arch()}`;
window.saveLog = logText => ipcRenderer.send('save-debug-log', logText); window.saveLog = additionalText => ipcRenderer.send('save-debug-log', additionalText);
window.sessionFeatureFlags = { window.sessionFeatureFlags = {
useOnionRequests: true, useOnionRequests: true,

@ -54,8 +54,8 @@ export const AboutView = () => {
} }
const versionInfo = `v${window.getVersion()}`; const versionInfo = `v${window.getVersion()}`;
const commitInfo = `Commit ${window.getCommitHash()}` || ''; const commitInfo = `Commit: ${window.getCommitHash()}` || '';
const osInfo = `${window.getOSRelease()}`; const osInfo = `Operating System: ${window.getOSRelease()}`;
useEffect(() => { useEffect(() => {
if (window.theme) { if (window.theme) {

@ -77,7 +77,7 @@ import { initAttachmentsChannel } from '../node/attachment_channel';
import * as updater from '../updater/index'; // checked - only node import * as updater from '../updater/index'; // checked - only node
import { ephemeralConfig } from '../node/config/ephemeral_config'; // checked - only node import { ephemeralConfig } from '../node/config/ephemeral_config'; // checked - only node
import { getLogger, initializeLogger } from '../node/logging'; // checked - only node import { getLoggerFilePath, getLogger, initializeLogger } from '../node/logging'; // checked - only node
import { createTemplate } from '../node/menu'; // checked - only node import { createTemplate } from '../node/menu'; // checked - only node
import { installPermissionsHandler } from '../node/permissions'; // checked - only node import { installPermissionsHandler } from '../node/permissions'; // checked - only node
import { installFileHandler, installWebHandler } from '../node/protocol_filter'; // checked - only node import { installFileHandler, installWebHandler } from '../node/protocol_filter'; // checked - only node
@ -648,7 +648,7 @@ async function showAbout() {
aboutWindow?.show(); aboutWindow?.show();
} }
async function saveDebugLog(_event: any, logText: any) { async function saveDebugLog(_event: any, additionalInfo: string) {
const options: Electron.SaveDialogOptions = { const options: Electron.SaveDialogOptions = {
title: 'Save debug log', title: 'Save debug log',
defaultPath: path.join( defaultPath: path.join(
@ -661,17 +661,24 @@ async function saveDebugLog(_event: any, logText: any) {
try { try {
const result = await dialog.showSaveDialog(options); const result = await dialog.showSaveDialog(options);
const outputPath = result.filePath; const outputPath = result.filePath;
console.info(`Trying to save logs to ${outputPath}`); console.info(`[log] Trying to save logs to ${outputPath}`);
if (result === undefined || outputPath === undefined || outputPath === '') { if (result === undefined || outputPath === undefined || outputPath === '') {
throw Error("User clicked Save button but didn't create a file"); throw Error("User clicked Save button but didn't create a file");
} }
fs.writeFile(outputPath, logText, err => { const loggerFilePath = getLoggerFilePath();
if (err) { if (!loggerFilePath) {
throw Error(`${err}`); throw Error('No logger file path');
} }
console.info(`Saved log - ${outputPath}`);
}); fs.copyFileSync(loggerFilePath, outputPath);
console.info(`[log] Copied logs to ${outputPath} from ${loggerFilePath}`);
// append any additional info
if (additionalInfo) {
fs.appendFileSync(outputPath, additionalInfo, { encoding: 'utf-8' });
console.info(`[log] Saved additional info to logs ${outputPath} from ${loggerFilePath}`);
}
} catch (err) { } catch (err) {
console.error('Error saving debug log', err); console.error('Error saving debug log', err);
} }

@ -15,6 +15,8 @@ import { redactAll } from '../util/privacy';
const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace']; const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
let logger: Logger | undefined; let logger: Logger | undefined;
let loggerFilePath: string | undefined;
export type ConsoleCustom = typeof console & { export type ConsoleCustom = typeof console & {
_log: (...args: any) => void; _log: (...args: any) => void;
_warn: (...args: any) => void; _warn: (...args: any) => void;
@ -29,12 +31,13 @@ export async function initializeLogger() {
const basePath = app.getPath('userData'); const basePath = app.getPath('userData');
const logFolder = path.join(basePath, 'logs'); const logFolder = path.join(basePath, 'logs');
const logFile = path.join(logFolder, 'log.log'); const logFile = path.join(logFolder, 'log.log');
loggerFilePath = logFile;
fs.mkdirSync(logFolder, { recursive: true }); fs.mkdirSync(logFolder, { recursive: true });
await cleanupLogs(logFile, logFolder); await cleanupLogs(logFile, logFolder);
console.warn('logFile', logFile); console.warn('[log] filepath', logFile);
logger = Logger.createLogger({ logger = Logger.createLogger({
name: 'log', name: 'log',
@ -64,14 +67,14 @@ export async function initializeLogger() {
fs.mkdirSync(logFolder, { recursive: true }); fs.mkdirSync(logFolder, { recursive: true });
} }
console.info('fetching logs from logPath'); console.info('[log] fetching logs from', logFile);
fetchLogFile(logFile).then( fetchLogFile(logFile).then(
data => { data => {
event.sender.send('fetched-log', data); event.sender.send('fetched-log', data);
}, },
error => { error => {
logger?.error(`Problem loading log from disk: ${error.stack}`); logger?.error(`[log] Problem loading log from disk: ${error.stack}`);
} }
); );
}); });
@ -81,13 +84,17 @@ export async function initializeLogger() {
try { try {
await deleteAllLogs(logFile); await deleteAllLogs(logFile);
} catch (error) { } catch (error) {
logger?.error(`Problem deleting all logs: ${error.stack}`); logger?.error(`[log] Problem deleting all logs: ${error.stack}`);
} }
event.sender.send('delete-all-logs-complete'); event.sender.send('delete-all-logs-complete');
}); });
} }
export function getLoggerFilePath() {
return loggerFilePath;
}
async function deleteAllLogs(logFile: string) { async function deleteAllLogs(logFile: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
rimraf( rimraf(
@ -116,7 +123,10 @@ async function cleanupLogs(logFile: string, logFolder: string) {
try { try {
await eliminateOldEntries(logFile, earliestDate); await eliminateOldEntries(logFile, earliestDate);
} catch (error) { } catch (error) {
console.error('Error cleaning logs; deleting and starting over from scratch.', error.stack); console.error(
'[log] Error cleaning logs; deleting and starting over from scratch.',
error.stack
);
fs.mkdirSync(logFolder, { recursive: true }); fs.mkdirSync(logFolder, { recursive: true });
} }
} }

@ -149,10 +149,16 @@ window.addEventListener('unhandledrejection', rejectionEvent => {
window.log.error('Top-level unhandled promise rejection:', errorInfo); window.log.error('Top-level unhandled promise rejection:', errorInfo);
}); });
export async function saveLogToDesktop() { export function saveLogToDesktop() {
const operatingSystemInfo = `Operating System ${window.getOSRelease()}`; const versionInfo = `v${window.getVersion()}`;
const commitHashInfo = window.getCommitHash() ? `Commit ${window.getCommitHash()}` : ''; const operatingSystemInfo = `Operating System: ${window.getOSRelease()}`;
const text = await fetchNodeLog(); const commitHashInfo = window.getCommitHash() ? `Commit: ${window.getCommitHash()}` : '';
const debugLogWithSystemInfo = `${operatingSystemInfo} ${commitHashInfo} ${text}`; const debugLogWithSystemInfo = `
******************************************************************************
# Application Info
${versionInfo}
${operatingSystemInfo}
${commitHashInfo}
******************************************************************************`;
window.saveLog(debugLogWithSystemInfo); window.saveLog(debugLogWithSystemInfo);
} }

2
ts/window.d.ts vendored

@ -82,7 +82,7 @@ declare global {
getCommitHash: () => string | undefined; getCommitHash: () => string | undefined;
getVersion: () => string; getVersion: () => string;
getOSRelease: () => string; getOSRelease: () => string;
saveLog: (text: string) => void; saveLog: (additionalText?: string) => void;
setAutoHideMenuBar: (val: boolean) => void; setAutoHideMenuBar: (val: boolean) => void;
setMenuBarVisibility: (val: boolean) => void; setMenuBarVisibility: (val: boolean) => void;
contextMenuShown: boolean; contextMenuShown: boolean;

Loading…
Cancel
Save