refactor: debugLog for frontend (#5587)

This commit is contained in:
Sline
2025-11-25 10:01:04 +08:00
committed by GitHub
parent 64727ad660
commit caca8b2bc4
24 changed files with 181 additions and 99 deletions
+2 -2
View File
@@ -59,7 +59,7 @@ export class SystemMonitorValidator implements ISystemMonitorOverviewValidator {
* 清理和修复数据,确保返回有效的ISystemMonitorOverview
*/
sanitize(data: any): ISystemMonitorOverview {
// console.log("[DataValidator] 开始数据清理:", data);
// debugLog("[DataValidator] 开始数据清理:", data);
const sanitized: ISystemMonitorOverview = {
traffic: this.sanitizeTrafficData(data?.traffic),
@@ -67,7 +67,7 @@ export class SystemMonitorValidator implements ISystemMonitorOverviewValidator {
overall_status: this.sanitizeOverallStatus(data?.overall_status),
};
// console.log("[DataValidator] 数据清理完成:", sanitized);
// debugLog("[DataValidator] 数据清理完成:", sanitized);
return sanitized;
}
+64
View File
@@ -0,0 +1,64 @@
const envVarValue = (import.meta.env.VITE_ENABLE_DEBUG_LOGS ?? "").toString();
let runtimeOverride: boolean | null = null;
const parseStringFlag = (value: string) => {
if (!value) return false;
const normalized = value.trim().toLowerCase();
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 isEnvDebugEnabled = () => {
if (runtimeOverride !== null) {
return runtimeOverride;
}
if (cachedDebugEnabled !== null) {
return cachedDebugEnabled;
}
cachedDebugEnabled = computeDebugEnabled();
return cachedDebugEnabled;
};
export const setDebugLoggingEnabled = (enabled: boolean) => {
runtimeOverride = enabled;
cachedDebugEnabled = enabled;
};
export const isDebugLoggingEnabled = () => isEnvDebugEnabled();
export const debugLog = (...args: any[]) => {
if (!isEnvDebugEnabled()) return;
console.log(...args);
};
+3 -1
View File
@@ -1,9 +1,11 @@
import { debugLog } from "@/utils/debug";
export const isValidUrl = (url: string) => {
try {
new URL(url);
return true;
} catch (e) {
console.log(e);
debugLog(e);
return false;
}
};
+3 -1
View File
@@ -1,5 +1,7 @@
import { KeyboardEvent } from "react";
import { debugLog } from "@/utils/debug";
const KEY_MAP: Record<string, string> = {
// Option + 特殊字符映射
"–": "Minus", // Option + -
@@ -62,7 +64,7 @@ export const parseHotkey = (keyEvent: KeyboardEvent) => {
} else if (temp.endsWith("RIGHT")) {
temp = temp.slice(0, -5);
}
console.log(temp, mapKeyCombination(temp));
debugLog(temp, mapKeyCombination(temp));
switch (temp) {
case "CONTROL":