diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-04-25 09:52:11 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-04-25 09:52:11 +0200 |
| commit | dd6985a5bb5b566133fe68103347c72ac254710d (patch) | |
| tree | 23a28cdc862fa1af42caac9686780d883a0a75a4 /gui/src/shared/logging.ts | |
| parent | d07d62e752f55718dad05218504c208ea4c354bc (diff) | |
| parent | 5d64426d40f87fb90db3550f51d83d63b53b0f65 (diff) | |
| download | mullvadvpn-dd6985a5bb5b566133fe68103347c72ac254710d.tar.xz mullvadvpn-dd6985a5bb5b566133fe68103347c72ac254710d.zip | |
Merge branch 'update-electron-log'
Diffstat (limited to 'gui/src/shared/logging.ts')
| -rw-r--r-- | gui/src/shared/logging.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/gui/src/shared/logging.ts b/gui/src/shared/logging.ts new file mode 100644 index 0000000000..d06abca4fe --- /dev/null +++ b/gui/src/shared/logging.ts @@ -0,0 +1,65 @@ +import { app, remote } from 'electron'; +import log from 'electron-log'; +import * as fs from 'fs'; +import * as path from 'path'; + +const LOG_FORMAT = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}][{level}] {text}'; + +// Returns platform specific logs folder for application +// See open issue and PR on Github: +// 1. https://github.com/electron/electron/issues/10118 +// 2. https://github.com/electron/electron/pull/10191 +export function getLogsDirectory() { + const theApp = process.type === 'browser' ? app : remote.app; + + switch (process.platform) { + case 'darwin': + // macOS: ~/Library/Logs/{appname} + return path.join(theApp.getPath('home'), 'Library/Logs', theApp.getName()); + default: + // Windows: %LOCALAPPDATA%\{appname}\logs + // Linux: ~/.config/{appname}/logs + return path.join(theApp.getPath('userData'), 'logs'); + } +} + +export function getMainLogFile(): string { + return path.join(getLogsDirectory(), 'frontend.log'); +} + +export function getRendererLogFile(): string { + return path.join(getLogsDirectory(), 'frontend-renderer.log'); +} + +export function setupLogging(logFile: string) { + log.transports.console.format = LOG_FORMAT; + log.transports.file.format = LOG_FORMAT; + log.transports.console.level = 'debug'; + + if (process.env.NODE_ENV === 'development') { + // Disable log file in development + log.transports.file.level = false; + } else { + // Configure logging to file + log.transports.file.level = 'debug'; + log.transports.file.file = logFile; + + log.debug(`Logging to ${logFile}`); + } +} + +export function backupLogFile(filePath: string): string | undefined { + const ext = path.extname(filePath); + const baseName = path.basename(filePath, ext); + const backupFile = path.join(path.dirname(filePath), baseName + '.old' + ext); + + try { + fs.accessSync(filePath); + fs.renameSync(filePath, backupFile); + + return backupFile; + } catch (error) { + // No previous log file exists + return undefined; + } +} |
