logging.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { clamp } from "@sv443-network/userutils";
  2. import { scriptInfo } from "../constants.js";
  3. import { setGlobalProp } from "../interface.js";
  4. import { LogLevel } from "../types.js";
  5. import { showIconToast } from "src/components/toast.js";
  6. import { t } from "./translations.js";
  7. import { getFeature } from "src/config.js";
  8. let curLogLevel = LogLevel.Info;
  9. /** Common prefix to be able to tell logged messages apart and filter them in devtools */
  10. const consPrefix = `[${scriptInfo.name}]`;
  11. const consPrefixDbg = `[${scriptInfo.name}/#DEBUG]`;
  12. /** Sets the current log level. 0 = Debug, 1 = Info */
  13. export function setLogLevel(level: LogLevel) {
  14. curLogLevel = level;
  15. setGlobalProp("logLevel", level);
  16. if(curLogLevel !== level)
  17. log("Set the log level to", LogLevel[level]);
  18. }
  19. /** Extracts the log level from the last item from spread arguments - returns 0 if the last item is not a number or too low or high */
  20. function getLogLevel(args: unknown[]): number {
  21. const minLogLvl = 0, maxLogLvl = 1;
  22. if(typeof args.at(-1) === "number")
  23. return clamp(
  24. args.splice(args.length - 1)[0] as number,
  25. minLogLvl,
  26. maxLogLvl,
  27. );
  28. return LogLevel.Debug;
  29. }
  30. /**
  31. * Logs all passed values to the console, as long as the log level is sufficient.
  32. * @param args Last parameter is log level (0 = Debug, 1/undefined = Info) - any number as the last parameter will be stripped out! Convert to string if it shouldn't be.
  33. */
  34. export function log(...args: unknown[]): void {
  35. if(curLogLevel <= getLogLevel(args))
  36. console.log(consPrefix, ...args);
  37. }
  38. /**
  39. * Logs all passed values to the console as info, as long as the log level is sufficient.
  40. * @param args Last parameter is log level (0 = Debug, 1/undefined = Info) - any number as the last parameter will be stripped out! Convert to string if it shouldn't be.
  41. */
  42. export function info(...args: unknown[]): void {
  43. if(curLogLevel <= getLogLevel(args))
  44. console.info(consPrefix, ...args);
  45. }
  46. /** Logs all passed values to the console as a warning, no matter the log level. */
  47. export function warn(...args: unknown[]): void {
  48. console.warn(consPrefix, ...args);
  49. }
  50. /** Logs all passed values to the console as an error, no matter the log level. */
  51. export function error(...args: unknown[]): void {
  52. console.error(consPrefix, ...args);
  53. getFeature("showToastOnGenericError")
  54. && showIconToast({
  55. icon: "icon-error",
  56. iconFill: "var(--bytm-error-col)",
  57. message: t("generic_error_toast", args.find(e => e instanceof Error)?.name ?? t("error")),
  58. duration: Math.max(getFeature("toastDuration"), 6) * 1000,
  59. });
  60. }
  61. document.addEventListener("DOMContentLoaded", () => error("Ayo chungus", new TypeError("chungus moment encountered")));
  62. /** Logs all passed values to the console with a debug-specific prefix */
  63. export function dbg(...args: unknown[]): void {
  64. console.log(consPrefixDbg, ...args);
  65. }