simplified logger

This commit is contained in:
Rick van Lieshout 2023-08-07 15:32:20 +02:00
parent c6dff0b0e5
commit c7931cf913
3 changed files with 38 additions and 17 deletions

View File

@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [next]
- [DEV]:
- Logger is now static and will automatically call either ipcRenderer or ipcMain
## 5.5.0 ## 5.5.0
- ListenBrainz integration added (thanks @Mar0xy) - ListenBrainz integration added (thanks @Mar0xy)

View File

@ -1,5 +1,4 @@
import axios from "axios"; import axios from "axios";
import { ipcRenderer } from "electron";
import Store from "electron-store"; import Store from "electron-store";
import { settings } from "../../constants/settings"; import { settings } from "../../constants/settings";
import { MediaStatus } from "../../models/mediaStatus"; import { MediaStatus } from "../../models/mediaStatus";
@ -125,8 +124,7 @@ export class ListenBrainz {
} }
} }
} catch (error) { } catch (error) {
const logger = new Logger(ipcRenderer); Logger.log(JSON.stringify(error));
logger.log(JSON.stringify(error));
} }
} }
} }

View File

@ -1,32 +1,50 @@
import { IpcMain, IpcRenderer } from "electron"; import { IpcMain, ipcRenderer, ipcMain } from "electron";
import { globalEvents } from "../constants/globalEvents"; import { globalEvents } from "../constants/globalEvents";
export class Logger { export class Logger {
/**
*
* @param ipcRenderer renderer IPC client so we can send messages to the main thread
*/
constructor(private ipcRenderer: IpcRenderer) {}
/** /**
* Subscribe to watch for logs from the IPC client * Subscribe to watch for logs from the IPC client
* @param ipcMain main thread IPC client so we can subscribe to events * @param ipcMain main thread IPC client so we can subscribe to events
*/ */
public static watch(ipcMain: IpcMain) { public static watch(ipcMain: IpcMain) {
ipcMain.on(globalEvents.log, (event, content, object) => { ipcMain.on(globalEvents.log, (event, message) => {
console.log(content, JSON.stringify(object, null, 2)); const { content, object } = message;
this.logToSTDOut(content, object);
}); });
} }
/** /**
* Log content to STDOut * Log content to STDOut
* @param content * @param content
* @param object js(on) object that will be prettyPrinted * @param object js(on) object that will be prettyPrinted
*/ */
public log(content: string, object: object = {}) { public static log(content: string, object: object = {}) {
if (this.ipcRenderer) { if (ipcRenderer) {
this.ipcRenderer.send(globalEvents.log, { content, object }); ipcRenderer.send(globalEvents.log, { content, object });
} else {
ipcMain.emit(globalEvents.log, { content, object });
} }
console.log(`${content} \n ${JSON.stringify(object, null, 2)}`); this.logToSTDOut(content, object);
}
/**
* Log content to STDOut and use the provided alert function to alert
* @param content
* @param object js(on) object that will be prettyPrinted
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static alert(content: string, object: any = {}, alert?: (msg: string) => void) {
Logger.log(content, object);
if (alert) {
alert(`${content} \n\nwith details: \n${JSON.stringify(object, null, 2)}`);
}
}
/**
* Log to STDOut
* @param content
* @param object
*/
private static logToSTDOut(content: string, object = {}) {
console.log(content, Object.keys(object).length > 0 ? JSON.stringify(object, null, 2) : "");
} }
} }