Commit 72d5402754132eddf0e8718e86f1b718efab0c16

Authored by 权海
1 parent 0af28457

feat(ui):增加日志系统

... ... @@ -400,6 +400,8 @@ protocol HealthKitRawDataHostApi {
func shareNativeAppleHealthObserverRecord(completion: @escaping (Result<Bool, Error>) -> Void)
/// 让原生分享出来flutter的记录文件
func shareFlutterObserverRecord(completion: @escaping (Result<Bool, Error>) -> Void)
/// 让原生分享出来上传记录
func shareUploadTaskRecord(completion: @escaping (Result<Bool, Error>) -> Void)
}
/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
... ... @@ -680,5 +682,21 @@ class HealthKitRawDataHostApiSetup {
} else {
shareFlutterObserverRecordChannel.setMessageHandler(nil)
}
/// 让原生分享出来上传记录
let shareUploadTaskRecordChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitRawDataHostApi.shareUploadTaskRecord\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
shareUploadTaskRecordChannel.setMessageHandler { _, reply in
api.shareUploadTaskRecord { result in
switch result {
case .success(let res):
reply(wrapResult(res))
case .failure(let error):
reply(wrapError(error))
}
}
}
} else {
shareUploadTaskRecordChannel.setMessageHandler(nil)
}
}
}
... ...
... ... @@ -24,6 +24,7 @@ enum DeveloperOptionsAction {
shareHealthRawUploadLog,
shareNativeAppleHealthObserverRecord,
shareFlutterObserverRecord,
shareUploadTaskRecord,
clearHealthRawDataDatabaseAndUploadLog,
}
... ... @@ -68,6 +69,10 @@ class DeveloperOptionsController extends GetxController {
action: DeveloperOptionsAction.shareFlutterObserverRecord,
),
DeveloperOptionsItem(
title: '分享上传Task日志',
action: DeveloperOptionsAction.shareUploadTaskRecord,
),
DeveloperOptionsItem(
title: '清除本地数据库、上传日志',
action: DeveloperOptionsAction.clearHealthRawDataDatabaseAndUploadLog,
),
... ... @@ -99,6 +104,9 @@ class DeveloperOptionsController extends GetxController {
case DeveloperOptionsAction.shareFlutterObserverRecord:
await shareFlutterObserverRecord();
break;
case DeveloperOptionsAction.shareUploadTaskRecord:
await shareUploadTaskRecord();
break;
case DeveloperOptionsAction.clearHealthRawDataDatabaseAndUploadLog:
await clearHealthRawDataDatabaseAndUploadLog();
break;
... ... @@ -153,6 +161,19 @@ class DeveloperOptionsController extends GetxController {
}
}
Future<void> shareUploadTaskRecord() async {
try {
await _healthRawDataCoreService.shareUploadTaskRecord();
} catch (error, stackTrace) {
AppLogger.e(
'Share upload task record failed',
error,
stackTrace,
);
Get.snackbar('Share failed', error.toString());
}
}
Future<void> clearHealthRawDataDatabaseAndUploadLog() async {
try {
await _healthRawDataCoreService.clearLocalDatabaseAndUploadLog();
... ...
... ... @@ -172,6 +172,11 @@ class HealthRawDataCoreService {
return _rawDataApi.shareFlutterObserverRecord();
}
Future<bool> shareUploadTaskRecord() async {
if (!_isDebug) return false;
return _rawDataApi.shareUploadTaskRecord();
}
Future<HealthRawStressCalculationResult> startCoreCaculate({
int? endTime,
int readChunkDays = defaultReadChunkDays,
... ...
... ... @@ -38,7 +38,18 @@ Future<void> healthBackgroundMain() async {
WidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('doublefeel_flutter_health_background_channel');
await AppBootstrap.init();
await _healthBackgroundLog(channel, 'entry.start');
try {
await _healthBackgroundLog(channel, 'bootstrap.start');
await AppBootstrap.init();
await _healthBackgroundLog(channel, 'bootstrap.success');
} catch (error, stackTrace) {
await _healthBackgroundLog(
channel,
'bootstrap.failed error=$error stack=$stackTrace',
);
rethrow;
}
channel.setMethodCallHandler((call) async {
switch (call.method) {
... ... @@ -53,6 +64,7 @@ Future<void> healthBackgroundMain() async {
await channel.invokeMethod('healthBackgroundTaskReady', {
'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000,
});
await _healthBackgroundLog(channel, 'entry.readySent');
}
Future<void> _runPendingHealthTasks(
... ... @@ -60,6 +72,10 @@ Future<void> _runPendingHealthTasks(
Object? arguments,
) async {
final tasks = _backgroundHealthTasksFromArguments(arguments);
await _healthBackgroundLog(
channel,
'tasks.received count=${tasks.length} ids=${tasks.map((e) => e.taskId).join(',')}',
);
if (tasks.isEmpty) return;
for (final task in tasks) {
... ... @@ -71,6 +87,10 @@ Future<void> _runPendingHealthTasks(
final userPrefs = Get.find<UserPreferencesStorage>();
final userId = userPrefs.preferences.value.meUserInfo?.id ?? 0;
final token = userPrefs.accessToken;
await _healthBackgroundLog(
channel,
'login.check userId=$userId hasToken=${token.isNotEmpty}',
);
if (userId <= 0 || token.isEmpty) {
for (final task in tasks) {
await channel.invokeMethod('healthBackgroundTaskFinished', {
... ... @@ -84,7 +104,9 @@ Future<void> _runPendingHealthTasks(
}
try {
await _healthBackgroundLog(channel, 'calculate.start');
await Get.find<HealthRawDataCoreService>().startCoreCaculate();
await _healthBackgroundLog(channel, 'calculate.success');
for (final task in tasks) {
await channel.invokeMethod('healthBackgroundTaskFinished', {
'taskId': task.taskId,
... ... @@ -92,6 +114,7 @@ Future<void> _runPendingHealthTasks(
});
}
} catch (error) {
await _healthBackgroundLog(channel, 'calculate.failed error=$error');
for (final task in tasks) {
await channel.invokeMethod('healthBackgroundTaskFinished', {
'taskId': task.taskId,
... ... @@ -103,6 +126,15 @@ Future<void> _runPendingHealthTasks(
}
}
Future<void> _healthBackgroundLog(MethodChannel channel, String message) async {
try {
await channel.invokeMethod('healthBackgroundLog', {
'message': message,
'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000,
});
} catch (_) {}
}
List<_BackgroundHealthTask> _backgroundHealthTasksFromArguments(
Object? arguments,
) {
... ...
... ... @@ -821,4 +821,33 @@ class HealthKitRawDataHostApi {
return (pigeonVar_replyList[0] as bool?)!;
}
}
/// 让原生分享出来上传记录
Future<bool> shareUploadTaskRecord() async {
final String pigeonVar_channelName = 'dev.flutter.pigeon.doublefeel_flutter.HealthKitRawDataHostApi.shareUploadTaskRecord$pigeonVar_messageChannelSuffix';
final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
pigeonVar_channelName,
pigeonChannelCodec,
binaryMessenger: pigeonVar_binaryMessenger,
);
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(null);
final List<Object?>? pigeonVar_replyList =
await pigeonVar_sendFuture as List<Object?>?;
if (pigeonVar_replyList == null) {
throw _createConnectionError(pigeonVar_channelName);
} else if (pigeonVar_replyList.length > 1) {
throw PlatformException(
code: pigeonVar_replyList[0]! as String,
message: pigeonVar_replyList[1] as String?,
details: pigeonVar_replyList[2],
);
} else if (pigeonVar_replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (pigeonVar_replyList[0] as bool?)!;
}
}
}
... ...
... ... @@ -162,4 +162,8 @@ abstract class HealthKitRawDataHostApi {
/// 让原生分享出来flutter的记录文件
@async
bool shareFlutterObserverRecord();
/// 让原生分享出来上传记录
@async
bool shareUploadTaskRecord();
}
... ...