From 86df66621bea1023aeb067442500aebae9b94eb1 Mon Sep 17 00:00:00 2001 From: yougotwill Date: Thu, 22 Aug 2024 14:11:59 +1000 Subject: [PATCH] fix: copy log file when saving to desktop --- preload.js | 2 +- ts/components/AboutView.tsx | 4 ++-- ts/mains/main_node.ts | 25 ++++++++++++++++--------- ts/node/logging.ts | 20 +++++++++++++++----- ts/util/logging.ts | 16 +++++++++++----- ts/window.d.ts | 2 +- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/preload.js b/preload.js index 662f35269..e423fc79c 100644 --- a/preload.js +++ b/preload.js @@ -29,7 +29,7 @@ window.getCommitHash = () => configAny.commitHash; window.getNodeVersion = () => configAny.node_version; window.getOSRelease = () => `${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 = { useOnionRequests: true, diff --git a/ts/components/AboutView.tsx b/ts/components/AboutView.tsx index 122f9c312..3efe8d26e 100644 --- a/ts/components/AboutView.tsx +++ b/ts/components/AboutView.tsx @@ -54,8 +54,8 @@ export const AboutView = () => { } const versionInfo = `v${window.getVersion()}`; - const commitInfo = `Commit ${window.getCommitHash()}` || ''; - const osInfo = `${window.getOSRelease()}`; + const commitInfo = `Commit: ${window.getCommitHash()}` || ''; + const osInfo = `Operating System: ${window.getOSRelease()}`; useEffect(() => { if (window.theme) { diff --git a/ts/mains/main_node.ts b/ts/mains/main_node.ts index fe74fbf5a..7cb6d417d 100644 --- a/ts/mains/main_node.ts +++ b/ts/mains/main_node.ts @@ -77,7 +77,7 @@ import { initAttachmentsChannel } from '../node/attachment_channel'; import * as updater from '../updater/index'; // 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 { installPermissionsHandler } from '../node/permissions'; // checked - only node import { installFileHandler, installWebHandler } from '../node/protocol_filter'; // checked - only node @@ -648,7 +648,7 @@ async function showAbout() { aboutWindow?.show(); } -async function saveDebugLog(_event: any, logText: any) { +async function saveDebugLog(_event: any, additionalInfo: string) { const options: Electron.SaveDialogOptions = { title: 'Save debug log', defaultPath: path.join( @@ -661,17 +661,24 @@ async function saveDebugLog(_event: any, logText: any) { try { const result = await dialog.showSaveDialog(options); 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 === '') { throw Error("User clicked Save button but didn't create a file"); } - fs.writeFile(outputPath, logText, err => { - if (err) { - throw Error(`${err}`); - } - console.info(`Saved log - ${outputPath}`); - }); + const loggerFilePath = getLoggerFilePath(); + if (!loggerFilePath) { + throw Error('No logger file path'); + } + + 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) { console.error('Error saving debug log', err); } diff --git a/ts/node/logging.ts b/ts/node/logging.ts index e42e6864f..bf232fe07 100644 --- a/ts/node/logging.ts +++ b/ts/node/logging.ts @@ -15,6 +15,8 @@ import { redactAll } from '../util/privacy'; const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace']; let logger: Logger | undefined; +let loggerFilePath: string | undefined; + export type ConsoleCustom = typeof console & { _log: (...args: any) => void; _warn: (...args: any) => void; @@ -29,12 +31,13 @@ export async function initializeLogger() { const basePath = app.getPath('userData'); const logFolder = path.join(basePath, 'logs'); const logFile = path.join(logFolder, 'log.log'); + loggerFilePath = logFile; fs.mkdirSync(logFolder, { recursive: true }); await cleanupLogs(logFile, logFolder); - console.warn('logFile', logFile); + console.warn('[log] filepath', logFile); logger = Logger.createLogger({ name: 'log', @@ -64,14 +67,14 @@ export async function initializeLogger() { fs.mkdirSync(logFolder, { recursive: true }); } - console.info('fetching logs from logPath'); + console.info('[log] fetching logs from', logFile); fetchLogFile(logFile).then( data => { event.sender.send('fetched-log', data); }, 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 { await deleteAllLogs(logFile); } 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'); }); } +export function getLoggerFilePath() { + return loggerFilePath; +} + async function deleteAllLogs(logFile: string) { return new Promise((resolve, reject) => { rimraf( @@ -116,7 +123,10 @@ async function cleanupLogs(logFile: string, logFolder: string) { try { await eliminateOldEntries(logFile, earliestDate); } 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 }); } } diff --git a/ts/util/logging.ts b/ts/util/logging.ts index b8e25940a..696faf2ae 100644 --- a/ts/util/logging.ts +++ b/ts/util/logging.ts @@ -149,10 +149,16 @@ window.addEventListener('unhandledrejection', rejectionEvent => { window.log.error('Top-level unhandled promise rejection:', errorInfo); }); -export async function saveLogToDesktop() { - const operatingSystemInfo = `Operating System ${window.getOSRelease()}`; - const commitHashInfo = window.getCommitHash() ? `Commit ${window.getCommitHash()}` : ''; - const text = await fetchNodeLog(); - const debugLogWithSystemInfo = `${operatingSystemInfo} ${commitHashInfo} ${text}`; +export function saveLogToDesktop() { + const versionInfo = `v${window.getVersion()}`; + const operatingSystemInfo = `Operating System: ${window.getOSRelease()}`; + const commitHashInfo = window.getCommitHash() ? `Commit: ${window.getCommitHash()}` : ''; + const debugLogWithSystemInfo = ` +****************************************************************************** +# Application Info +${versionInfo} +${operatingSystemInfo} +${commitHashInfo} +******************************************************************************`; window.saveLog(debugLogWithSystemInfo); } diff --git a/ts/window.d.ts b/ts/window.d.ts index dbefbba98..b9011d2ee 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -82,7 +82,7 @@ declare global { getCommitHash: () => string | undefined; getVersion: () => string; getOSRelease: () => string; - saveLog: (text: string) => void; + saveLog: (additionalText?: string) => void; setAutoHideMenuBar: (val: boolean) => void; setMenuBarVisibility: (val: boolean) => void; contextMenuShown: boolean;