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
@@ -1,4 +1,4 @@
import 'dart:io';
import 'dart:io' as io;
import 'dart:math';
import 'dart:convert';
@@ -21,6 +21,8 @@ import '../kr_device_info_service.dart';
import '../kr_site_config_service.dart';
import '../../common/app_config.dart';
import 'package:dio/dio.dart' as dio;
import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
import 'package:flutter/foundation.dart';
class KRAuthApi {
/// 检查账号是否已注册(仅支持邮箱)
@@ -465,17 +467,17 @@ class KRAuthApi {
}
String _kr_getUserAgent() {
if (Platform.isAndroid) {
if (io.Platform.isAndroid) {
return 'android';
} else if (Platform.isIOS) {
} else if (io.Platform.isIOS) {
return 'ios';
} else if (Platform.isMacOS) {
} else if (io.Platform.isMacOS) {
return 'mac';
} else if (Platform.isWindows) {
} else if (io.Platform.isWindows) {
return 'windows';
} else if (Platform.isLinux) {
} else if (io.Platform.isLinux) {
return 'linux';
} else if (Platform.isFuchsia) {
} else if (io.Platform.isFuchsia) {
return 'harmony';
} else {
return 'unknown';
+61 -25
View File
@@ -27,26 +27,32 @@ class KRDeviceInfoService {
/// 初始化设备信息
Future<void> initialize() async {
try {
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
print('📱 开始初始化设备信息服务');
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
if (kDebugMode) {
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
print('📱 开始初始化设备信息服务');
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
KRLogUtil.kr_i('📱 开始初始化设备信息', tag: 'KRDeviceInfoService');
_deviceId = await _getDeviceId();
_deviceDetails = await _getDeviceDetails();
print('✅ 设备信息初始化成功');
print('📱 设备ID: $_deviceId');
print('📱 设备平台: ${getPlatformName()}');
print('📱 设备型号: ${getDeviceModel()}');
print('📱 系统版本: ${getOSVersion()}');
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
if (kDebugMode) {
print(' 设备信息初始化成功');
print('📱 设备ID: $_deviceId');
print('📱 设备平台: ${getPlatformName()}');
print('📱 设备型号: ${getDeviceModel()}');
print('📱 系统版本: ${getOSVersion()}');
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
KRLogUtil.kr_i('✅ 设备信息初始化成功', tag: 'KRDeviceInfoService');
KRLogUtil.kr_i('📱 设备ID - $_deviceId', tag: 'KRDeviceInfoService');
KRLogUtil.kr_i('📱 设备详情 - $_deviceDetails', tag: 'KRDeviceInfoService');
} catch (e) {
print('❌ 设备信息初始化失败: $e');
if (kDebugMode) {
print('❌ 设备信息初始化失败: $e');
}
KRLogUtil.kr_e('❌ 设备信息初始化失败 - $e', tag: 'KRDeviceInfoService');
}
}
@@ -79,7 +85,9 @@ class KRDeviceInfoService {
return identifier;
} catch (e) {
print('❌ 获取设备ID失败: $e');
if (kDebugMode) {
print('❌ 获取设备ID失败: $e');
}
KRLogUtil.kr_e('❌ 获取设备ID失败 - $e', tag: 'KRDeviceInfoService');
// 如果获取失败,返回存储的或生成新的ID
return await _getOrCreateStoredDeviceId();
@@ -119,12 +127,16 @@ class KRDeviceInfoService {
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Android多因子ID生成 - 因子数: ${factors.where((f) => f != null && f.isNotEmpty).length}');
if (kDebugMode) {
print('📱 Android多因子ID生成 - 因子数: ${factors.where((f) => f != null && f.isNotEmpty).length}');
}
KRLogUtil.kr_i('📱 Android多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Android设备ID获取失败: $e');
if (kDebugMode) {
print('❌ Android设备ID获取失败: $e');
}
KRLogUtil.kr_e('❌ Android设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
@@ -157,12 +169,16 @@ class KRDeviceInfoService {
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 iOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
if (kDebugMode) {
print('📱 iOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
}
KRLogUtil.kr_i('📱 iOS多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ iOS设备ID获取失败: $e');
if (kDebugMode) {
print('❌ iOS设备ID获取失败: $e');
}
KRLogUtil.kr_e('❌ iOS设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
@@ -193,12 +209,16 @@ class KRDeviceInfoService {
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 macOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
if (kDebugMode) {
print('📱 macOS多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
}
KRLogUtil.kr_i('📱 macOS多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ macOS设备ID获取失败: $e');
if (kDebugMode) {
print('❌ macOS设备ID获取失败: $e');
}
KRLogUtil.kr_e('❌ macOS设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
@@ -229,12 +249,16 @@ class KRDeviceInfoService {
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Windows多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
if (kDebugMode) {
print('📱 Windows多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
}
KRLogUtil.kr_i('📱 Windows多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Windows设备ID获取失败: $e');
if (kDebugMode) {
print('❌ Windows设备ID获取失败: $e');
}
KRLogUtil.kr_e('❌ Windows设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
@@ -265,12 +289,16 @@ class KRDeviceInfoService {
final bytes = utf8.encode(combined);
final hash = sha256.convert(bytes);
print('📱 Linux多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
if (kDebugMode) {
print('📱 Linux多因子ID生成 - 因子数: ${factors.where((f) => f.isNotEmpty).length}');
}
KRLogUtil.kr_i('📱 Linux多因子ID - $hash', tag: 'KRDeviceInfoService');
return hash.toString();
} catch (e) {
print('❌ Linux设备ID获取失败: $e');
if (kDebugMode) {
print('❌ Linux设备ID获取失败: $e');
}
KRLogUtil.kr_e('❌ Linux设备ID获取失败 - $e', tag: 'KRDeviceInfoService');
return '';
}
@@ -288,16 +316,22 @@ class KRDeviceInfoService {
// 生成新的UUID
storedId = _generateUniqueId();
await storage.kr_saveData(key: key, value: storedId);
print('📱 生成新的设备ID: $storedId');
if (kDebugMode) {
print('📱 生成新的设备ID: $storedId');
}
KRLogUtil.kr_i('📱 生成新的设备ID - $storedId', tag: 'KRDeviceInfoService');
} else {
print('📱 使用存储的设备ID: $storedId');
if (kDebugMode) {
print('📱 使用存储的设备ID: $storedId');
}
KRLogUtil.kr_i('📱 使用存储的设备ID - $storedId', tag: 'KRDeviceInfoService');
}
return storedId;
} catch (e) {
print('❌ 获取存储的设备ID失败: $e');
if (kDebugMode) {
print('❌ 获取存储的设备ID失败: $e');
}
KRLogUtil.kr_e('❌ 获取存储的设备ID失败 - $e', tag: 'KRDeviceInfoService');
return _generateUniqueId();
}
@@ -383,7 +417,9 @@ class KRDeviceInfoService {
'platform': 'unknown',
};
} catch (e) {
print('❌ 获取设备详情失败: $e');
if (kDebugMode) {
print('❌ 获取设备详情失败: $e');
}
KRLogUtil.kr_e('❌ 获取设备详情失败 - $e', tag: 'KRDeviceInfoService');
return {
'platform': 'unknown',
+64 -22
View File
@@ -47,33 +47,53 @@ class KRSiteConfigService extends ChangeNotifier {
/// 初始化站点配置
Future<bool> initialize() async {
try {
print('🔧 KRSiteConfigService.initialize() 开始执行');
if (kDebugMode) {
print('🔧 KRSiteConfigService.initialize() 开始执行');
}
KRLogUtil.kr_i('🔧 开始初始化网站配置', tag: 'KRSiteConfigService');
// Debug 模式下使用固定地址
final baseUrl = AppConfig().baseUrl;
print('📍 baseUrl = $baseUrl');
if (kDebugMode) {
print('📍 baseUrl = $baseUrl');
}
final url = '$baseUrl/v1/common/site/config';
print('📍 完整URL = $url');
if (kDebugMode) {
print('📍 完整URL = $url');
}
KRLogUtil.kr_i('📤 请求网站配置 - $url', tag: 'KRSiteConfigService');
print('📤 准备发送 GET 请求到: $url');
print('⏱️ 超时配置: connectTimeout=10s, sendTimeout=10s, receiveTimeout=10s');
if (kDebugMode) {
print('📤 准备发送 GET 请求到: $url');
}
if (kDebugMode) {
print('⏱️ 超时配置: connectTimeout=10s, sendTimeout=10s, receiveTimeout=10s');
}
print('⏳ 开始发送请求...');
if (kDebugMode) {
print('⏳ 开始发送请求...');
}
final startTime = DateTime.now();
final response = await _dio.get(url);
final endTime = DateTime.now();
final duration = endTime.difference(startTime).inMilliseconds;
print('⏱️ 请求耗时: ${duration}ms');
if (kDebugMode) {
print('⏱️ 请求耗时: ${duration}ms');
}
print('✅ 请求完成,状态码: ${response.statusCode}');
if (kDebugMode) {
print('✅ 请求完成,状态码: ${response.statusCode}');
}
KRLogUtil.kr_i('📥 响应状态码 - ${response.statusCode}', tag: 'KRSiteConfigService');
if (response.statusCode == 200) {
final responseData = response.data;
print('📥 响应数据类型: ${responseData.runtimeType}');
print('📥 响应数据: $responseData');
if (kDebugMode) {
print('📥 响应数据类型: ${responseData.runtimeType}');
}
if (kDebugMode) {
print('📥 响应数据: $responseData');
}
KRLogUtil.kr_i('📥 响应数据 - $responseData', tag: 'KRSiteConfigService');
if (responseData['code'] == 200) {
@@ -96,24 +116,46 @@ class KRSiteConfigService extends ChangeNotifier {
return false;
}
} on DioException catch (e, stackTrace) {
print('❌ Dio请求异常: ${e.type}');
print('错误信息: ${e.message}');
print('❌ 请求URL: ${e.requestOptions.uri}');
print('❌ 连接超时: ${e.requestOptions.connectTimeout}');
print('发送超时: ${e.requestOptions.sendTimeout}');
print('❌ 接收超时: ${e.requestOptions.receiveTimeout}');
if (e.response != null) {
print('响应状态码: ${e.response?.statusCode}');
print('❌ 响应数据: ${e.response?.data}');
if (kDebugMode) {
print('Dio请求异常: ${e.type}');
}
if (kDebugMode) {
print('错误信息: ${e.message}');
}
if (kDebugMode) {
print('请求URL: ${e.requestOptions.uri}');
}
if (kDebugMode) {
print('❌ 连接超时: ${e.requestOptions.connectTimeout}');
}
if (kDebugMode) {
print('❌ 发送超时: ${e.requestOptions.sendTimeout}');
}
if (kDebugMode) {
print('❌ 接收超时: ${e.requestOptions.receiveTimeout}');
}
if (e.response != null) {
if (kDebugMode) {
print('❌ 响应状态码: ${e.response?.statusCode}');
}
if (kDebugMode) {
print('❌ 响应数据: ${e.response?.data}');
}
}
if (kDebugMode) {
print('📚 堆栈跟踪: $stackTrace');
}
print('📚 堆栈跟踪: $stackTrace');
KRLogUtil.kr_e('❌ Dio异常 - ${e.type}: ${e.message}', tag: 'KRSiteConfigService');
KRLogUtil.kr_e('📚 堆栈: $stackTrace', tag: 'KRSiteConfigService');
return false;
} catch (e, stackTrace) {
print('❌ 未知异常: $e');
print('📚 堆栈跟踪: $stackTrace');
if (kDebugMode) {
print('❌ 未知异常: $e');
}
if (kDebugMode) {
print('📚 堆栈跟踪: $stackTrace');
}
KRLogUtil.kr_e('❌ 初始化失败 - $e', tag: 'KRSiteConfigService');
KRLogUtil.kr_e('📚 堆栈: $stackTrace', tag: 'KRSiteConfigService');
return false;
@@ -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) {