diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2021-02-15 18:10:33 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2021-02-17 10:15:01 +0100 |
| commit | d2b55f74a2247f2a7ae87e649f9d071188570e23 (patch) | |
| tree | 81b496ded74cc4496a89d0ad31f2ef15a36823bf /gui/src | |
| parent | 66f6c3ad02be5d3a2b3bf66667c6debc5007aeaa (diff) | |
| download | mullvadvpn-d2b55f74a2247f2a7ae87e649f9d071188570e23.tar.xz mullvadvpn-d2b55f74a2247f2a7ae87e649f9d071188570e23.zip | |
Prevent logging when running tests
Diffstat (limited to 'gui/src')
| -rw-r--r-- | gui/src/main/index.ts | 29 | ||||
| -rw-r--r-- | gui/src/main/logging.ts | 65 | ||||
| -rw-r--r-- | gui/src/shared/logging-types.ts | 2 | ||||
| -rw-r--r-- | gui/src/shared/logging.ts | 11 |
4 files changed, 57 insertions, 50 deletions
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 601f58f232..53a572ede1 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -275,17 +275,21 @@ class ApplicationMain { const rendererLogPath = getRendererLogPath(); if (process.env.NODE_ENV !== 'development') { - createLoggingDirectory(); - cleanUpLogDirectory(OLD_LOG_FILES); + this.rendererLog = new Logger(); + this.rendererLog.addInput(new IpcInput()); - backupLogFile(mainLogPath); - backupLogFile(rendererLogPath); + try { + createLoggingDirectory(); + cleanUpLogDirectory(OLD_LOG_FILES); - log.addOutput(new FileOutput(LogLevel.debug, mainLogPath)); + backupLogFile(mainLogPath); + backupLogFile(rendererLogPath); - this.rendererLog = new Logger(); - this.rendererLog.addInput(new IpcInput()); - this.rendererLog.addOutput(new FileOutput(LogLevel.debug, rendererLogPath)); + log.addOutput(new FileOutput(LogLevel.debug, mainLogPath)); + this.rendererLog.addOutput(new FileOutput(LogLevel.debug, rendererLogPath)); + } catch (e) { + console.error('Failed to initialize logging:', e); + } } log.addOutput(new ConsoleOutput(LogLevel.debug)); @@ -356,8 +360,13 @@ class ApplicationMain { this.daemonRpc.disconnect(); - log.dispose(); - this.rendererLog?.dispose(); + for (const logger of [log, this.rendererLog]) { + try { + logger?.dispose(); + } catch (e) { + console.error('Failed to dispose logger:', e); + } + } } private detectLocale(): string { diff --git a/gui/src/main/logging.ts b/gui/src/main/logging.ts index 7029625d3f..fb3730d59e 100644 --- a/gui/src/main/logging.ts +++ b/gui/src/main/logging.ts @@ -7,34 +7,26 @@ import { LogLevel, ILogInput, ILogOutput } from '../shared/logging-types'; export const OLD_LOG_FILES = ['frontend-renderer.log']; export class FileOutput implements ILogOutput { - private fileDescriptor?: number; + private fileDescriptor: number; - constructor(public level: LogLevel, private filePath: string) { - try { - this.fileDescriptor = fs.openSync(filePath, fs.constants.O_CREAT | fs.constants.O_WRONLY); - } catch (e) { - console.error(`Failed to open ${this.filePath}`); - } + constructor(public level: LogLevel, filePath: string) { + this.fileDescriptor = fs.openSync(filePath, fs.constants.O_CREAT | fs.constants.O_WRONLY); } public dispose() { - if (this.fileDescriptor) { - try { - fs.closeSync(this.fileDescriptor); - } catch (e) { - console.error(`Failed to close ${this.filePath}`); - } - } + fs.closeSync(this.fileDescriptor); } - public write(_level: LogLevel, message: string) { - if (this.fileDescriptor) { + public write(_level: LogLevel, message: string): Promise<void> { + return new Promise((resolve, reject) => { fs.write(this.fileDescriptor, `${message}\n`, (err) => { if (err) { - console.error(`Failed to log to ${this.filePath}`); + reject(err); + } else { + resolve(); } }); - } + }); } } @@ -53,11 +45,7 @@ export function getRendererLogPath() { } export function createLoggingDirectory(): void { - try { - fs.mkdirSync(getLogDirectoryDir(), { recursive: true }); - } catch (e) { - console.error('Failed to create logging directory'); - } + fs.mkdirSync(getLogDirectoryDir(), { recursive: true }); } // When cleaning up old log files they are first backed up and the next time removed. @@ -68,27 +56,19 @@ export function cleanUpLogDirectory(fileNames: string[]): void { }); } -export function backupLogFile(filePath: string): boolean { +export function backupLogFile(filePath: string) { const backupFilePath = getBackupFilePath(filePath); - try { - fs.accessSync(filePath); + if (fileExists(filePath)) { fs.renameSync(filePath, backupFilePath); - return true; - } catch (e) { - console.error(`Failed to backup ${filePath}`); - return false; } } export function rotateOrDeleteFile(filePath: string): void { - if (!backupLogFile(filePath)) { - const backupFilePath = getBackupFilePath(filePath); - try { - fs.accessSync(backupFilePath); - fs.unlinkSync(backupFilePath); - } catch (e) { - console.error(`Failed to delete ${filePath}`); - } + const backupFilePath = getBackupFilePath(filePath); + if (fileExists(filePath)) { + backupLogFile(filePath); + } else if (fileExists(backupFilePath)) { + fs.unlinkSync(backupFilePath); } } @@ -101,3 +81,12 @@ function getBackupFilePath(filePath: string): string { function getLogDirectoryDir() { return app.getPath('logs'); } + +function fileExists(filePath: string): boolean { + try { + fs.accessSync(filePath); + return true; + } catch (e) { + return false; + } +} diff --git a/gui/src/shared/logging-types.ts b/gui/src/shared/logging-types.ts index 09bd3f66cf..8b4ff9e306 100644 --- a/gui/src/shared/logging-types.ts +++ b/gui/src/shared/logging-types.ts @@ -8,7 +8,7 @@ export enum LogLevel { export interface ILogOutput { level: LogLevel; - write(level: LogLevel, message: string): void; + write(level: LogLevel, message: string): void | Promise<void>; dispose?(): void; } diff --git a/gui/src/shared/logging.ts b/gui/src/shared/logging.ts index 48c290c9b5..43d0b271d9 100644 --- a/gui/src/shared/logging.ts +++ b/gui/src/shared/logging.ts @@ -32,7 +32,16 @@ export class Logger { private outputMessage(level: LogLevel, message: string) { this.outputs .filter((output) => level <= output.level) - .forEach((output) => output.write(level, message)); + .forEach(async (output) => { + const maybePromise = output.write(level, message); + if (maybePromise instanceof Promise) { + try { + await maybePromise; + } catch (e) { + console.error(`${output.constructor.name}.write: ${e.message}`); + } + } + }); } } |
