summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/logging.ts
blob: 471fd758c89a863269c6655214d276d65c66b290 (plain)
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
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.name);
    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(), '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.resolvePath = (_variables) => logFile;

    log.debug(`Logging to ${logFile}`);
  }
}

export function backupLogFile(filePath: string): boolean {
  const exists = fileExists(filePath);
  if (exists) {
    const backupFilePath = getBackupFilePath(filePath);
    fs.renameSync(filePath, backupFilePath);
  }

  return exists;
}

function getBackupFilePath(filePath: string): string {
  const parsedPath = path.parse(filePath);
  parsedPath.base = parsedPath.name + '.old' + parsedPath.ext;
  return path.normalize(path.format(parsedPath));
}

function fileExists(filePath: string): boolean {
  try {
    fs.accessSync(filePath);
    return true;
  } catch {
    return false;
  }
}

// When cleaning up old log files they are first backed up and the next time removed.
export function cleanUpLogDirectory(): void {
  const oldLogFileNames = ['frontend-renderer.log'];

  oldLogFileNames.forEach((fileName) => {
    const filePath = path.join(getLogsDirectory(), fileName);
    rotateOrDeleteFile(filePath);
  });
}

export function rotateOrDeleteFile(filePath: string) {
  const backupFilePath = getBackupFilePath(filePath);
  if (!backupLogFile(filePath) && fileExists(backupFilePath)) {
    fs.unlinkSync(backupFilePath);
  }
}