From a987b59b65a81d2b0c2ffe8aaf0bc0b93aee3580 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 26 Jun 2021 14:32:40 +0200 Subject: [PATCH] Add logger that encapsulates console logging --- src/module/actor/sheets/actor-sheet.ts | 4 +-- src/module/hooks/init.ts | 3 +- src/module/logger.ts | 27 ++++++++++++++ src/module/macros/roll-check.ts | 2 +- src/module/macros/roll-generic-check.ts | 2 +- src/module/macros/roll-item.ts | 2 +- src/module/migrations.ts | 5 +-- src/module/migrations/001.ts | 4 ++- src/module/migrations/002.ts | 18 +++++----- src/module/migrations/003.ts | 18 +++++----- src/module/migrations/004.ts | 17 ++++----- src/module/ui/notifications.ts | 47 ++++++++++++------------- 12 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 src/module/logger.ts diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts index 7a2e3299..c133c2c5 100644 --- a/src/module/actor/sheets/actor-sheet.ts +++ b/src/module/actor/sheets/actor-sheet.ts @@ -235,7 +235,7 @@ export class DS4ActorSheet extends ActorSheet> { event.preventDefault(); const id = $(event.currentTarget).parents(".item").data("itemId"); const item = this.actor.getOwnedItem(id); - item.roll(); + item.roll().catch((e) => notifications.error(e, { log: true })); } /** @@ -245,7 +245,7 @@ export class DS4ActorSheet extends ActorSheet> { protected _onRollCheck(event: JQuery.ClickEvent): void { event.preventDefault(); const check = event.currentTarget.dataset["check"]; - this.actor.rollCheck(check); + this.actor.rollCheck(check).catch((e) => notifications.error(e, { log: true })); } /** @override */ diff --git a/src/module/hooks/init.ts b/src/module/hooks/init.ts index 4722da94..b1b4d7da 100644 --- a/src/module/hooks/init.ts +++ b/src/module/hooks/init.ts @@ -6,6 +6,7 @@ import registerHandlebarsHelpers from "../handlebars/handlebars-helpers"; import registerHandlebarsPartials from "../handlebars/handlebars-partials"; import { DS4Item } from "../item/item"; import { DS4ItemSheet } from "../item/item-sheet"; +import logger from "../logger"; import { macros } from "../macros/macros"; import { migration } from "../migrations"; import { DS4Check } from "../rolls/check"; @@ -19,7 +20,7 @@ export default function registerForInitHook(): void { } async function init() { - console.log(`DS4 | Initializing the DS4 Game System\n${DS4.ASCII}`); + logger.info(`Initializing the DS4 Game System\n${DS4.ASCII}`); game.ds4 = { DS4Actor, diff --git a/src/module/logger.ts b/src/module/logger.ts new file mode 100644 index 00000000..cb923e22 --- /dev/null +++ b/src/module/logger.ts @@ -0,0 +1,27 @@ +const loggingContext = "DS4"; +const loggingSeparator = "|"; + +type LogLevel = "debug" | "info" | "warning" | "error"; +type LoggingFunction = (...data: unknown[]) => void; + +class Logger { + readonly debug: LoggingFunction; + readonly info: LoggingFunction; + readonly warn: LoggingFunction; + readonly error: LoggingFunction; + + constructor() { + this.debug = this.getLoggingFunction("debug"); + this.info = this.getLoggingFunction("info"); + this.warn = this.getLoggingFunction("warning"); + this.error = this.getLoggingFunction("error"); + } + + getLoggingFunction(type: LogLevel = "info") { + const log = { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[type]; + return (...data: unknown[]) => log(loggingContext, loggingSeparator, ...data); + } +} + +const logger = new Logger(); +export default logger; diff --git a/src/module/macros/roll-check.ts b/src/module/macros/roll-check.ts index f40ad75b..9e18fb85 100644 --- a/src/module/macros/roll-check.ts +++ b/src/module/macros/roll-check.ts @@ -45,5 +45,5 @@ export async function rollCheck(check: Check): Promise { return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro")); } - return actor.rollCheck(check); + return actor.rollCheck(check).catch((e) => notifications.error(e, { log: true })); } diff --git a/src/module/macros/roll-generic-check.ts b/src/module/macros/roll-generic-check.ts index 75ee9162..3fa5f1bc 100644 --- a/src/module/macros/roll-generic-check.ts +++ b/src/module/macros/roll-generic-check.ts @@ -9,5 +9,5 @@ export async function rollGenericCheck(): Promise { return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro")); } - return actor.rollGenericCheck(); + return actor.rollGenericCheck().catch((e) => notifications.error(e, { log: true })); } diff --git a/src/module/macros/roll-item.ts b/src/module/macros/roll-item.ts index ec85fdde..7b98c916 100644 --- a/src/module/macros/roll-item.ts +++ b/src/module/macros/roll-item.ts @@ -53,5 +53,5 @@ export async function rollItem(itemId: string): Promise { ); } - return item.roll(); + return item.roll().catch((e) => notifications.error(e, { log: true })); } diff --git a/src/module/migrations.ts b/src/module/migrations.ts index b60428cd..86118160 100644 --- a/src/module/migrations.ts +++ b/src/module/migrations.ts @@ -1,3 +1,4 @@ +import logger from "./logger"; import { migrate as migrate001 } from "./migrations/001"; import { migrate as migrate002 } from "./migrations/002"; import { migrate as migrate003 } from "./migrations/003"; @@ -40,7 +41,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion for (const [i, migration] of migrationsToExecute.entries()) { const currentMigrationVersion = oldMigrationVersion + i + 1; - console.log("executing migration script ", currentMigrationVersion); + logger.info("executing migration script ", currentMigrationVersion); try { await migration(); game.settings.set("ds4", "systemMigrationVersion", currentMigrationVersion); @@ -54,7 +55,7 @@ async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion { permanent: true }, ); err.message = `Failed ds4 system migration: ${err.message}`; - console.error(err); + logger.error(err); return; } } diff --git a/src/module/migrations/001.ts b/src/module/migrations/001.ts index 943aecc9..43256289 100644 --- a/src/module/migrations/001.ts +++ b/src/module/migrations/001.ts @@ -1,7 +1,9 @@ +import logger from "../logger"; + export async function migrate(): Promise { for (const a of game.actors?.entities ?? []) { const updateData = getActorUpdateData(); - console.log(`Migrating actor ${a.name}`); + logger.info(`Migrating actor ${a.name}`); await a.update(updateData, { enforceTypes: false }); } } diff --git a/src/module/migrations/002.ts b/src/module/migrations/002.ts index 0f0d30b5..0a267e01 100644 --- a/src/module/migrations/002.ts +++ b/src/module/migrations/002.ts @@ -1,3 +1,5 @@ +import logger from "../logger"; + export async function migrate(): Promise { await migrateItems(); await migrateActors(); @@ -10,12 +12,12 @@ async function migrateItems() { try { const updateData = getItemUpdateData(item._data); if (updateData) { - console.log(`Migrating Item entity ${item.name} (${item.id})`); + logger.info(`Migrating Item entity ${item.name} (${item.id})`); await item.update(updateData), { enforceTypes: false }; } } catch (err) { err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -30,12 +32,12 @@ async function migrateActors() { try { const updateData = getActorUpdateData(actor._data); if (updateData) { - console.log(`Migrating Actor entity ${actor.name} (${actor.id})`); + logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); await actor.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -59,12 +61,12 @@ async function migrateScenes() { try { const updateData = getSceneUpdateData(scene._data); if (updateData) { - console.log(`Migrating Scene entity ${scene.name} (${scene.id})`); + logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); await scene.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -123,12 +125,12 @@ async function migrateCompendium(compendium: Compendium) { }; const updateData = getUpdateData(entity); if (updateData) { - console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); + logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); await compendium.updateEntity({ ...updateData, _id: entity._id }); } } catch (err) { err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`; - console.error(err); + logger.error(err); } } diff --git a/src/module/migrations/003.ts b/src/module/migrations/003.ts index 0fd553a4..c9d5b879 100644 --- a/src/module/migrations/003.ts +++ b/src/module/migrations/003.ts @@ -1,3 +1,5 @@ +import logger from "../logger"; + export async function migrate(): Promise { await migrateItems(); await migrateActors(); @@ -10,12 +12,12 @@ async function migrateItems() { try { const updateData = getItemUpdateData(item._data); if (updateData) { - console.log(`Migrating Item entity ${item.name} (${item.id})`); + logger.info(`Migrating Item entity ${item.name} (${item.id})`); await item.update(updateData), { enforceTypes: false }; } } catch (err) { err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -34,12 +36,12 @@ async function migrateActors() { try { const updateData = getActorUpdateData(actor._data); if (updateData) { - console.log(`Migrating Actor entity ${actor.name} (${actor.id})`); + logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); await actor.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -63,12 +65,12 @@ async function migrateScenes() { try { const updateData = getSceneUpdateData(scene._data); if (updateData) { - console.log(`Migrating Scene entity ${scene.name} (${scene.id})`); + logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); await scene.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -127,12 +129,12 @@ async function migrateCompendium(compendium: Compendium) { }; const updateData = getUpdateData(entity); if (updateData) { - console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); + logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); await compendium.updateEntity({ ...updateData, _id: entity._id }); } } catch (err) { err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`; - console.error(err); + logger.error(err); } } diff --git a/src/module/migrations/004.ts b/src/module/migrations/004.ts index 82c924ad..77e493f0 100644 --- a/src/module/migrations/004.ts +++ b/src/module/migrations/004.ts @@ -1,4 +1,5 @@ import { DS4SpellDataData } from "../item/item-data"; +import logger from "../logger"; export async function migrate(): Promise { await migrateItems(); @@ -12,12 +13,12 @@ async function migrateItems() { try { const updateData = getItemUpdateData(item._data); if (updateData) { - console.log(`Migrating Item entity ${item.name} (${item.id})`); + logger.info(`Migrating Item entity ${item.name} (${item.id})`); await item.update(updateData), { enforceTypes: false }; } } catch (err) { err.message = `Error during migration of Item entity ${item.name} (${item.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -39,12 +40,12 @@ async function migrateActors() { try { const updateData = getActorUpdateData(actor._data); if (updateData) { - console.log(`Migrating Actor entity ${actor.name} (${actor.id})`); + logger.info(`Migrating Actor entity ${actor.name} (${actor.id})`); await actor.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Actor entity ${actor.name} (${actor.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -75,12 +76,12 @@ async function migrateScenes() { try { const updateData = getSceneUpdateData(scene._data); if (updateData) { - console.log(`Migrating Scene entity ${scene.name} (${scene.id})`); + logger.info(`Migrating Scene entity ${scene.name} (${scene.id})`); await scene.update(updateData, { enforceTypes: false }); } } catch (err) { err.message = `Error during migration of Scene entity ${scene.name} (${scene.id}), continuing anyways.`; - console.error(err); + logger.error(err); } } } @@ -140,12 +141,12 @@ async function migrateCompendium(compendium: Compendium) { }; const updateData = getUpdateData(entity); if (updateData) { - console.log(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); + logger.info(`Migrating entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}`); await compendium.updateEntity({ ...updateData, _id: entity._id }); } } catch (err) { err.message = `Error during migration of entity ${entity.name} (${entity.id}) in compendium ${compendium.collection}, continuing anyways.`; - console.error(err); + logger.error(err); } } diff --git a/src/module/ui/notifications.ts b/src/module/ui/notifications.ts index cb6a301f..3c961363 100644 --- a/src/module/ui/notifications.ts +++ b/src/module/ui/notifications.ts @@ -1,35 +1,34 @@ +import logger from "../logger"; + +function getNotificationFunction(type: "info" | "warn" | "error") { + return (message: string, { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}): void => { + if (ui.notifications) { + ui.notifications[type](message, { permanent }); + if (log) { + logger[type](message); + } + } else { + logger[type](message); + } + }; +} + const notifications = { - info: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => { - if (ui.notifications) { - ui.notifications.info(message, { permanent }); - } else { - console.info(message); - } - }, - warn: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => { - if (ui.notifications) { - ui.notifications.warn(message, { permanent }); - } else { - console.log(message); - } - }, - error: (message: string, { permanent = false }: { permanent?: boolean } = {}): void => { - if (ui.notifications) { - ui.notifications.error(message, { permanent }); - } else { - console.warn(message); - } - }, + info: getNotificationFunction("info"), + warn: getNotificationFunction("warn"), + error: getNotificationFunction("error"), notify: ( message: string, type: "info" | "warning" | "error" = "info", - { permanent = false }: { permanent?: boolean } = {}, + { permanent = false, log = false }: { permanent?: boolean; log?: boolean } = {}, ): void => { if (ui.notifications) { ui.notifications.notify(message, type, { permanent }); + if (log) { + logger.getLoggingFunction(type)(message); + } } else { - const log = { info: console.info, warning: console.warn, error: console.error }[type]; - log(message); + logger.getLoggingFunction(type)(message); } }, };