mirror of
https://github.com/gabehf/Fladder.git
synced 2026-03-07 21:48:14 -08:00
feature: Save logging to cache directory (#196)
Co-authored-by: PartyDonut <PartyDonut@users.noreply.github.com>
This commit is contained in:
parent
ae4707d3a6
commit
1a42be4be0
5 changed files with 143 additions and 62 deletions
90
lib/models/error_log_model.dart
Normal file
90
lib/models/error_log_model.dart
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
enum ErrorType {
|
||||
severe,
|
||||
warning,
|
||||
shout,
|
||||
}
|
||||
|
||||
class ErrorLogModel {
|
||||
final ErrorType type;
|
||||
final String message;
|
||||
final DateTime time;
|
||||
final StackTrace? stackTrace;
|
||||
|
||||
const ErrorLogModel({
|
||||
required this.type,
|
||||
required this.message,
|
||||
required this.time,
|
||||
required this.stackTrace,
|
||||
});
|
||||
|
||||
factory ErrorLogModel.fromLogRecord(LogRecord record) {
|
||||
late ErrorType type;
|
||||
if (record.level == Level.WARNING) {
|
||||
type = ErrorType.warning;
|
||||
} else if (record.level == Level.SHOUT) {
|
||||
type = ErrorType.shout;
|
||||
} else {
|
||||
type = ErrorType.severe;
|
||||
}
|
||||
|
||||
return ErrorLogModel(
|
||||
type: type,
|
||||
message: record.message,
|
||||
time: record.time,
|
||||
stackTrace: record.stackTrace,
|
||||
);
|
||||
}
|
||||
|
||||
String get label {
|
||||
var join = _label;
|
||||
if (join.length > 250) {
|
||||
return "${join.substring(0, 250)}... \n \nTruncated copy log to see more";
|
||||
} else {
|
||||
return join;
|
||||
}
|
||||
}
|
||||
|
||||
String get _label {
|
||||
return [
|
||||
type.name.toUpperCase(),
|
||||
" | ",
|
||||
message,
|
||||
].join();
|
||||
}
|
||||
|
||||
String get content => [
|
||||
time.toIso8601String(),
|
||||
"\n",
|
||||
"\n",
|
||||
stackTrace,
|
||||
].whereNotNull().join();
|
||||
|
||||
String get clipBoard => [_label, content].toString();
|
||||
|
||||
Color get color => switch (type) {
|
||||
ErrorType.severe => Colors.redAccent,
|
||||
ErrorType.warning => Colors.orange,
|
||||
ErrorType.shout => Colors.yellowAccent,
|
||||
};
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'time': time.toIso8601String(),
|
||||
'level': type.name,
|
||||
'message': message,
|
||||
'stackTrace': stackTrace?.toString(),
|
||||
};
|
||||
|
||||
static ErrorLogModel fromJson(Map<String, dynamic> json) {
|
||||
return ErrorLogModel(
|
||||
type: ErrorType.values.firstWhereOrNull((level) => level.name == json['level']) ?? ErrorType.warning,
|
||||
message: json['message'],
|
||||
stackTrace: json['stackTrace'] != null ? StackTrace.fromString(json['stackTrace']) : null,
|
||||
time: DateTime.parse(json['time']),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue