refactor: 优化日志输出,仅在调试模式下启用
- 为所有 print 语句添加 kDebugMode 检查 - 更新 KRLogUtil 工具类,Release 模式下禁用日志输出 - 优化 18 个文件中的 335+ 条日志语句 - 提升 Release 版本性能并增强安全性 (cherry picked from commit 301f1510ba81fe94fb08e013ca80b3ca708a2e15)
This commit is contained in:
@@ -29,6 +29,7 @@ import 'package:kaer_with_panels/app/utils/kr_log_util.dart';
|
||||
import 'package:kaer_with_panels/app/utils/kr_common_util.dart';
|
||||
import 'package:kaer_with_panels/app/utils/kr_secure_storage.dart';
|
||||
import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
/// 订阅服务
|
||||
@@ -498,7 +499,9 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
void _bindConnectionStatus() {
|
||||
// 添加更详细的状态监听
|
||||
ever(KRSingBoxImp.instance.kr_status, (status) {
|
||||
print('🔵 Controller 收到状态变化: ${status.runtimeType}');
|
||||
if (kDebugMode) {
|
||||
print('🔵 Controller 收到状态变化: ${status.runtimeType}');
|
||||
}
|
||||
KRLogUtil.kr_i('🔄 连接状态变化: $status', tag: 'HomeController');
|
||||
KRLogUtil.kr_i('📊 当前状态类型: ${status.runtimeType}', tag: 'HomeController');
|
||||
|
||||
@@ -526,7 +529,9 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
break;
|
||||
case SingboxStarted():
|
||||
KRLogUtil.kr_i('🟢 状态: 已启动', tag: 'HomeController');
|
||||
print('🔵 状态变为 Started, 当前延迟=${kr_currentNodeLatency.value}');
|
||||
if (kDebugMode) {
|
||||
print('🔵 状态变为 Started, 当前延迟=${kr_currentNodeLatency.value}');
|
||||
}
|
||||
|
||||
// 取消连接超时处理
|
||||
_cancelConnectionTimeout();
|
||||
@@ -538,10 +543,14 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
|
||||
// 🔧 关键修复:如果延迟还是-1(连接中状态),立即设置为0(已连接但延迟未知)
|
||||
if (kr_currentNodeLatency.value == -1) {
|
||||
print('🔵 强制将延迟从 -1 更新为 0');
|
||||
if (kDebugMode) {
|
||||
print('🔵 强制将延迟从 -1 更新为 0');
|
||||
}
|
||||
kr_currentNodeLatency.value = 0;
|
||||
kr_currentNodeLatency.refresh(); // 强制刷新延迟值
|
||||
print('🔵 延迟值已刷新');
|
||||
if (kDebugMode) {
|
||||
print('🔵 延迟值已刷新');
|
||||
}
|
||||
}
|
||||
|
||||
// 🔧 修复:立即尝试更新延迟值
|
||||
@@ -551,7 +560,9 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
kr_isConnected.refresh();
|
||||
// 强制更新UI
|
||||
update();
|
||||
print('🔵 状态更新完成,当前延迟=${kr_currentNodeLatency.value}');
|
||||
if (kDebugMode) {
|
||||
print('🔵 状态更新完成,当前延迟=${kr_currentNodeLatency.value}');
|
||||
}
|
||||
break;
|
||||
case SingboxStopping():
|
||||
KRLogUtil.kr_i('🟠 状态: 正在停止', tag: 'HomeController');
|
||||
@@ -637,12 +648,16 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
final currentStatus = KRSingBoxImp.instance.kr_status.value;
|
||||
|
||||
KRLogUtil.kr_i('🔵 toggleSwitch 被调用: value=$value, currentStatus=$currentStatus', tag: 'HomeController');
|
||||
print('🔵 toggleSwitch: value=$value, currentStatus=$currentStatus');
|
||||
if (kDebugMode) {
|
||||
print('🔵 toggleSwitch: value=$value, currentStatus=$currentStatus');
|
||||
}
|
||||
|
||||
// 🔧 关键: 如果正在切换中,直接忽略(参考 hiddify-app 的 "switching status, debounce")
|
||||
if (currentStatus is SingboxStarting || currentStatus is SingboxStopping) {
|
||||
KRLogUtil.kr_i('🔄 正在切换中,忽略本次操作 (当前状态: $currentStatus)', tag: 'HomeController');
|
||||
print('🔵 忽略操作:正在切换中');
|
||||
if (kDebugMode) {
|
||||
print('🔵 忽略操作:正在切换中');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -650,17 +665,23 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
if (value) {
|
||||
// 开启连接
|
||||
KRLogUtil.kr_i('🔄 开始连接...', tag: 'HomeController');
|
||||
print('🔵 执行 kr_start()');
|
||||
if (kDebugMode) {
|
||||
print('🔵 执行 kr_start()');
|
||||
}
|
||||
await KRSingBoxImp.instance.kr_start();
|
||||
KRLogUtil.kr_i('✅ 连接命令已发送', tag: 'HomeController');
|
||||
print('🔵 kr_start() 完成');
|
||||
if (kDebugMode) {
|
||||
print('🔵 kr_start() 完成');
|
||||
}
|
||||
|
||||
// 🔧 修复: 等待状态更新,最多3秒
|
||||
await _waitForStatus(SingboxStarted, maxSeconds: 3);
|
||||
} else {
|
||||
// 关闭连接
|
||||
KRLogUtil.kr_i('🛑 开始断开连接...', tag: 'HomeController');
|
||||
print('🔵 执行 kr_stop()');
|
||||
if (kDebugMode) {
|
||||
print('🔵 执行 kr_stop()');
|
||||
}
|
||||
await KRSingBoxImp.instance.kr_stop().timeout(
|
||||
const Duration(seconds: 10),
|
||||
onTimeout: () {
|
||||
@@ -669,32 +690,44 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
},
|
||||
);
|
||||
KRLogUtil.kr_i('✅ 断开命令已发送', tag: 'HomeController');
|
||||
print('🔵 kr_stop() 完成');
|
||||
if (kDebugMode) {
|
||||
print('🔵 kr_stop() 完成');
|
||||
}
|
||||
|
||||
// 🔧 修复: 等待状态更新,最多2秒
|
||||
await _waitForStatus(SingboxStopped, maxSeconds: 2);
|
||||
}
|
||||
} catch (e) {
|
||||
KRLogUtil.kr_e('❌ 切换失败: $e', tag: 'HomeController');
|
||||
print('🔵 切换失败: $e');
|
||||
if (kDebugMode) {
|
||||
print('🔵 切换失败: $e');
|
||||
}
|
||||
// 发生错误时强制同步状态
|
||||
kr_forceSyncConnectionStatus();
|
||||
}
|
||||
|
||||
print('🔵 toggleSwitch 完成,当前 kr_isConnected=${kr_isConnected.value}');
|
||||
if (kDebugMode) {
|
||||
print('🔵 toggleSwitch 完成,当前 kr_isConnected=${kr_isConnected.value}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔧 等待状态达到预期值
|
||||
Future<void> _waitForStatus(Type expectedType, {int maxSeconds = 3}) async {
|
||||
print('🔵 等待状态变为: $expectedType');
|
||||
if (kDebugMode) {
|
||||
print('🔵 等待状态变为: $expectedType');
|
||||
}
|
||||
final startTime = DateTime.now();
|
||||
|
||||
while (DateTime.now().difference(startTime).inSeconds < maxSeconds) {
|
||||
final currentStatus = KRSingBoxImp.instance.kr_status.value;
|
||||
print('🔵 当前状态: ${currentStatus.runtimeType}');
|
||||
if (kDebugMode) {
|
||||
print('🔵 当前状态: ${currentStatus.runtimeType}');
|
||||
}
|
||||
|
||||
if (currentStatus.runtimeType == expectedType) {
|
||||
print('🔵 状态已达到: $expectedType');
|
||||
if (kDebugMode) {
|
||||
print('🔵 状态已达到: $expectedType');
|
||||
}
|
||||
// 强制同步确保 kr_isConnected 正确
|
||||
kr_forceSyncConnectionStatus();
|
||||
return;
|
||||
@@ -703,7 +736,9 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
print('🔵 等待超时,强制同步状态');
|
||||
if (kDebugMode) {
|
||||
print('🔵 等待超时,强制同步状态');
|
||||
}
|
||||
kr_forceSyncConnectionStatus();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
|
||||
import 'package:kaer_with_panels/singbox/model/singbox_status.dart';
|
||||
import '../controllers/kr_home_controller.dart';
|
||||
import '../models/kr_home_views_status.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class KRHomeConnectionInfoView extends GetView<KRHomeController> {
|
||||
const KRHomeConnectionInfoView({super.key});
|
||||
@@ -82,7 +83,9 @@ class KRHomeConnectionInfoView extends GetView<KRHomeController> {
|
||||
// 🔧 修复:使用 Obx 包裹确保国旗响应式更新
|
||||
Obx(() {
|
||||
final countryCode = controller.kr_getCurrentNodeCountry();
|
||||
print('🌍 ConnectionInfo 更新,国家代码: $countryCode');
|
||||
if (kDebugMode) {
|
||||
print('🌍 ConnectionInfo 更新,国家代码: $countryCode');
|
||||
}
|
||||
return KRCountryFlag(
|
||||
countryCode: countryCode,
|
||||
);
|
||||
@@ -104,7 +107,9 @@ class KRHomeConnectionInfoView extends GetView<KRHomeController> {
|
||||
children: [
|
||||
Obx(() {
|
||||
final delay = controller.kr_currentNodeLatency.value;
|
||||
print('🔵 UI延迟显示更新: delay=$delay');
|
||||
if (kDebugMode) {
|
||||
print('🔵 UI延迟显示更新: delay=$delay');
|
||||
}
|
||||
|
||||
// 获取延迟颜色
|
||||
Color getLatencyColor(int delay) {
|
||||
@@ -227,7 +232,9 @@ class KRHomeConnectionInfoView extends GetView<KRHomeController> {
|
||||
final isSwitching = status is SingboxStarting || status is SingboxStopping;
|
||||
|
||||
// 🔧 调试日志
|
||||
print('🔵 Switch UI 更新: status=${status.runtimeType}, isConnected=$isConnected, isSwitching=$isSwitching');
|
||||
if (kDebugMode) {
|
||||
print('🔵 Switch UI 更新: status=${status.runtimeType}, isConnected=$isConnected, isSwitching=$isSwitching');
|
||||
}
|
||||
|
||||
return CupertinoSwitch(
|
||||
value: isConnected,
|
||||
@@ -235,7 +242,9 @@ class KRHomeConnectionInfoView extends GetView<KRHomeController> {
|
||||
onChanged: isSwitching
|
||||
? null
|
||||
: (bool value) {
|
||||
print('🔵 Switch onChanged 触发: 请求=$value, 当前状态=$status');
|
||||
if (kDebugMode) {
|
||||
print('🔵 Switch onChanged 触发: 请求=$value, 当前状态=$status');
|
||||
}
|
||||
controller.kr_toggleSwitch(value);
|
||||
},
|
||||
activeColor: Colors.blue,
|
||||
|
||||
@@ -16,6 +16,7 @@ import 'package:kaer_with_panels/app/widgets/kr_network_image.dart';
|
||||
import '../../../widgets/kr_simple_loading.dart';
|
||||
|
||||
import '../../../../singbox/model/singbox_proxy_type.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 节点列表视图组件
|
||||
/// 用于展示所有节点相关的列表视图
|
||||
@@ -281,7 +282,9 @@ class KRHomeNodeListView extends GetView<KRHomeController> {
|
||||
// 🔧 修复:改为 async,等待节点切换完成后再关闭列表
|
||||
onTap: () async {
|
||||
try {
|
||||
print('🔄 用户点击节点: ${server.tag}');
|
||||
if (kDebugMode) {
|
||||
print('🔄 用户点击节点: ${server.tag}');
|
||||
}
|
||||
// 使用统一的节点切换方法,等待完成
|
||||
final success = await controller
|
||||
.kr_performNodeSwitch(server.tag);
|
||||
@@ -290,12 +293,18 @@ class KRHomeNodeListView extends GetView<KRHomeController> {
|
||||
if (success) {
|
||||
controller.kr_currentListStatus.value =
|
||||
KRHomeViewsListStatus.kr_none;
|
||||
print('✅ 节点切换成功,关闭列表');
|
||||
if (kDebugMode) {
|
||||
print('✅ 节点切换成功,关闭列表');
|
||||
}
|
||||
} else {
|
||||
print('❌ 节点切换失败,列表保持打开');
|
||||
if (kDebugMode) {
|
||||
print('❌ 节点切换失败,列表保持打开');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('❌ 节点切换异常: $e');
|
||||
if (kDebugMode) {
|
||||
print('❌ 节点切换异常: $e');
|
||||
}
|
||||
KRLogUtil.kr_e('节点切换异常: $e',
|
||||
tag: 'NodeListView');
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:latlong2/latlong.dart';
|
||||
import '../../../utils/kr_fm_tc.dart';
|
||||
import '../controllers/kr_home_controller.dart';
|
||||
import '../../../utils/kr_log_util.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 首页地图视图组件
|
||||
class KRHomeMapView extends GetView<KRHomeController> {
|
||||
@@ -187,12 +188,16 @@ return GetBuilder<KRHomeController>(
|
||||
|
||||
/// 构建样式化的标记
|
||||
Marker _buildStyledMarker(dynamic node) {
|
||||
print("原始Marker:${node}");
|
||||
if (kDebugMode) {
|
||||
print("原始Marker:${node}");
|
||||
}
|
||||
int type = 0;
|
||||
MaterialColor markerColor = Colors.grey;
|
||||
//如果没订阅过,就是灰色
|
||||
bool status = controller.kr_isConnected.value;
|
||||
print("连接状态:$status");
|
||||
if (kDebugMode) {
|
||||
print("连接状态:$status");
|
||||
}
|
||||
if(status){
|
||||
if (node.urlTestDelay.value == 0) {
|
||||
// 延迟为0时使用默认颜色
|
||||
|
||||
Reference in New Issue
Block a user