summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main/logging.ts
blob: 7029625d3f9e1efa01b3ce23224a1534449df446 (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
93
94
95
96
97
98
99
100
101
102
103
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 = ['frontend-renderer.log'];

export class FileOutput implements ILogOutput {
  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}`);
    }
  }

  public dispose() {
    if (this.fileDescriptor) {
      try {
        fs.closeSync(this.fileDescriptor);
      } catch (e) {
        console.error(`Failed to close ${this.filePath}`);
      }
    }
  }

  public write(_level: LogLevel, message: string) {
    if (this.fileDescriptor) {
      fs.write(this.fileDescriptor, `${message}\n`, (err) => {
        if (err) {
          console.error(`Failed to log to ${this.filePath}`);
        }
      });
    }
  }
}

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(), 'main.log');
}

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

export function createLoggingDirectory(): void {
  try {
    fs.mkdirSync(getLogDirectoryDir(), { recursive: true });
  } catch (e) {
    console.error('Failed to create logging directory');
  }
}

// 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): boolean {
  const backupFilePath = getBackupFilePath(filePath);
  try {
    fs.accessSync(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}`);
    }
  }
}

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');
}