refactor: 优化日志输出,仅在调试模式下启用

- 为所有 print 语句添加 kDebugMode 检查
- 更新 KRLogUtil 工具类,Release 模式下禁用日志输出
- 优化 18 个文件中的 335+ 条日志语句
- 提升 Release 版本性能并增强安全性

(cherry picked from commit 301f1510ba81fe94fb08e013ca80b3ca708a2e15)
This commit is contained in:
2025-11-01 05:15:34 -07:00
committed by speakeloudest
parent b3ee1cc6dc
commit 4c5763647d
18 changed files with 665 additions and 232 deletions
@@ -22,6 +22,7 @@ import '../../utils/kr_country_util.dart';
import '../../utils/kr_log_util.dart';
import '../../utils/kr_secure_storage.dart';
import '../../common/app_run_data.dart';
import 'package:flutter/foundation.dart';
enum KRConnectionType {
global,
@@ -549,14 +550,18 @@ class KRSingBoxImp {
/// 订阅状态变化流
/// 参考 hiddify-app: 监听 libcore 发送的状态事件来自动更新 UI
void _kr_subscribeToStatus() {
print('🔵 _kr_subscribeToStatus 被调用,重新订阅状态流');
if (kDebugMode) {
print('🔵 _kr_subscribeToStatus 被调用,重新订阅状态流');
}
KRLogUtil.kr_i('🔵 _kr_subscribeToStatus 被调用', tag: 'SingBox');
// 取消之前的状态订阅
for (var sub in _kr_subscriptions) {
if (sub.hashCode.toString().contains('Status')) {
sub.cancel();
print('🔵 已取消旧的状态订阅');
if (kDebugMode) {
print('🔵 已取消旧的状态订阅');
}
}
}
_kr_subscriptions
@@ -565,19 +570,25 @@ class KRSingBoxImp {
_kr_subscriptions.add(
kr_singBox.watchStatus().listen(
(status) {
print('🔵 收到 Native 状态更新: ${status.runtimeType}');
if (kDebugMode) {
print('🔵 收到 Native 状态更新: ${status.runtimeType}');
}
KRLogUtil.kr_i('📡 收到状态更新: $status', tag: 'SingBox');
kr_status.value = status;
},
onError: (error) {
print('🔵 状态流错误: $error');
if (kDebugMode) {
print('🔵 状态流错误: $error');
}
KRLogUtil.kr_e('📡 状态流错误: $error', tag: 'SingBox');
},
cancelOnError: false,
),
);
print('🔵 状态流订阅完成');
if (kDebugMode) {
print('🔵 状态流订阅完成');
}
}
/// 订阅统计数据流
@@ -1141,7 +1152,9 @@ class KRSingBoxImp {
final selectedNode = await KRSecureStorage().kr_readData(key: _keySelectedNode);
if (selectedNode != null && selectedNode.isNotEmpty) {
KRLogUtil.kr_i('🎯 恢复用户选择的节点: $selectedNode', tag: 'SingBox');
print('🔵 启动后恢复节点选择: $selectedNode');
if (kDebugMode) {
print('🔵 启动后恢复节点选择: $selectedNode');
}
// 延迟500ms确保sing-box完全启动
await Future.delayed(const Duration(milliseconds: 500));
@@ -1150,18 +1163,26 @@ class KRSingBoxImp {
try {
await kr_selectOutbound(selectedNode);
KRLogUtil.kr_i('✅ 节点已切换到用户选择: $selectedNode', tag: 'SingBox');
print('🔵 节点切换成功: $selectedNode');
if (kDebugMode) {
print('🔵 节点切换成功: $selectedNode');
}
} catch (e) {
KRLogUtil.kr_e('❌ 节点切换失败: $e', tag: 'SingBox');
print('🔵 节点切换失败: $e');
if (kDebugMode) {
print('🔵 节点切换失败: $e');
}
}
} else {
KRLogUtil.kr_i('️ 没有保存的节点选择,使用默认配置', tag: 'SingBox');
print('🔵 没有保存的节点选择,使用默认');
if (kDebugMode) {
print('🔵 没有保存的节点选择,使用默认');
}
}
} catch (e) {
KRLogUtil.kr_e('❌ 恢复节点选择失败: $e', tag: 'SingBox');
print('🔵 恢复节点选择失败: $e');
if (kDebugMode) {
print('🔵 恢复节点选择失败: $e');
}
}
});
} catch (e, stackTrace) {