Skip to content

Commit

Permalink
feat: error logging for desktop and android (#180)
Browse files Browse the repository at this point in the history
* feat: error loging for desktop and android

* change route from home to setting-bugreport

* delete conflict files

* feat: extension search and separation by types (#192)

* feat: extension search and separation by types

* Refactor extensionCards filtering logic

* perf: extension load optimisation (#193)

* Add logging and share_plus dependencies

---------

Co-authored-by: MiaoMint <44718819+MiaoMint@users.noreply.github.com>
Co-authored-by: MiaoMint <miaomint@0u0.ren>
  • Loading branch information
3 people authored Jan 19, 2024
1 parent d391ae8 commit bc03074
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 15 deletions.
23 changes: 21 additions & 2 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"logout": "Logout",
"login": "Login",
"no-data": "No data",
"clear": "Clear"
"clear": "Clear",
"export": "Export"
},
"home": {
"continue-watching": "Continue",
Expand Down Expand Up @@ -133,7 +134,13 @@
"proxy-type-socks5": "Socks5",
"proxy-type-http": "HTTP",
"proxy": "Proxy",
"proxy-subtitle": "Proxy address (e.g. username:password@host:port)"
"proxy-subtitle": "Proxy address (e.g. username:password@host:port)",
"log": "Log",
"log-subtitle": "Save log, export log...",
"save-log": "Save Log",
"save-log-subtitle": "Auto save log file",
"export-log": "Export Log",
"export-log-subtitle": "Export log file"
},
"external-player-launching": "Launching {player}",
"detail": {
Expand Down Expand Up @@ -186,6 +193,11 @@
"novel-settings": {
"font-size": "Font size"
},
"bugreport": {
"auto-remove-subtitle": "delete in ~ days",
"show-report-dialog": "Show Report Dialog",
"show-report-dialog-subtitle": "Show error report dialog when app starts"
},
"reader": {
"chapters": "Chapters",
"read-now": "Read Now",
Expand Down Expand Up @@ -240,6 +252,13 @@
"upgrade": "Upgrade",
"start": "Start"
},
"report": {
"copied": "Copied to clipboard",
"github-bug-report": "Report to Github issue",
"title": "Bug Report",
"copy-message": "Copy error message",
"show-report-checkbox": "Show report dialog when app starts"
},
"anilist": {
"title": "Anilist Tracking",
"login-hint-1": "Seems you haven't login into AniList yet",
Expand Down
25 changes: 21 additions & 4 deletions assets/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"save": "保存",
"save-success": "保存成功",
"logout": "登出",
"clear": "清除"
"clear": "清除",
"export": "导出"
},
"home": {
"continue-watching": "继续观看",
Expand Down Expand Up @@ -98,15 +99,31 @@
"theme-black": "黑色",
"nsfw": "NSFW",
"nsfw-subtitle": "显示 NSFW 内容",
"network-ua": "Webview User-Agent",
"network-ua-subtitle": "修改 Webview 和扩展的请求头的 User-Agent",
"external-player": "优先使用的视频播放器",
"external-player-subtitle": "当前优先使用的是 {player}",
"external-player-builtin": "内置播放器",
"language-subtitle": "选择软件的语言",
"extension-log": "扩展日志窗口",
"extension-log-subtitle": "用于调试扩展",
"default-reader-mode": "默认漫画阅读模式"
"default-reader-mode": "默认漫画阅读模式",
"network": "网络",
"network-subtitle": "代理, User-Agent...",
"network-ua": "Webview User-Agent",
"network-ua-subtitle": "修改 Webview 和扩展的请求头的 User-Agent",
"proxy-type": "代理类型",
"proxy-type-subtitle": "设置请求时的代理类型",
"proxy-type-direct": "不代理",
"proxy-type-socks4": "Socks4",
"proxy-type-socks5": "Socks5",
"proxy-type-http": "HTTP",
"proxy": "代理",
"proxy-subtitle": "代理地址 (例如 username:password@host:port)",
"log": "日志",
"log-subtitle": "保存日志, 导出日志...",
"save-log": "保存日志",
"save-log-subtitle": "自动保存日志文件",
"export-log": "导出日志",
"export-log-subtitle": "导出日志文件到本地"
},
"external-player-launching": "正在启动 {player}",
"detail": {
Expand Down
17 changes: 13 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:desktop_multi_window/desktop_multi_window.dart';
Expand All @@ -7,6 +8,7 @@ import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:media_kit/media_kit.dart';
import 'package:miru_app/controllers/application_controller.dart';
import 'package:miru_app/utils/log.dart';
import 'package:miru_app/utils/miru_directory.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/pages/debug_page.dart';
Expand All @@ -20,6 +22,10 @@ import 'package:miru_app/views/widgets/platform_widget.dart';
import 'package:window_manager/window_manager.dart';

void main(List<String> args) async {
FlutterError.onError = (FlutterErrorDetails details) {
logger.severe("", details.exception, details.stack);
};

WidgetsFlutterBinding.ensureInitialized();

// 多窗口
Expand All @@ -41,6 +47,7 @@ void main(List<String> args) async {
// 主窗口
await MiruDirectory.ensureInitialized();
await MiruStorage.ensureInitialized();
MiruLog.ensureInitialized();
await ApplicationUtils.ensureInitialized();
await MiruRequest.ensureInitialized();
ExtensionUtils.ensureInitialized();
Expand Down Expand Up @@ -81,17 +88,19 @@ void main(List<String> args) async {
);
SystemChrome.setSystemUIOverlayStyle(style);
}
runApp(const MainApp());
runZonedGuarded(() => runApp(const MainApp()), (error, stack) {
logger.severe("", error, stack);
});
}

class MainApp extends fluent.StatefulWidget {
class MainApp extends StatefulWidget {
const MainApp({super.key});

@override
fluent.State<MainApp> createState() => _MainAppState();
State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends fluent.State<MainApp> {
class _MainAppState extends State<MainApp> {
late ApplicationController c;

@override
Expand Down
40 changes: 40 additions & 0 deletions lib/utils/log.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:miru_app/utils/miru_directory.dart';
import 'package:miru_app/utils/miru_storage.dart';
import 'package:path/path.dart' as path;

final logger = Logger('Miru');

class MiruLog {
static final logFilePath = path.join(MiruDirectory.getDirectory, 'miru.log');

static void ensureInitialized() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
final log =
'${record.loggerName} ${record.level.name} ${record.time}: ${record.message} ${record.error ?? ''} ${record.stackTrace ?? ''}';
// 如果是开发环境则打印到控制台
if (kReleaseMode) {
writeLogToFile(log);
return;
}

debugPrint(log);
});
}

// 写入日志到文件
static void writeLogToFile(String log) {
if (!MiruStorage.getSetting(SettingKey.saveLog)) {
return;
}
final file = File(logFilePath);
file.writeAsStringSync('$log\n', mode: FileMode.append);
if (file.lengthSync() > 1024 * 1024 * 10) {
file.deleteSync();
}
}
}
2 changes: 2 additions & 0 deletions lib/utils/miru_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class MiruStorage {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0");
await _initSetting(SettingKey.proxy, '');
await _initSetting(SettingKey.proxyType, 'DIRECT');
await _initSetting(SettingKey.saveLog, true);
}

static _initSetting(String key, dynamic value) async {
Expand Down Expand Up @@ -185,4 +186,5 @@ class SettingKey {
static String windowsWebviewUA = "WindowsWebviewUA";
static String proxy = "Proxy";
static String proxyType = "ProxyType";
static String saveLog = "SaveLog";
}
1 change: 1 addition & 0 deletions lib/views/pages/detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ class _DetailPageState extends State<DetailPage> {
tag: widget.tag,
),
const SizedBox(width: 8),

if (c.tmdbDetail != null)
fluent.Button(
child: const Padding(
Expand Down
61 changes: 58 additions & 3 deletions lib/views/pages/settings/settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:miru_app/data/providers/tmdb_provider.dart';
import 'package:miru_app/controllers/application_controller.dart';
import 'package:miru_app/router/router.dart';
import 'package:miru_app/utils/log.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/dialogs/bt_dialog.dart';
import 'package:miru_app/controllers/extension/extension_repo_controller.dart';
Expand All @@ -23,6 +25,7 @@ import 'package:miru_app/utils/miru_storage.dart';
import 'package:miru_app/utils/application.dart';
import 'package:miru_app/views/widgets/list_title.dart';
import 'package:miru_app/views/widgets/platform_widget.dart';
import 'package:share_plus/share_plus.dart';
import 'package:tmdb_api/tmdb_api.dart';
import 'package:url_launcher/url_launcher.dart';

Expand Down Expand Up @@ -398,7 +401,10 @@ class _SettingsPageState extends State<SettingsPage> {
title: 'settings.tracking'.i18n,
subTitle: 'settings.tracking-subtitle'.i18n,
),
const SizedBox(height: 10),
const SizedBox(height: 20),
// 高级
ListTitle(title: '高级'.i18n),
const SizedBox(height: 20),
// 网络设置
SettingsExpanderTile(
content: Column(
Expand Down Expand Up @@ -453,7 +459,55 @@ class _SettingsPageState extends State<SettingsPage> {
),
const SizedBox(height: 10),
// Debug
if (!Platform.isAndroid)
SettingsExpanderTile(
title: "settings.log".i18n,
subTitle: 'settings.log-subtitle'.i18n,
androidIcon: Icons.report,
icon: fluent.FluentIcons.report_alert,
content: Column(
children: [
SettingsSwitchTile(
title: 'settings.save-log'.i18n,
buildSubtitle: () => 'settings.save-log-subtitle'.i18n,
buildValue: () {
return MiruStorage.getSetting(SettingKey.saveLog);
},
onChanged: (value) {
MiruStorage.setSetting(SettingKey.saveLog, value);
},
),
const SizedBox(height: 10),
// 导出日志
SettingsTile(
title: 'settings.export-log'.i18n,
buildSubtitle: () => 'settings.export-log-subtitle'.i18n,
trailing: PlatformWidget(
androidWidget: TextButton(
onPressed: () {
Share.shareXFiles([XFile(MiruLog.logFilePath)]);
},
child: Text('common.export'.i18n),
),
desktopWidget: fluent.FilledButton(
onPressed: () async {
final path = await FilePicker.platform.saveFile(
type: FileType.custom,
allowedExtensions: ['log'],
fileName: 'miru.log',
);
if (path != null) {
File(MiruLog.logFilePath).copy(path);
}
},
child: Text('common.export'.i18n),
),
),
),
],
),
),
if (!Platform.isAndroid) ...[
const SizedBox(height: 10),
Obx(
() {
final value = c.extensionLogWindowId.value != -1;
Expand All @@ -471,7 +525,8 @@ class _SettingsPageState extends State<SettingsPage> {
isCard: true,
);
},
),
)
],
// 关于
const SizedBox(height: 20),
ListTitle(title: 'settings.about'.i18n),
Expand Down
2 changes: 1 addition & 1 deletion lib/views/widgets/settings/settings_expander_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SettingsExpanderTile extends StatelessWidget {
buildSubtitle: () => subTitle,
onTap: () {
Get.to(
Scaffold(
() => Scaffold(
appBar: AppBar(
title: Text(title),
),
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import package_info_plus
import path_provider_foundation
import screen_brightness_macos
import screen_retriever
import share_plus
import url_launcher_macos
import wakelock_plus
import window_manager
Expand All @@ -30,6 +31,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
ScreenBrightnessMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenBrightnessMacosPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin"))
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
Expand Down
26 changes: 25 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.8"
cross_file:
dependency: transitive
description:
name: cross_file
sha256: fedaadfa3a6996f75211d835aaeb8fede285dae94262485698afd832371b9a5e
url: "https://pub.dev"
source: hosted
version: "0.3.3+8"
crypto:
dependency: "direct main"
description:
Expand Down Expand Up @@ -687,7 +695,7 @@ packages:
source: hosted
version: "3.0.0"
logging:
dependency: transitive
dependency: "direct main"
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
Expand Down Expand Up @@ -1078,6 +1086,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.8"
share_plus:
dependency: "direct main"
description:
name: share_plus
sha256: f74fc3f1cbd99f39760182e176802f693fa0ec9625c045561cfad54681ea93dd
url: "https://pub.dev"
source: hosted
version: "7.2.1"
share_plus_platform_interface:
dependency: transitive
description:
name: share_plus_platform_interface
sha256: df08bc3a07d01f5ea47b45d03ffcba1fa9cd5370fb44b3f38c70e42cced0f956
url: "https://pub.dev"
source: hosted
version: "3.3.1"
shelf:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ dependencies:
flutter_hls_parser: ^2.0.1
image_gallery_saver: ^2.0.3
flutter_socks_proxy: ^0.0.3
logging: ^1.2.0
share_plus: ^7.2.1

dev_dependencies:
flutter_test:
Expand Down
Loading

0 comments on commit bc03074

Please sign in to comment.