// Leveled logger. Outputs to console with [AutistMask] prefix. // Level is DEBUG when the compile-time DEBUG constant is true or the runtime // debugMode state flag is enabled. The runtime flag is checked lazily so it // responds immediately when toggled in settings. const { DEBUG } = require("./constants"); const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 }; // Runtime debug mode flag — set by settings.js when the user toggles debug // mode via the easter egg. Kept here as a simple mutable reference so it can // be updated without circular dependency issues with state.js. let _runtimeDebug = false; function setRuntimeDebug(enabled) { _runtimeDebug = enabled; } function isDebug() { return DEBUG || _runtimeDebug; } function emit(level, method, args) { const threshold = isDebug() ? LEVELS.debug : LEVELS.info; if (LEVELS[level] >= threshold) { console[method]("[AutistMask]", ...args); } } const log = { debugf(...args) { emit("debug", "log", args); }, infof(...args) { emit("info", "log", args); }, warnf(...args) { emit("warn", "warn", args); }, errorf(...args) { emit("error", "error", args); }, }; // Fetch wrapper that debug-logs every request and response. async function debugFetch(url, opts) { const method = (opts && opts.method) || "GET"; const body = opts && opts.body; log.debugf("fetch →", method, url, body || ""); const resp = await fetch(url, opts); log.debugf("fetch ←", resp.status, url); return resp; } module.exports = { log, debugFetch, setRuntimeDebug, isDebug };