mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
refactor(debug): simplify toggle flow and unify flag handling
This commit is contained in:
@@ -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: string) => {
|
||||
if (!value) return false;
|
||||
const normalized = value.trim().toLowerCase();
|
||||
const parseStringFlag = (value: unknown) => {
|
||||
const normalized = String(value ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
if (!normalized) return false;
|
||||
return normalized === "1" || normalized === "true" || normalized === "yes";
|
||||
};
|
||||
|
||||
let cachedDebugEnabled: boolean | null = null;
|
||||
|
||||
const computeDebugEnabled = () => {
|
||||
if (import.meta.env.DEV) {
|
||||
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 readGlobalFlag = (): boolean | null => {
|
||||
if (typeof window === "undefined") return null;
|
||||
const flag = (window as any).__VERGE_ENABLE_DEBUG_LOGS__;
|
||||
return typeof flag === "boolean" ? flag : null;
|
||||
};
|
||||
|
||||
const isEnvDebugEnabled = () => {
|
||||
if (runtimeOverride !== null) {
|
||||
return runtimeOverride;
|
||||
const readStoredFlag = (): boolean | null => {
|
||||
if (typeof window === "undefined") return null;
|
||||
try {
|
||||
const stored = window.localStorage?.getItem("VERGE_DEBUG_LOGS");
|
||||
return stored ? parseStringFlag(stored) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
if (cachedDebugEnabled !== null) {
|
||||
return cachedDebugEnabled;
|
||||
}
|
||||
const computeDebugEnabled = (): boolean => {
|
||||
if (import.meta.env.DEV) return true;
|
||||
if (parseStringFlag(import.meta.env.VITE_ENABLE_DEBUG_LOGS)) return true;
|
||||
|
||||
cachedDebugEnabled = computeDebugEnabled();
|
||||
return cachedDebugEnabled;
|
||||
const globalFlag = readGlobalFlag();
|
||||
if (globalFlag !== null) return globalFlag;
|
||||
|
||||
const storedFlag = readStoredFlag();
|
||||
if (storedFlag !== null) return storedFlag;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const setDebugLoggingEnabled = (enabled: boolean) => {
|
||||
@@ -56,9 +51,16 @@ export const setDebugLoggingEnabled = (enabled: boolean) => {
|
||||
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[]) => {
|
||||
if (!isEnvDebugEnabled()) return;
|
||||
if (!isDebugLoggingEnabled()) return;
|
||||
console.log(...args);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user