generated from League-of-Foundry-Developers/FoundryVTT-Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from p4535992/master
add some helpers
- Loading branch information
Showing
4 changed files
with
957 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import CONSTANTS from "../constants/constants"; | ||
|
||
// ================================ | ||
// Logger utility | ||
// ================================ | ||
export default class Logger { | ||
static get DEBUG() { | ||
return ( | ||
game.settings.get(CONSTANTS.MODULE_ID, "debug") || | ||
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(CONSTANTS.MODULE_ID, "boolean") | ||
); | ||
} | ||
// export let debugEnabled = 0; | ||
// 0 = none, warnings = 1, debug = 2, all = 3 | ||
|
||
static debug(msg, ...args) { | ||
try { | ||
if ( | ||
game.settings.get(CONSTANTS.MODULE_ID, "debug") || | ||
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(CONSTANTS.MODULE_ID, "boolean") | ||
) { | ||
console.log(`DEBUG | ${CONSTANTS.MODULE_ID} | ${msg}`, ...args); | ||
} | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return msg; | ||
} | ||
|
||
static logObject(...args) { | ||
return this.log("", args); | ||
} | ||
|
||
static log(message, ...args) { | ||
try { | ||
message = `${CONSTANTS.MODULE_ID} | ${message}`; | ||
console.log(message.replace("<br>", "\n"), ...args); | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return message; | ||
} | ||
|
||
static notify(message, ...args) { | ||
try { | ||
message = `${CONSTANTS.MODULE_ID} | ${message}`; | ||
ui.notifications?.notify(message); | ||
console.log(message.replace("<br>", "\n"), ...args); | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return message; | ||
} | ||
|
||
static info(info, notify = false, ...args) { | ||
try { | ||
info = `${CONSTANTS.MODULE_ID} | ${info}`; | ||
if (notify) { | ||
ui.notifications?.info(info); | ||
} | ||
console.log(info.replace("<br>", "\n"), ...args); | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return info; | ||
} | ||
|
||
static warn(warning, notify = false, ...args) { | ||
try { | ||
warning = `${CONSTANTS.MODULE_ID} | ${warning}`; | ||
if (notify) { | ||
ui.notifications?.warn(warning); | ||
} | ||
console.warn(warning.replace("<br>", "\n"), ...args); | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return warning; | ||
} | ||
|
||
static errorObject(...args) { | ||
return this.error("", false, args); | ||
} | ||
|
||
static error(error, notify = true, ...args) { | ||
try { | ||
error = `${CONSTANTS.MODULE_ID} | ${error}`; | ||
if (notify) { | ||
ui.notifications?.error(error); | ||
} | ||
console.error(error.replace("<br>", "\n"), ...args); | ||
} catch (e) { | ||
console.error(e.message); | ||
} | ||
return new Error(error.replace("<br>", "\n")); | ||
} | ||
|
||
static timelog(message) { | ||
warn(Date.now(), message); | ||
} | ||
|
||
static i18n = (key) => { | ||
return game.i18n.localize(key)?.trim(); | ||
}; | ||
|
||
static i18nFormat = (key, data = {}) => { | ||
return game.i18n.format(key, data)?.trim(); | ||
}; | ||
|
||
// setDebugLevel = (debugText): void => { | ||
// debugEnabled = { none: 0, warn: 1, debug: 2, all: 3 }[debugText] || 0; | ||
// // 0 = none, warnings = 1, debug = 2, all = 3 | ||
// if (debugEnabled >= 3) CONFIG.debug.hooks = true; | ||
// }; | ||
|
||
static dialogWarning(message, icon = "fas fa-exclamation-triangle") { | ||
return `<p class="${CONSTANTS.MODULE_ID}-dialog"> | ||
<i style="font-size:3rem;" class="${icon}"></i><br><br> | ||
<strong style="font-size:1.2rem;">${CONSTANTS.MODULE_ID}</strong> | ||
<br><br>${message} | ||
</p>`; | ||
} | ||
} |
Oops, something went wrong.