refactor(debug): simplify toggle flow and unify flag handling

This commit is contained in:
Slinetrac 2025-12-06 11:36:47 +08:00
parent 90b1c6e153
commit eb7d22f610
No known key found for this signature in database

View File

@ -1,54 +1,49 @@
const envVarValue = (import.meta.env.VITE_ENABLE_DEBUG_LOGS ?? "").toString(); /**
* Debug logging is enabled when:
* - dev build (`import.meta.env.DEV`)
* - env flag `VITE_ENABLE_DEBUG_LOGS` is truthy (1/true/yes)
* - page sets `window.__VERGE_ENABLE_DEBUG_LOGS__ = true`
* - localStorage item `VERGE_DEBUG_LOGS` is truthy (1/true/yes)
* Use `setDebugLoggingEnabled` to force-enable/disable at runtime.
*/
let runtimeOverride: boolean | undefined;
let cachedDebugEnabled: boolean | undefined;
let runtimeOverride: boolean | null = null; const parseStringFlag = (value: unknown) => {
const normalized = String(value ?? "")
const parseStringFlag = (value: string) => { .trim()
if (!value) return false; .toLowerCase();
const normalized = value.trim().toLowerCase(); if (!normalized) return false;
return normalized === "1" || normalized === "true" || normalized === "yes"; return normalized === "1" || normalized === "true" || normalized === "yes";
}; };
let cachedDebugEnabled: boolean | null = null; const readGlobalFlag = (): boolean | null => {
if (typeof window === "undefined") return null;
const computeDebugEnabled = () => { const flag = (window as any).__VERGE_ENABLE_DEBUG_LOGS__;
if (import.meta.env.DEV) { return typeof flag === "boolean" ? flag : null;
return true;
}
if (parseStringFlag(envVarValue)) {
return true;
}
if (typeof window !== "undefined") {
const globalFlag = (window as any).__VERGE_ENABLE_DEBUG_LOGS__;
if (typeof globalFlag === "boolean") {
return globalFlag;
}
try {
const stored = window.localStorage?.getItem("VERGE_DEBUG_LOGS");
if (stored) {
return parseStringFlag(stored);
}
} catch {
// ignore storage access errors
}
}
return false;
}; };
const isEnvDebugEnabled = () => { const readStoredFlag = (): boolean | null => {
if (runtimeOverride !== null) { if (typeof window === "undefined") return null;
return runtimeOverride; try {
const stored = window.localStorage?.getItem("VERGE_DEBUG_LOGS");
return stored ? parseStringFlag(stored) : null;
} catch {
return null;
} }
};
if (cachedDebugEnabled !== null) { const computeDebugEnabled = (): boolean => {
return cachedDebugEnabled; if (import.meta.env.DEV) return true;
} if (parseStringFlag(import.meta.env.VITE_ENABLE_DEBUG_LOGS)) return true;
cachedDebugEnabled = computeDebugEnabled(); const globalFlag = readGlobalFlag();
return cachedDebugEnabled; if (globalFlag !== null) return globalFlag;
const storedFlag = readStoredFlag();
if (storedFlag !== null) return storedFlag;
return false;
}; };
export const setDebugLoggingEnabled = (enabled: boolean) => { export const setDebugLoggingEnabled = (enabled: boolean) => {
@ -56,9 +51,16 @@ export const setDebugLoggingEnabled = (enabled: boolean) => {
cachedDebugEnabled = enabled; cachedDebugEnabled = enabled;
}; };
export const isDebugLoggingEnabled = () => isEnvDebugEnabled(); export const isDebugLoggingEnabled = () =>
runtimeOverride ??
cachedDebugEnabled ??
(cachedDebugEnabled = computeDebugEnabled());
/**
* Logs to the console only when debug logging is enabled.
* Forwards all arguments to `console.log`; does nothing otherwise.
*/
export const debugLog = (...args: any[]) => { export const debugLog = (...args: any[]) => {
if (!isEnvDebugEnabled()) return; if (!isDebugLoggingEnabled()) return;
console.log(...args); console.log(...args);
}; };