From 9f3ce454b599dce7671de8cee5e499f0b5435f82 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 13 Feb 2023 15:08:38 -0600 Subject: [PATCH] only delete logs older than 30 days automatically --- lib/main.dart | 2 +- .../advanced_views/debug_view.dart | 20 +++---- .../global_settings_view/hidden_settings.dart | 2 +- .../advanced_settings/debug_info_dialog.dart | 8 +-- lib/services/debug_service.dart | 59 ++++++++++--------- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 6105ec8f2..8612d2f7e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -112,7 +112,7 @@ void main() async { await DebugService.instance.init(isar); // clear out all info logs on startup. No need to await and block - unawaited(DebugService.instance.purgeInfoLogs()); + unawaited(DebugService.instance.deleteLogsOlderThan()); } // Registering Transaction Model Adapters diff --git a/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart b/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart index d86dbe272..4642ff0d9 100644 --- a/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart +++ b/lib/pages/settings_views/global_settings_view/advanced_views/debug_view.dart @@ -85,7 +85,6 @@ class _DebugViewState extends ConsumerState { @override void initState() { - ref.read(debugServiceProvider).updateRecentLogs(); super.initState(); } @@ -181,10 +180,7 @@ class _DebugViewState extends ConsumerState { await ref .read(debugServiceProvider) - .deleteAllMessages(); - await ref - .read(debugServiceProvider) - .updateRecentLogs(); + .deleteAllLogs(); shouldPop = true; @@ -194,6 +190,8 @@ class _DebugViewState extends ConsumerState { type: FlushBarType.info, context: context, message: 'Logs cleared!')); + + setState(() {}); } }, ), @@ -313,7 +311,7 @@ class _DebugViewState extends ConsumerState { _searchTerm) .reversed .toList(growable: false); - List errorLogs = []; + List errorLogs = []; for (var log in logs) { if (log.logLevel == LogLevel.Error || log.logLevel == LogLevel.Fatal) { @@ -406,14 +404,14 @@ class _DebugViewState extends ConsumerState { ), )); - bool logssaved = true; - var filename; + bool logsSaved = true; + String? filename; try { filename = await ref .read(debugServiceProvider) .exportToFile(path, eventBus); } catch (e, s) { - logssaved = false; + logsSaved = false; Logging.instance .log("$e $s", level: LogLevel.Error); } @@ -428,7 +426,7 @@ class _DebugViewState extends ConsumerState { showDialog( context: context, builder: (context) => StackOkDialog( - title: logssaved + title: logsSaved ? "Logs saved to" : "Error Saving Logs", message: "${path!}/$filename", @@ -440,7 +438,7 @@ class _DebugViewState extends ConsumerState { showFloatingFlushBar( type: FlushBarType.info, context: context, - message: logssaved + message: logsSaved ? 'Logs file saved' : "Error Saving Logs", ), diff --git a/lib/pages/settings_views/global_settings_view/hidden_settings.dart b/lib/pages/settings_views/global_settings_view/hidden_settings.dart index f7b193a81..3d16e20b4 100644 --- a/lib/pages/settings_views/global_settings_view/hidden_settings.dart +++ b/lib/pages/settings_views/global_settings_view/hidden_settings.dart @@ -110,7 +110,7 @@ class HiddenSettings extends StatelessWidget { onTap: () async { await ref .read(debugServiceProvider) - .deleteAllMessages(); + .deleteAllLogs(); unawaited(showFloatingFlushBar( type: FlushBarType.success, diff --git a/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/debug_info_dialog.dart b/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/debug_info_dialog.dart index e6372fa33..38f0e5527 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/debug_info_dialog.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/advanced_settings/debug_info_dialog.dart @@ -8,7 +8,6 @@ import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/providers/global/debug_service_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; -import 'package:stackwallet/utilities/enums/flush_bar_type.dart'; import 'package:stackwallet/utilities/enums/log_level_enum.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; @@ -73,7 +72,6 @@ class _DebugInfoDialog extends ConsumerState { searchDebugController = TextEditingController(); searchDebugFocusNode = FocusNode(); - ref.read(debugServiceProvider).updateRecentLogs(); super.initState(); } @@ -109,7 +107,7 @@ class _DebugInfoDialog extends ConsumerState { ], ), Padding( - padding: EdgeInsets.symmetric(horizontal: 32), + padding: const EdgeInsets.symmetric(horizontal: 32), child: ClipRRect( borderRadius: BorderRadius.circular( Constants.size.circularBorderRadius, @@ -319,8 +317,8 @@ class _DebugInfoDialog extends ConsumerState { child: SecondaryButton( label: "Clear logs", onPressed: () async { - await ref.read(debugServiceProvider).deleteAllMessages(); - await ref.read(debugServiceProvider).updateRecentLogs(); + await ref.read(debugServiceProvider).deleteAllLogs(); + setState(() {}); if (mounted) { Navigator.pop(context); diff --git a/lib/services/debug_service.dart b/lib/services/debug_service.dart index 6557fc96e..dadb76990 100644 --- a/lib/services/debug_service.dart +++ b/lib/services/debug_service.dart @@ -15,8 +15,6 @@ class DebugService extends ChangeNotifier { late final Isar isar; // late final Stream logsChanged; - final int numberOfRecentLogsToLoad = 500; - // bool _shouldPause = false; // // void togglePauseUiUpdates() { @@ -36,44 +34,49 @@ class DebugService extends ChangeNotifier { // }); } - List _recentLogs = []; - List get recentLogs => _recentLogs; + List get recentLogs => isar.logs.where().limit(200).findAllSync(); - Future updateRecentLogs() async { - int totalCount = await isar.logs.count(); - int offset = totalCount - numberOfRecentLogsToLoad; - if (offset < 0) { - offset = 0; - } + // Future updateRecentLogs() async { + // int totalCount = await isar.logs.count(); + // int offset = totalCount - numberOfRecentLogsToLoad; + // if (offset < 0) { + // offset = 0; + // } + // + // _recentLogs = (await isar.logs + // .where() + // .anyTimestampInMillisUTC() + // .offset(offset) + // .limit(numberOfRecentLogsToLoad) + // .findAll()); + // notifyListeners(); + // } - _recentLogs = (await isar.logs - .where() - .anyTimestampInMillisUTC() - .offset(offset) - .limit(numberOfRecentLogsToLoad) - .findAll()); - notifyListeners(); - } - - Future deleteAllMessages() async { + Future deleteAllLogs() async { try { await isar.writeTxn(() async => await isar.logs.clear()); notifyListeners(); - } catch (e, s) { - //todo: come back to this - debugPrint("$e, $s"); + return true; + } catch (_) { + return false; } } - Future purgeInfoLogs() async { - final now = DateTime.now(); + Future deleteLogsOlderThan({ + Duration timeframe = const Duration(days: 30), + }) async { + final cutoffDate = DateTime.now().subtract(timeframe).toUtc(); await isar.writeTxn(() async { - await isar.logs.filter().logLevelEqualTo(LogLevel.Info).deleteAll(); + await isar.logs + .where() + .timestampInMillisUTCLessThan(cutoffDate.millisecondsSinceEpoch) + .deleteAll(); }); Logging.instance.log( - "Info logs purged in ${DateTime.now().difference(now).inMilliseconds} milliseconds", - level: LogLevel.Info); + "Logs older than $cutoffDate cleared!", + level: LogLevel.Info, + ); } /// returns the filename of the saved logs file