Skip to content

Commit

Permalink
Added optional args to all logging functions. (#684)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kgar authored Jul 24, 2024
1 parent 5f9a32b commit 8eadccd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
29 changes: 22 additions & 7 deletions src/sheets/actor/SkillsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down
60 changes: 50 additions & 10 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// ================================
// Logger utility
// ================================
// export let debugEnabled = 0;

import { CONSTANTS } from 'src/constants';
import { SettingsProvider } from 'src/settings/settings';
Expand All @@ -10,34 +9,75 @@ 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 {
console.log(formattedMsg);
}
}
}

export function log(message: string, args?: any) {
message = `${CONSTANTS.MODULE_ID} | ${message}`;
console.log(message.replace('<br>', '\n'), args);

const formattedLog = message.replace('<br>', '\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('<br>', '\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('<br>', '\n'));

if (notify) {
ui.notifications?.info(info);
}

const formattedInfo = info.replace('<br>', '\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('<br>', '\n'));

if (notify) {
ui.notifications?.warn(warning);
}

const formattedWarning = warning.replace('<br>', '\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);
}
}

0 comments on commit 8eadccd

Please sign in to comment.