diff --git a/CHANGELOG.md b/CHANGELOG.md index 5733718..d205ead 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 - ListenBrainz integration added (thanks @Mar0xy) diff --git a/src/features/listenbrainz/listenbrainz.ts b/src/features/listenbrainz/listenbrainz.ts index 1032a72..ae6ecc0 100644 --- a/src/features/listenbrainz/listenbrainz.ts +++ b/src/features/listenbrainz/listenbrainz.ts @@ -1,5 +1,4 @@ import axios from "axios"; -import { ipcRenderer } from "electron"; import Store from "electron-store"; import { settings } from "../../constants/settings"; import { MediaStatus } from "../../models/mediaStatus"; @@ -125,8 +124,7 @@ export class ListenBrainz { } } } catch (error) { - const logger = new Logger(ipcRenderer); - logger.log(JSON.stringify(error)); + Logger.log(JSON.stringify(error)); } } } diff --git a/src/features/logger.ts b/src/features/logger.ts index 11868f0..2aa184e 100644 --- a/src/features/logger.ts +++ b/src/features/logger.ts @@ -1,32 +1,50 @@ -import { IpcMain, IpcRenderer } from "electron"; +import { IpcMain, ipcRenderer, ipcMain } from "electron"; import { globalEvents } from "../constants/globalEvents"; 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 * @param ipcMain main thread IPC client so we can subscribe to events */ public static watch(ipcMain: IpcMain) { - ipcMain.on(globalEvents.log, (event, content, object) => { - console.log(content, JSON.stringify(object, null, 2)); + ipcMain.on(globalEvents.log, (event, message) => { + const { content, object } = message; + this.logToSTDOut(content, object); }); } - /** * Log content to STDOut * @param content * @param object js(on) object that will be prettyPrinted */ - public log(content: string, object: object = {}) { - if (this.ipcRenderer) { - this.ipcRenderer.send(globalEvents.log, { content, object }); + public static log(content: string, object: object = {}) { + if (ipcRenderer) { + 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) : ""); } }