summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main/logging.ts
blob: 79a42b35c15c3081673470708f658909d14810a7 (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
92
import { app } from 'electron';
import fs from 'fs';
import path from 'path';
import { IpcMainEventChannel } from './ipc-event-channel';
import { LogLevel, ILogInput, ILogOutput } from '../shared/logging-types';

export const OLD_LOG_FILES = ['main.log', 'renderer.log', 'frontend.log'];

export class FileOutput implements ILogOutput {
  private fileDescriptor: number;

  constructor(public level: LogLevel, filePath: string) {
    this.fileDescriptor = fs.openSync(filePath, fs.constants.O_CREAT | fs.constants.O_WRONLY);
  }

  public dispose() {
    fs.closeSync(this.fileDescriptor);
  }

  public write(_level: LogLevel, message: string): Promise<void> {
    return new Promise((resolve, reject) => {
      fs.write(this.fileDescriptor, `${message}\n`, (err) => {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    });
  }
}

export class IpcInput implements ILogInput {
  public on(handler: (level: LogLevel, message: string) => void) {
    IpcMainEventChannel.logging.handleLog(({ level, message }) => handler(level, message));
  }
}

export function getMainLogPath() {
  return path.join(getLogDirectoryDir(), 'frontend-main.log');
}

export function getRendererLogPath() {
  return path.join(getLogDirectoryDir(), 'frontend-renderer.log');
}

export function createLoggingDirectory(): void {
  fs.mkdirSync(getLogDirectoryDir(), { recursive: true });
}

// When cleaning up old log files they are first backed up and the next time removed.
export function cleanUpLogDirectory(fileNames: string[]): void {
  fileNames.forEach((fileName) => {
    const filePath = path.join(getLogDirectoryDir(), fileName);
    rotateOrDeleteFile(filePath);
  });
}

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

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

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

function getLogDirectoryDir() {
  return app.getPath('logs');
}

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