app-starlock/lib/tools/bugly/bugly_tool.dart

94 lines
3.1 KiB
Dart
Raw Normal View History

2024-05-18 18:25:39 +08:00
import 'dart:async';
import 'package:flutter/foundation.dart';
2024-09-29 13:47:50 +08:00
import 'package:flutter_bugly/flutter_bugly.dart';
2024-05-18 18:25:39 +08:00
import 'package:star_lock/app_settings/app_settings.dart';
import 'package:star_lock/flavors.dart';
import 'package:star_lock/login/login/entity/LoginData.dart';
import 'package:star_lock/tools/storage.dart';
2024-05-18 18:25:39 +08:00
///
/// 错误日志监控
///
///
class BuglyTool {
2024-09-30 10:35:44 +08:00
static int time = 0;
static Future<void> init() async {
2024-05-18 18:25:39 +08:00
if (F.isProductionEnv) {
//生产
2024-09-29 13:47:50 +08:00
await FlutterBugly.init(
androidAppId: '5729fb97dc',
iOSAppId: '33c4430cce',
2024-05-18 18:25:39 +08:00
);
} else {
//测试
2024-09-29 13:47:50 +08:00
await FlutterBugly.init(
androidAppId: '5729fb97dc',
iOSAppId: '33c4430cce',
2024-05-18 18:25:39 +08:00
);
}
//关联用户 id
final LoginData? loginData = await Storage.getLoginData();
setUserId(loginData?.userid);
2024-05-18 18:25:39 +08:00
//错误日志监控
2024-09-29 13:47:50 +08:00
// FlutterError.onError = (FlutterErrorDetails details) async {
// AppLog.log('error:${details.exception.toString()}',
// stackTrace: details.stack, error: true);
// FlutterBugly.uploadException(
// message: details.exception.toString(),
// detail: details.stack.toString());
// Zone.current.handleUncaughtError(
// details.exception, details.stack ?? StackTrace.empty);
// };
2024-05-18 18:25:39 +08:00
//错误日志监控
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
AppLog.log('error:$error', stackTrace: stack, error: true);
2024-09-29 13:47:50 +08:00
FlutterBugly.uploadException(
message: error.toString(), detail: stack.toString());
2024-05-18 18:25:39 +08:00
return true;
};
}
2024-05-18 18:25:39 +08:00
//关联 userid
static void setUserId(int? userId) {
2024-09-29 13:47:50 +08:00
FlutterBugly.setUserId(userId?.toString() ?? '');
}
static String uploadStr = '';
2024-09-30 10:35:44 +08:00
static Future<void> uploadException({
2024-09-29 13:47:50 +08:00
required String message,
required String detail,
required bool upload,
String eventStr = '开门事件',
bool begin = false,
2024-09-29 13:47:50 +08:00
Map? data,
2024-09-30 10:35:44 +08:00
}) async {
2024-12-04 15:21:27 +08:00
String getMessage =
'${(await Storage.getMobile())!.isNotEmpty ? (await Storage.getMobile()) : (await Storage.getEmail())}+$time --- message:$message detail:$detail';
if (begin == true) {
// 开始
uploadStr = '';
2024-12-04 15:21:27 +08:00
} else {
uploadStr = '$uploadStr \n<----------->\n $getMessage';
}
2024-12-04 15:21:27 +08:00
if (upload) {
// AppLog.log('message:${(await Storage.getMobile())!.isNotEmpty ? (await Storage.getMobile()) : (await Storage.getEmail())}+$time --- 开门事件 detail:$uploadStr');
// FlutterBugly.uploadException(message: '${(await Storage.getMobile())!.isNotEmpty ? (await Storage.getMobile()) : (await Storage.getEmail())}+$time --- 开门事件', detail: uploadStr, data: data);
2024-12-04 15:21:27 +08:00
BuglyTool.uploadExceptionWithEvent(eventStr: eventStr, detail: uploadStr);
}
}
static Future<void> uploadExceptionWithEvent({
required String eventStr,
required String detail,
}) async {
2024-12-04 15:21:27 +08:00
// FlutterBugly.uploadException(message: '${(await Storage.getMobile())!.isNotEmpty ? (await Storage.getMobile()) : (await Storage.getEmail())}+${time == 0 ? DateTime.now().millisecondsSinceEpoch : 0}--- $eventStr', detail: detail);
2024-05-18 18:25:39 +08:00
}
}