stack_wallet/lib/services/debug_service.dart

126 lines
3.2 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-08-26 08:11:35 +00:00
import 'dart:async';
import 'dart:io';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
2024-05-27 23:56:22 +00:00
2024-07-09 14:54:29 +00:00
import '../app_config.dart';
import '../models/isar/models/log.dart';
import '../utilities/logger.dart';
2022-08-26 08:11:35 +00:00
class DebugService extends ChangeNotifier {
DebugService._();
static final DebugService _instance = DebugService._();
static DebugService get instance => _instance;
late final Isar isar;
// late final Stream<void> logsChanged;
// bool _shouldPause = false;
//
// void togglePauseUiUpdates() {
// _shouldPause = !_shouldPause;
// notifyListeners();
// }
// bool get isPaused => _shouldPause;
Future<void> init(Isar isar) async {
this.isar = isar;
// logsChanged = this.isar.logs.watchLazy();
// logsChanged.listen((_) {
// if (!_shouldPause) {
// updateRecentLogs();
// }
// });
}
2024-05-27 23:56:22 +00:00
List<Log> get recentLogs => isar.logs
.where()
.sortByTimestampInMillisUTCDesc()
.limit(100)
.findAllSync();
2022-08-26 08:11:35 +00:00
// Future<void> 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();
// }
2022-08-26 08:11:35 +00:00
Future<bool> deleteAllLogs() async {
2022-08-26 08:11:35 +00:00
try {
await isar.writeTxn(() async => await isar.logs.clear());
notifyListeners();
return true;
} catch (_) {
return false;
2022-08-26 08:11:35 +00:00
}
}
Future<void> deleteLogsOlderThan({
Duration timeframe = const Duration(days: 30),
}) async {
final cutoffDate = DateTime.now().subtract(timeframe).toUtc();
2022-08-26 08:11:35 +00:00
await isar.writeTxn(() async {
await isar.logs
.where()
.timestampInMillisUTCLessThan(cutoffDate.millisecondsSinceEpoch)
.deleteAll();
2022-08-26 08:11:35 +00:00
});
Logging.instance.log(
"Logs older than $cutoffDate cleared!",
level: LogLevel.Info,
);
2022-08-26 08:11:35 +00:00
}
2022-10-01 00:46:52 +00:00
/// returns the filename of the saved logs file
Future<String> exportToFile(String directory, EventBus eventBus) async {
2022-08-26 08:11:35 +00:00
final now = DateTime.now();
final filename =
2024-07-09 14:54:29 +00:00
"${AppConfig.prefix}_Wallet_logs_${now.year}_${now.month}_${now.day}_${now.hour}_${now.minute}_${now.second}.txt";
2022-08-26 08:11:35 +00:00
final filepath = "$directory/$filename";
2024-05-27 23:56:22 +00:00
final File file = await File(filepath).create();
2022-08-26 08:11:35 +00:00
final sink = file.openWrite();
final logs = await isar.logs.where().anyTimestampInMillisUTC().findAll();
final count = logs.length;
int counter = 0;
for (final log in logs) {
sink.writeln(log);
await sink.flush();
counter++;
final exportPercent = (counter / count).clamp(0.0, 1.0);
eventBus.fire(exportPercent);
}
await sink.flush();
await sink.close();
eventBus.fire(1.0);
2022-10-01 00:46:52 +00:00
return filename;
2022-08-26 08:11:35 +00:00
}
}