summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared/logging.ts
blob: 4c0af38334b45213ec5b1cc8b4292ed7898ac30c (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
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(), '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;
  }
}