From 8eadccd9206108de759018051d6a7ec4ec476447 Mon Sep 17 00:00:00 2001 From: Kevin Lee Garner Date: Tue, 23 Jul 2024 21:47:16 -0500 Subject: [PATCH] Added optional args to all logging functions. (#684) Added guard code to prevent custom skills that are in CONFIG.DND5E.skills but not on the actor from crashing the resident sheet tab. Added warning log advising of the missing skill. --- src/sheets/actor/SkillsList.svelte | 29 +++++++++++---- src/utils/logging.ts | 60 +++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/sheets/actor/SkillsList.svelte b/src/sheets/actor/SkillsList.svelte index 70ec83170..fdba27beb 100644 --- a/src/sheets/actor/SkillsList.svelte +++ b/src/sheets/actor/SkillsList.svelte @@ -7,6 +7,7 @@ import type { CharacterSheetContext, NpcSheetContext } from 'src/types/types'; import { ActiveEffectsHelper } from 'src/utils/active-effect'; import { formatAsModifier } from 'src/utils/formatting'; + import { warn } from 'src/utils/logging'; import { getContext } from 'svelte'; import type { Readable } from 'svelte/store'; @@ -29,17 +30,31 @@ }; let skillRefs: SkillRef[]; - $: skillRefs = Array.from(Object.entries($context.config.skills)).map( - ([key, { label }]: [string, any]) => { + $: { + skillRefs = Array.from(Object.entries($context.config.skills)).reduce< + SkillRef[] + >((prev, [key, configSkill]: [string, any]) => { const skill = getSkill(key); - return { + + if (!skill) { + warn( + 'Unable to find skill. Ensure custom skills are added at "init" time.', + false, + { key, configSkill }, + ); + return prev; + } + + prev.push({ key: key, - label: label, + label: configSkill.label, ability: skill.ability, skill: skill, - }; - }, - ); + }); + + return prev; + }, []); + } $: abilities = FoundryAdapter.getAbilitiesAsDropdownOptions( $context.abilities, diff --git a/src/utils/logging.ts b/src/utils/logging.ts index c32419175..74ceb0b68 100644 --- a/src/utils/logging.ts +++ b/src/utils/logging.ts @@ -1,7 +1,6 @@ // ================================ // Logger utility // ================================ -// export let debugEnabled = 0; import { CONSTANTS } from 'src/constants'; import { SettingsProvider } from 'src/settings/settings'; @@ -10,6 +9,7 @@ import { SettingsProvider } from 'src/settings/settings'; export function debug(msg: string, args?: any) { if (SettingsProvider.settings.debug.get()) { let formattedMsg = `DEBUG | ${CONSTANTS.MODULE_ID} | ${msg}`; + if (args !== undefined) { console.log(formattedMsg, args); } else { @@ -17,27 +17,67 @@ export function debug(msg: string, args?: any) { } } } + export function log(message: string, args?: any) { message = `${CONSTANTS.MODULE_ID} | ${message}`; - console.log(message.replace('
', '\n'), args); + + const formattedLog = message.replace('
', '\n'); + + if (args !== undefined) { + console.log(formattedLog, args); + } else { + console.log(formattedLog); + } } + export function notify(message: string) { message = `${CONSTANTS.MODULE_ID} | ${message}`; ui.notifications?.notify(message); console.log(message.replace('
', '\n')); } -export function info(info: string, notify = false) { + +export function info(info: string, notify = false, args?: any) { info = `${CONSTANTS.MODULE_ID} | ${info}`; - if (notify) ui.notifications?.info(info); - console.log(info.replace('
', '\n')); + + if (notify) { + ui.notifications?.info(info); + } + + const formattedInfo = info.replace('
', '\n'); + + if (args !== undefined) { + console.log(formattedInfo, args); + } else { + console.log(formattedInfo); + } } -export function warn(warning: string, notify = false) { + +export function warn(warning: string, notify = false, args?: any) { warning = `${CONSTANTS.MODULE_ID} | ${warning}`; - if (notify) ui.notifications?.warn(warning); - console.warn(warning.replace('
', '\n')); + + if (notify) { + ui.notifications?.warn(warning); + } + + const formattedWarning = warning.replace('
', '\n'); + + if (args !== undefined) { + console.warn(formattedWarning, args); + } else { + console.warn(formattedWarning); + } } + export function error(message: string, notify = true, args?: any) { message = `${CONSTANTS.MODULE_ID} | ${message}`; - if (notify) ui.notifications?.error(message); - console.error(message, args); + + if (notify) { + ui.notifications?.error(message); + } + + if (args !== undefined) { + console.error(message, args); + } else { + console.error(message); + } }