SimpleCloudNotifier/flutter/lib/state/application_log.dart

196 lines
4.9 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'dart:io';
2024-05-25 18:09:39 +02:00
import 'package:hive_flutter/hive_flutter.dart';
import 'package:simplecloudnotifier/state/globals.dart';
2024-05-26 00:20:25 +02:00
import 'package:simplecloudnotifier/state/interfaces.dart';
import 'package:xid/xid.dart';
import 'package:path/path.dart' as path;
2024-05-23 20:05:55 +02:00
2024-05-25 18:09:39 +02:00
part 'application_log.g.dart';
2024-05-26 00:20:25 +02:00
class ApplicationLog {
2024-06-15 16:33:30 +02:00
//TODO max size, auto clear old
2024-05-26 00:20:25 +02:00
static void debug(String message, {String? additional, StackTrace? trace}) {
2024-06-15 12:37:38 +02:00
(additional != null && additional != '') ? print('[DEBUG] ${message}: ${additional}') : print('[DEBUG] ${message}');
2024-05-25 18:09:39 +02:00
2024-06-15 21:29:51 +02:00
if (!Hive.isBoxOpen('scn-logs')) return;
2024-05-26 00:20:25 +02:00
Hive.box<SCNLog>('scn-logs').add(SCNLog(
id: Xid().toString(),
timestamp: DateTime.now(),
level: SCNLogLevel.debug,
message: message,
additional: additional ?? '',
trace: trace?.toString() ?? '',
));
}
static void info(String message, {String? additional, StackTrace? trace}) {
2024-06-15 12:37:38 +02:00
(additional != null && additional != '') ? print('[INFO] ${message}: ${additional}') : print('[INFO] ${message}');
2024-05-26 00:20:25 +02:00
2024-06-15 21:29:51 +02:00
if (!Hive.isBoxOpen('scn-logs')) return;
2024-05-26 00:20:25 +02:00
Hive.box<SCNLog>('scn-logs').add(SCNLog(
id: Xid().toString(),
timestamp: DateTime.now(),
level: SCNLogLevel.info,
message: message,
additional: additional ?? '',
trace: trace?.toString() ?? '',
));
}
static void warn(String message, {String? additional, StackTrace? trace}) {
2024-06-15 12:37:38 +02:00
(additional != null && additional != '') ? print('[WARN] ${message}: ${additional}') : print('[WARN] ${message}');
2024-05-26 00:20:25 +02:00
2024-06-15 21:29:51 +02:00
if (!Hive.isBoxOpen('scn-logs')) return;
2024-05-26 00:20:25 +02:00
Hive.box<SCNLog>('scn-logs').add(SCNLog(
id: Xid().toString(),
timestamp: DateTime.now(),
level: SCNLogLevel.warning,
message: message,
additional: additional ?? '',
trace: trace?.toString() ?? '',
));
}
static void error(String message, {String? additional, StackTrace? trace}) {
2024-06-15 12:37:38 +02:00
(additional != null && additional != '') ? print('[ERROR] ${message}: ${additional}') : print('[ERROR] ${message}');
2024-05-26 00:20:25 +02:00
2024-06-15 21:29:51 +02:00
if (!Hive.isBoxOpen('scn-logs')) return;
2024-05-26 00:20:25 +02:00
Hive.box<SCNLog>('scn-logs').add(SCNLog(
id: Xid().toString(),
timestamp: DateTime.now(),
level: SCNLogLevel.error,
message: message,
additional: additional ?? '',
trace: trace?.toString() ?? '',
));
}
static void fatal(String message, {String? additional, StackTrace? trace}) {
2024-06-15 12:37:38 +02:00
(additional != null && additional != '') ? print('[FATAL] ${message}: ${additional}') : print('[FATAL] ${message}');
2024-05-26 00:20:25 +02:00
2024-06-15 21:29:51 +02:00
if (!Hive.isBoxOpen('scn-logs')) return;
2024-05-26 00:20:25 +02:00
Hive.box<SCNLog>('scn-logs').add(SCNLog(
id: Xid().toString(),
timestamp: DateTime.now(),
level: SCNLogLevel.fatal,
message: message,
additional: additional ?? '',
trace: trace?.toString() ?? '',
));
}
static void writeRawFailure(String message, Map<String, dynamic> extraData) async {
try {
await Globals().init();
final fn = path.join(Globals().rawFailureLogsDir.path, 'failure-${DateTime.now().toIso8601String()}.log');
var txt = "[TEXT]\n${message}\n\n";
for (var k in extraData.keys) {
txt += "[${k}]\n${_debugToStr(extraData[k])}\n\n";
}
await File(fn).writeAsString(txt);
ApplicationLog.debug("Wrote raw failure log to '${fn}' ('${message}')");
} catch (e) {
print("Failed to <writeRawFailure>: ${e}");
}
}
static String _debugToStr(dynamic v) {
if (v is String) {
return v;
}
if (v is StackTrace) {
return v.toString();
}
try {
final enc = new JsonEncoder.withIndent(" ");
return enc.convert(v);
} catch (e) {
// ignore
}
try {
return jsonEncode(v);
} catch (e) {
// ignore
}
try {
return v.toString();
} catch (e) {
// ignore
}
try {
return "${v}";
} catch (e) {
// ignore
}
return "<[!]FAILED_TO_PRINT_OBJECT>";
}
2024-05-26 00:20:25 +02:00
}
@HiveType(typeId: 103)
enum SCNLogLevel {
@HiveField(0)
debug,
@HiveField(1)
info,
@HiveField(2)
warning,
@HiveField(3)
error,
@HiveField(4)
fatal
}
2024-05-25 18:09:39 +02:00
@HiveType(typeId: 101)
2024-05-26 00:20:25 +02:00
class SCNLog extends HiveObject implements FieldDebuggable {
2024-05-25 18:09:39 +02:00
@HiveField(0)
2024-05-26 00:20:25 +02:00
final String id;
@HiveField(10)
2024-05-25 18:09:39 +02:00
final DateTime timestamp;
2024-05-26 00:20:25 +02:00
@HiveField(11)
2024-05-25 18:09:39 +02:00
final SCNLogLevel level;
2024-05-26 00:20:25 +02:00
@HiveField(12)
2024-05-25 18:09:39 +02:00
final String message;
2024-05-26 00:20:25 +02:00
@HiveField(13)
2024-05-25 18:09:39 +02:00
final String additional;
2024-05-26 00:20:25 +02:00
@HiveField(14)
2024-05-25 18:09:39 +02:00
final String trace;
2024-05-26 00:20:25 +02:00
SCNLog({
required this.id,
required this.timestamp,
required this.level,
required this.message,
required this.additional,
required this.trace,
});
@override
String toString() {
return 'SCNLog[${this.id}]';
}
List<(String, String)> debugFieldList() {
return [
('id', this.id),
('timestamp', this.timestamp.toIso8601String()),
('level', this.level.name),
('message', this.message),
('additional', this.additional),
('trace', this.trace),
];
}
2024-05-25 18:09:39 +02:00
}