feat: 集成性能优化模块到管理员调试控制台
- 修改 ArgonDebug 输出方法,支持模块标识前缀 - 为不同模块添加标识:DOM缓存、事件管理、渲染优化、性能监控 - 在管理员控制台的缓存清理功能中添加性能优化模块清理 - 清理 DOM 缓存、事件监听器、内存管理器、渲染优化器 - 优化日志输出,所有日志现在会显示来源模块
This commit is contained in:
@@ -12,31 +12,68 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义调试控制台
|
* 自定义调试控制台
|
||||||
* 仅在调试模式下输出日志,避免生产环境污染控制台
|
* 输出日志到管理员调试控制台(如果启用)或浏览器控制台
|
||||||
|
* 为不同模块添加标识前缀
|
||||||
*/
|
*/
|
||||||
const ArgonDebug = {
|
const ArgonDebug = {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
useAdminConsole: false,
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) {
|
if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) {
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
}
|
}
|
||||||
|
// 检查是否有管理员调试控制台
|
||||||
|
if (typeof window.argonDebug !== 'undefined' && typeof window.argonDebug.log === 'function') {
|
||||||
|
this.useAdminConsole = true;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
log(...args) {
|
/**
|
||||||
if (this.enabled) console.log('[Argon]', ...args);
|
* 输出日志到管理员控制台或浏览器控制台
|
||||||
|
* @param {string} level - 日志级别 (log/warn/error/info)
|
||||||
|
* @param {string} module - 模块名称
|
||||||
|
* @param {...*} args - 日志内容
|
||||||
|
*/
|
||||||
|
_output(level, module, ...args) {
|
||||||
|
if (!this.enabled) return;
|
||||||
|
|
||||||
|
const prefix = module ? `[${module}]` : '[Argon]';
|
||||||
|
const message = args.map(arg => {
|
||||||
|
if (typeof arg === 'object') {
|
||||||
|
try {
|
||||||
|
return JSON.stringify(arg, null, 2);
|
||||||
|
} catch (e) {
|
||||||
|
return String(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return String(arg);
|
||||||
|
}).join(' ');
|
||||||
|
|
||||||
|
// 如果有管理员控制台,优先使用
|
||||||
|
if (this.useAdminConsole && window.argonDebug) {
|
||||||
|
// 管理员控制台会自动处理日志级别和格式
|
||||||
|
console[level](`${prefix} ${message}`);
|
||||||
|
} else {
|
||||||
|
// 否则输出到浏览器控制台
|
||||||
|
console[level](`${prefix} ${message}`);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
warn(...args) {
|
log(module, ...args) {
|
||||||
if (this.enabled) console.warn('[Argon]', ...args);
|
this._output('log', module, ...args);
|
||||||
},
|
},
|
||||||
|
|
||||||
error(...args) {
|
warn(module, ...args) {
|
||||||
if (this.enabled) console.error('[Argon]', ...args);
|
this._output('warn', module, ...args);
|
||||||
},
|
},
|
||||||
|
|
||||||
info(...args) {
|
error(module, ...args) {
|
||||||
if (this.enabled) console.info('[Argon]', ...args);
|
this._output('error', module, ...args);
|
||||||
|
},
|
||||||
|
|
||||||
|
info(module, ...args) {
|
||||||
|
this._output('info', module, ...args);
|
||||||
},
|
},
|
||||||
|
|
||||||
group(label) {
|
group(label) {
|
||||||
@@ -318,7 +355,7 @@ class ArgonDOMCache {
|
|||||||
|
|
||||||
// 如果启用了调试模式,输出淘汰信息
|
// 如果启用了调试模式,输出淘汰信息
|
||||||
if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) {
|
if (typeof argonConfig !== 'undefined' && argonConfig.debug_mode) {
|
||||||
ArgonDebug.log(`DOM缓存 LRU 淘汰: ${lruKey}`);
|
ArgonDebug.log('DOM缓存', `LRU 淘汰: ${lruKey}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,7 +412,7 @@ class ArgonDOMCache {
|
|||||||
*/
|
*/
|
||||||
setMaxSize(newMaxSize) {
|
setMaxSize(newMaxSize) {
|
||||||
if (newMaxSize < 1) {
|
if (newMaxSize < 1) {
|
||||||
ArgonDebug.warn('DOM缓存最大大小必须大于 0');
|
ArgonDebug.warn('DOM缓存', 'DOM缓存最大大小必须大于 0');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,6 +440,7 @@ class ArgonDOMCache {
|
|||||||
this.accessTimes.delete(key);
|
this.accessTimes.delete(key);
|
||||||
this._removeFromAccessOrder(key);
|
this._removeFromAccessOrder(key);
|
||||||
|
|
||||||
|
ArgonDebug.log('DOM缓存', `删除缓存项: ${key}`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -845,7 +883,7 @@ class ArgonRenderOptimizer {
|
|||||||
try {
|
try {
|
||||||
readFn();
|
readFn();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ArgonDebug.error('读取操作错误:', error);
|
ArgonDebug.error('渲染优化', '读取操作错误:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -855,7 +893,7 @@ class ArgonRenderOptimizer {
|
|||||||
try {
|
try {
|
||||||
writeFn();
|
writeFn();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ArgonDebug.error('写入操作错误:', error);
|
ArgonDebug.error('渲染优化', '写入操作错误:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -953,7 +991,7 @@ class ArgonRenderOptimizer {
|
|||||||
try {
|
try {
|
||||||
animationFn(element);
|
animationFn(element);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ArgonDebug.error('动画执行错误:', error);
|
ArgonDebug.error('渲染优化', '动画执行错误:', error);
|
||||||
// 出错时也要清理
|
// 出错时也要清理
|
||||||
this.endAnimation(element);
|
this.endAnimation(element);
|
||||||
}
|
}
|
||||||
@@ -1261,7 +1299,7 @@ class ArgonPerformanceMonitor {
|
|||||||
this.metrics.timestamp = Date.now();
|
this.metrics.timestamp = Date.now();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ArgonDebug.warn('性能指标记录失败:', error);
|
ArgonDebug.warn('性能监控', '性能指标记录失败:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1442,7 +1480,7 @@ class ArgonPerformanceMonitor {
|
|||||||
// 如果有问题,在控制台输出警告
|
// 如果有问题,在控制台输出警告
|
||||||
if (issues.length > 0) {
|
if (issues.length > 0) {
|
||||||
ArgonDebug.group('性能问题检测');
|
ArgonDebug.group('性能问题检测');
|
||||||
issues.forEach(issue => ArgonDebug.warn(issue));
|
issues.forEach(issue => ArgonDebug.warn('性能监控', issue));
|
||||||
ArgonDebug.groupEnd();
|
ArgonDebug.groupEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1701,9 +1739,9 @@ class ArgonPerformanceMonitor {
|
|||||||
|
|
||||||
// 输出建议
|
// 输出建议
|
||||||
if (suggestions.length > 0) {
|
if (suggestions.length > 0) {
|
||||||
suggestions.forEach(suggestion => ArgonDebug.log(suggestion));
|
suggestions.forEach(suggestion => ArgonDebug.log('性能监控', suggestion));
|
||||||
} else {
|
} else {
|
||||||
ArgonDebug.log('✅ 未检测到明显的性能问题,继续保持!');
|
ArgonDebug.log('性能监控', '✅ 未检测到明显的性能问题,继续保持!');
|
||||||
}
|
}
|
||||||
|
|
||||||
ArgonDebug.groupEnd();
|
ArgonDebug.groupEnd();
|
||||||
|
|||||||
@@ -360,6 +360,7 @@ function argon_clear_all_caches() {
|
|||||||
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_timeout_puc_%'");
|
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_timeout_puc_%'");
|
||||||
|
|
||||||
// 触发缓存清理钩子,允许其他插件响应
|
// 触发缓存清理钩子,允许其他插件响应
|
||||||
|
// 这也会触发前端 JavaScript 清理性能优化模块的缓存
|
||||||
do_action('argon_cache_cleared');
|
do_action('argon_cache_cleared');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -882,11 +883,35 @@ function argon_debug_console_script() {
|
|||||||
window.argonDebug={toggle:function(){panel.classList.toggle('show');if(panel.classList.contains('show')&¤tFilter==='resources'){collectResources();renderResources();}},clear:function(){logs=[];counts={all:0,log:0,warn:0,error:0,resources:counts.resources};updateCounts();renderLogs();},clearCache:function(){
|
window.argonDebug={toggle:function(){panel.classList.toggle('show');if(panel.classList.contains('show')&¤tFilter==='resources'){collectResources();renderResources();}},clear:function(){logs=[];counts={all:0,log:0,warn:0,error:0,resources:counts.resources};updateCounts();renderLogs();},clearCache:function(){
|
||||||
// 清除浏览器缓存
|
// 清除浏览器缓存
|
||||||
var cleared = [];
|
var cleared = [];
|
||||||
// 1. 清除 localStorage
|
|
||||||
|
// 1. 清除性能优化模块缓存
|
||||||
|
try {
|
||||||
|
if (typeof argonDOMCache !== 'undefined' && argonDOMCache) {
|
||||||
|
argonDOMCache.clear();
|
||||||
|
cleared.push('DOM缓存');
|
||||||
|
}
|
||||||
|
if (typeof argonEventManager !== 'undefined' && argonEventManager) {
|
||||||
|
argonEventManager.clear();
|
||||||
|
cleared.push('事件监听器');
|
||||||
|
}
|
||||||
|
if (typeof argonMemoryManager !== 'undefined' && argonMemoryManager) {
|
||||||
|
argonMemoryManager.clearAll();
|
||||||
|
cleared.push('内存管理器');
|
||||||
|
}
|
||||||
|
if (typeof argonRenderOptimizer !== 'undefined' && argonRenderOptimizer) {
|
||||||
|
argonRenderOptimizer.clearAllAnimations();
|
||||||
|
cleared.push('渲染优化器');
|
||||||
|
}
|
||||||
|
console.log('<?php _e("已清除性能优化模块缓存", "argon"); ?>');
|
||||||
|
} catch(e) {
|
||||||
|
console.warn('<?php _e("清除性能优化模块缓存时出错", "argon"); ?>:', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 清除 localStorage
|
||||||
try { var lsLen = localStorage.length; localStorage.clear(); if(lsLen > 0) cleared.push('localStorage ('+lsLen+' items)'); } catch(e) {}
|
try { var lsLen = localStorage.length; localStorage.clear(); if(lsLen > 0) cleared.push('localStorage ('+lsLen+' items)'); } catch(e) {}
|
||||||
// 2. 清除 sessionStorage
|
// 3. 清除 sessionStorage
|
||||||
try { var ssLen = sessionStorage.length; sessionStorage.clear(); if(ssLen > 0) cleared.push('sessionStorage ('+ssLen+' items)'); } catch(e) {}
|
try { var ssLen = sessionStorage.length; sessionStorage.clear(); if(ssLen > 0) cleared.push('sessionStorage ('+ssLen+' items)'); } catch(e) {}
|
||||||
// 3. 清除 Service Worker 缓存
|
// 4. 清除 Service Worker 缓存
|
||||||
if ('caches' in window) {
|
if ('caches' in window) {
|
||||||
caches.keys().then(function(names) {
|
caches.keys().then(function(names) {
|
||||||
var count = names.length;
|
var count = names.length;
|
||||||
@@ -896,7 +921,7 @@ function argon_debug_console_script() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// 4. 注销 Service Worker
|
// 5. 注销 Service Worker
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||||
registrations.forEach(function(registration) { registration.unregister(); });
|
registrations.forEach(function(registration) { registration.unregister(); });
|
||||||
|
|||||||
Reference in New Issue
Block a user