only delete logs older than 30 days automatically

This commit is contained in:
julian 2023-02-13 15:08:38 -06:00
parent 4b1ff8fd09
commit 9f3ce454b5
5 changed files with 45 additions and 46 deletions

View file

@ -112,7 +112,7 @@ void main() async {
await DebugService.instance.init(isar); await DebugService.instance.init(isar);
// clear out all info logs on startup. No need to await and block // 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 // Registering Transaction Model Adapters

View file

@ -85,7 +85,6 @@ class _DebugViewState extends ConsumerState<DebugView> {
@override @override
void initState() { void initState() {
ref.read(debugServiceProvider).updateRecentLogs();
super.initState(); super.initState();
} }
@ -181,10 +180,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
await ref await ref
.read(debugServiceProvider) .read(debugServiceProvider)
.deleteAllMessages(); .deleteAllLogs();
await ref
.read(debugServiceProvider)
.updateRecentLogs();
shouldPop = true; shouldPop = true;
@ -194,6 +190,8 @@ class _DebugViewState extends ConsumerState<DebugView> {
type: FlushBarType.info, type: FlushBarType.info,
context: context, context: context,
message: 'Logs cleared!')); message: 'Logs cleared!'));
setState(() {});
} }
}, },
), ),
@ -313,7 +311,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
_searchTerm) _searchTerm)
.reversed .reversed
.toList(growable: false); .toList(growable: false);
List errorLogs = []; List<String> errorLogs = [];
for (var log in logs) { for (var log in logs) {
if (log.logLevel == LogLevel.Error || if (log.logLevel == LogLevel.Error ||
log.logLevel == LogLevel.Fatal) { log.logLevel == LogLevel.Fatal) {
@ -406,14 +404,14 @@ class _DebugViewState extends ConsumerState<DebugView> {
), ),
)); ));
bool logssaved = true; bool logsSaved = true;
var filename; String? filename;
try { try {
filename = await ref filename = await ref
.read(debugServiceProvider) .read(debugServiceProvider)
.exportToFile(path, eventBus); .exportToFile(path, eventBus);
} catch (e, s) { } catch (e, s) {
logssaved = false; logsSaved = false;
Logging.instance Logging.instance
.log("$e $s", level: LogLevel.Error); .log("$e $s", level: LogLevel.Error);
} }
@ -428,7 +426,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
showDialog( showDialog(
context: context, context: context,
builder: (context) => StackOkDialog( builder: (context) => StackOkDialog(
title: logssaved title: logsSaved
? "Logs saved to" ? "Logs saved to"
: "Error Saving Logs", : "Error Saving Logs",
message: "${path!}/$filename", message: "${path!}/$filename",
@ -440,7 +438,7 @@ class _DebugViewState extends ConsumerState<DebugView> {
showFloatingFlushBar( showFloatingFlushBar(
type: FlushBarType.info, type: FlushBarType.info,
context: context, context: context,
message: logssaved message: logsSaved
? 'Logs file saved' ? 'Logs file saved'
: "Error Saving Logs", : "Error Saving Logs",
), ),

View file

@ -110,7 +110,7 @@ class HiddenSettings extends StatelessWidget {
onTap: () async { onTap: () async {
await ref await ref
.read(debugServiceProvider) .read(debugServiceProvider)
.deleteAllMessages(); .deleteAllLogs();
unawaited(showFloatingFlushBar( unawaited(showFloatingFlushBar(
type: FlushBarType.success, type: FlushBarType.success,

View file

@ -8,7 +8,6 @@ import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/providers/global/debug_service_provider.dart'; import 'package:stackwallet/providers/global/debug_service_provider.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.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/enums/log_level_enum.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
@ -73,7 +72,6 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
searchDebugController = TextEditingController(); searchDebugController = TextEditingController();
searchDebugFocusNode = FocusNode(); searchDebugFocusNode = FocusNode();
ref.read(debugServiceProvider).updateRecentLogs();
super.initState(); super.initState();
} }
@ -109,7 +107,7 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
], ],
), ),
Padding( Padding(
padding: EdgeInsets.symmetric(horizontal: 32), padding: const EdgeInsets.symmetric(horizontal: 32),
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular( borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius, Constants.size.circularBorderRadius,
@ -319,8 +317,8 @@ class _DebugInfoDialog extends ConsumerState<DebugInfoDialog> {
child: SecondaryButton( child: SecondaryButton(
label: "Clear logs", label: "Clear logs",
onPressed: () async { onPressed: () async {
await ref.read(debugServiceProvider).deleteAllMessages(); await ref.read(debugServiceProvider).deleteAllLogs();
await ref.read(debugServiceProvider).updateRecentLogs(); setState(() {});
if (mounted) { if (mounted) {
Navigator.pop(context); Navigator.pop(context);

View file

@ -15,8 +15,6 @@ class DebugService extends ChangeNotifier {
late final Isar isar; late final Isar isar;
// late final Stream<void> logsChanged; // late final Stream<void> logsChanged;
final int numberOfRecentLogsToLoad = 500;
// bool _shouldPause = false; // bool _shouldPause = false;
// //
// void togglePauseUiUpdates() { // void togglePauseUiUpdates() {
@ -36,44 +34,49 @@ class DebugService extends ChangeNotifier {
// }); // });
} }
List<Log> _recentLogs = []; List<Log> get recentLogs => isar.logs.where().limit(200).findAllSync();
List<Log> get recentLogs => _recentLogs;
Future<void> updateRecentLogs() async { // Future<void> updateRecentLogs() async {
int totalCount = await isar.logs.count(); // int totalCount = await isar.logs.count();
int offset = totalCount - numberOfRecentLogsToLoad; // int offset = totalCount - numberOfRecentLogsToLoad;
if (offset < 0) { // if (offset < 0) {
offset = 0; // offset = 0;
} // }
//
// _recentLogs = (await isar.logs
// .where()
// .anyTimestampInMillisUTC()
// .offset(offset)
// .limit(numberOfRecentLogsToLoad)
// .findAll());
// notifyListeners();
// }
_recentLogs = (await isar.logs Future<bool> deleteAllLogs() async {
.where()
.anyTimestampInMillisUTC()
.offset(offset)
.limit(numberOfRecentLogsToLoad)
.findAll());
notifyListeners();
}
Future<void> deleteAllMessages() async {
try { try {
await isar.writeTxn(() async => await isar.logs.clear()); await isar.writeTxn(() async => await isar.logs.clear());
notifyListeners(); notifyListeners();
} catch (e, s) { return true;
//todo: come back to this } catch (_) {
debugPrint("$e, $s"); return false;
} }
} }
Future<void> purgeInfoLogs() async { Future<void> deleteLogsOlderThan({
final now = DateTime.now(); Duration timeframe = const Duration(days: 30),
}) async {
final cutoffDate = DateTime.now().subtract(timeframe).toUtc();
await isar.writeTxn(() async { await isar.writeTxn(() async {
await isar.logs.filter().logLevelEqualTo(LogLevel.Info).deleteAll(); await isar.logs
.where()
.timestampInMillisUTCLessThan(cutoffDate.millisecondsSinceEpoch)
.deleteAll();
}); });
Logging.instance.log( Logging.instance.log(
"Info logs purged in ${DateTime.now().difference(now).inMilliseconds} milliseconds", "Logs older than $cutoffDate cleared!",
level: LogLevel.Info); level: LogLevel.Info,
);
} }
/// returns the filename of the saved logs file /// returns the filename of the saved logs file