mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-09 16:07:57 -06:00
feat: finished new logging feature
This commit is contained in:
111
lib/core/logging/cubit/app_logs_cubit.dart
Normal file
111
lib/core/logging/cubit/app_logs_cubit.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:paperless_mobile/core/logging/models/parsed_log_message.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
part 'app_logs_state.dart';
|
||||
|
||||
final _fileNameFormat = DateFormat("yyyy-MM-dd");
|
||||
|
||||
class AppLogsCubit extends Cubit<AppLogsState> {
|
||||
StreamSubscription? _fileChangesSubscription;
|
||||
AppLogsCubit(DateTime date) : super(AppLogsStateInitial(date: date));
|
||||
|
||||
Future<void> loadLogs(DateTime date) async {
|
||||
if (date == state.date) {
|
||||
return;
|
||||
}
|
||||
_fileChangesSubscription?.cancel();
|
||||
emit(AppLogsStateLoading(date: date));
|
||||
final logDir = FileService.instance.logDirectory;
|
||||
final availableLogs = (await logDir
|
||||
.list()
|
||||
.whereType<File>()
|
||||
.where((event) => event.path.endsWith('.log'))
|
||||
.map((e) =>
|
||||
_fileNameFormat.parse(p.basenameWithoutExtension(e.path)))
|
||||
.toList())
|
||||
.sorted();
|
||||
final logFile = _getLogfile(date);
|
||||
if (!await logFile.exists()) {
|
||||
emit(AppLogsStateLoaded(
|
||||
date: date,
|
||||
logs: [],
|
||||
availableLogs: availableLogs,
|
||||
));
|
||||
}
|
||||
try {
|
||||
final logs = await logFile.readAsLines();
|
||||
final parsedLogs =
|
||||
ParsedLogMessage.parse(logs.skip(2000).toList()).reversed.toList();
|
||||
_fileChangesSubscription = logFile.watch().listen((event) async {
|
||||
if (!isClosed) {
|
||||
final logs = await logFile.readAsLines();
|
||||
emit(AppLogsStateLoaded(
|
||||
date: date,
|
||||
logs: parsedLogs,
|
||||
availableLogs: availableLogs,
|
||||
));
|
||||
}
|
||||
});
|
||||
emit(AppLogsStateLoaded(
|
||||
date: date,
|
||||
logs: parsedLogs,
|
||||
availableLogs: availableLogs,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(AppLogsStateError(
|
||||
error: e,
|
||||
date: date,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> clearLogs(DateTime date) async {
|
||||
final logFile = _getLogfile(date);
|
||||
await logFile.writeAsString('');
|
||||
await loadLogs(date);
|
||||
}
|
||||
|
||||
Future<void> copyToClipboard(DateTime date) async {
|
||||
final file = _getLogfile(date);
|
||||
if (!await file.exists()) {
|
||||
return;
|
||||
}
|
||||
final content = await file.readAsString();
|
||||
Clipboard.setData(ClipboardData(text: content));
|
||||
}
|
||||
|
||||
Future<void> saveLogs(DateTime date, String locale) async {
|
||||
var formattedDate = _fileNameFormat.format(date);
|
||||
final filename = 'paperless_mobile_logs_$formattedDate.log';
|
||||
final parentDir = await FilePicker.platform.getDirectoryPath(
|
||||
dialogTitle: "Save log from ${DateFormat.yMd(locale).format(date)}",
|
||||
initialDirectory: Platform.isAndroid
|
||||
? FileService.instance.downloadsDirectory.path
|
||||
: null,
|
||||
);
|
||||
final logFile = _getLogfile(date);
|
||||
if (parentDir != null) {
|
||||
await logFile.copy(p.join(parentDir, filename));
|
||||
}
|
||||
}
|
||||
|
||||
File _getLogfile(DateTime date) {
|
||||
return File(p.join(FileService.instance.logDirectory.path,
|
||||
'${_fileNameFormat.format(date)}.log'));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_fileChangesSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
33
lib/core/logging/cubit/app_logs_state.dart
Normal file
33
lib/core/logging/cubit/app_logs_state.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
part of 'app_logs_cubit.dart';
|
||||
|
||||
sealed class AppLogsState {
|
||||
final DateTime date;
|
||||
const AppLogsState({required this.date});
|
||||
}
|
||||
|
||||
class AppLogsStateInitial extends AppLogsState {
|
||||
const AppLogsStateInitial({required super.date});
|
||||
}
|
||||
|
||||
class AppLogsStateLoading extends AppLogsState {
|
||||
const AppLogsStateLoading({required super.date});
|
||||
}
|
||||
|
||||
class AppLogsStateLoaded extends AppLogsState {
|
||||
const AppLogsStateLoaded({
|
||||
required super.date,
|
||||
required this.logs,
|
||||
required this.availableLogs,
|
||||
});
|
||||
final List<DateTime> availableLogs;
|
||||
final List<ParsedLogMessage> logs;
|
||||
}
|
||||
|
||||
class AppLogsStateError extends AppLogsState {
|
||||
const AppLogsStateError({
|
||||
required this.error,
|
||||
required super.date,
|
||||
});
|
||||
|
||||
final Object error;
|
||||
}
|
||||
44
lib/core/logging/data/formatted_printer.dart
Normal file
44
lib/core/logging/data/formatted_printer.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:paperless_mobile/core/logging/models/formatted_log_message.dart';
|
||||
|
||||
class FormattedPrinter extends LogPrinter {
|
||||
static final _timestampFormat = DateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
static const _mulitlineObjectEncoder = JsonEncoder.withIndent(null);
|
||||
|
||||
@override
|
||||
List<String> log(LogEvent event) {
|
||||
final unformattedMessage = event.message;
|
||||
final formattedMessage = switch (unformattedMessage) {
|
||||
FormattedLogMessage m => m.format(),
|
||||
Iterable i => _mulitlineObjectEncoder
|
||||
.convert(i)
|
||||
.padLeft(FormattedLogMessage.maxLength),
|
||||
Map m => _mulitlineObjectEncoder
|
||||
.convert(m)
|
||||
.padLeft(FormattedLogMessage.maxLength),
|
||||
_ => unformattedMessage.toString().padLeft(FormattedLogMessage.maxLength),
|
||||
};
|
||||
final formattedLevel = event.level.name
|
||||
.toUpperCase()
|
||||
.padRight(Level.values.map((e) => e.name.length).max);
|
||||
final formattedTimestamp = _timestampFormat.format(event.time);
|
||||
|
||||
return [
|
||||
'$formattedTimestamp\t$formattedLevel --- $formattedMessage',
|
||||
if (event.error != null) ...[
|
||||
"---BEGIN ERROR---",
|
||||
event.error.toString(),
|
||||
"---END ERROR---",
|
||||
],
|
||||
if (event.stackTrace != null) ...[
|
||||
"---BEGIN STACKTRACE---",
|
||||
event.stackTrace.toString(),
|
||||
"---END STACKTRACE---"
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
116
lib/core/logging/data/logger.dart
Normal file
116
lib/core/logging/data/logger.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:paperless_mobile/core/logging/models/formatted_log_message.dart';
|
||||
|
||||
late Logger logger;
|
||||
|
||||
extension FormattedLoggerExtension on Logger {
|
||||
void ft(
|
||||
dynamic message, {
|
||||
String className = '',
|
||||
String methodName = '',
|
||||
DateTime? time,
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
}) {
|
||||
final formattedMessage = FormattedLogMessage(
|
||||
message,
|
||||
className: className,
|
||||
methodName: methodName,
|
||||
);
|
||||
log(
|
||||
Level.trace,
|
||||
formattedMessage,
|
||||
time: time,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
}
|
||||
|
||||
void fw(
|
||||
dynamic message, {
|
||||
String className = '',
|
||||
String methodName = '',
|
||||
DateTime? time,
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
}) {
|
||||
final formattedMessage = FormattedLogMessage(
|
||||
message,
|
||||
className: className,
|
||||
methodName: methodName,
|
||||
);
|
||||
log(
|
||||
Level.warning,
|
||||
formattedMessage,
|
||||
time: time,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
}
|
||||
|
||||
void fd(
|
||||
dynamic message, {
|
||||
String className = '',
|
||||
String methodName = '',
|
||||
DateTime? time,
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
}) {
|
||||
final formattedMessage = FormattedLogMessage(
|
||||
message,
|
||||
className: className,
|
||||
methodName: methodName,
|
||||
);
|
||||
log(
|
||||
Level.debug,
|
||||
formattedMessage,
|
||||
time: time,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
}
|
||||
|
||||
void fi(
|
||||
dynamic message, {
|
||||
String className = '',
|
||||
String methodName = '',
|
||||
DateTime? time,
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
}) {
|
||||
final formattedMessage = FormattedLogMessage(
|
||||
message,
|
||||
className: className,
|
||||
methodName: methodName,
|
||||
);
|
||||
log(
|
||||
Level.info,
|
||||
formattedMessage,
|
||||
time: time,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
}
|
||||
|
||||
void fe(
|
||||
dynamic message, {
|
||||
String className = '',
|
||||
String methodName = '',
|
||||
DateTime? time,
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
}) {
|
||||
final formattedMessage = FormattedLogMessage(
|
||||
message,
|
||||
className: className,
|
||||
methodName: methodName,
|
||||
);
|
||||
log(
|
||||
Level.error,
|
||||
formattedMessage,
|
||||
time: time,
|
||||
error: error,
|
||||
stackTrace: stackTrace,
|
||||
);
|
||||
}
|
||||
}
|
||||
54
lib/core/logging/data/mirrored_file_output.dart
Normal file
54
lib/core/logging/data/mirrored_file_output.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:synchronized/synchronized.dart';
|
||||
|
||||
class MirroredFileOutput extends LogOutput {
|
||||
final Completer _initCompleter = Completer();
|
||||
var lock = Lock();
|
||||
MirroredFileOutput();
|
||||
|
||||
late final File file;
|
||||
|
||||
@override
|
||||
Future<void> init() async {
|
||||
final today = DateFormat("yyyy-MM-dd").format(DateTime.now());
|
||||
final logDir = FileService.instance.logDirectory;
|
||||
file = File(p.join(logDir.path, '$today.log'));
|
||||
debugPrint("Logging files to ${file.path}.");
|
||||
_initCompleter.complete();
|
||||
try {
|
||||
final oldLogs = await FileService.instance.getAllFiles(logDir);
|
||||
if (oldLogs.length > 10) {
|
||||
oldLogs
|
||||
.sortedBy((file) => file.lastModifiedSync())
|
||||
.reversed
|
||||
.skip(10)
|
||||
.forEach((log) => log.delete());
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Failed to delete old logs...");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void output(OutputEvent event) async {
|
||||
await lock.synchronized(() async {
|
||||
for (var line in event.lines) {
|
||||
debugPrint(line);
|
||||
if (_initCompleter.isCompleted) {
|
||||
await file.writeAsString(
|
||||
"$line${Platform.lineTerminator}",
|
||||
mode: FileMode.append,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
late Logger logger;
|
||||
|
||||
class MirroredFileOutput extends LogOutput {
|
||||
late final File file;
|
||||
final Completer _initCompleter = Completer();
|
||||
MirroredFileOutput();
|
||||
|
||||
@override
|
||||
Future<void> init() async {
|
||||
final today = DateFormat("yyyy-MM-dd").format(DateTime.now());
|
||||
final logDir = await FileService.logDirectory;
|
||||
file = File(p.join(logDir.path, '$today.log'));
|
||||
debugPrint("Logging files to ${file.path}.");
|
||||
_initCompleter.complete();
|
||||
try {
|
||||
final oldLogs = await logDir.list().whereType<File>().toList();
|
||||
if (oldLogs.length > 10) {
|
||||
oldLogs
|
||||
.sortedBy((file) => file.lastModifiedSync())
|
||||
.reversed
|
||||
.skip(10)
|
||||
.forEach((log) => log.delete());
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Failed to delete old logs...");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void output(OutputEvent event) async {
|
||||
for (var line in event.lines) {
|
||||
debugPrint(line);
|
||||
if (_initCompleter.isCompleted) {
|
||||
await file.writeAsString(
|
||||
"$line\n",
|
||||
mode: FileMode.append,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SpringBootLikePrinter extends LogPrinter {
|
||||
SpringBootLikePrinter();
|
||||
static final _timestampFormat = DateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
@override
|
||||
List<String> log(LogEvent event) {
|
||||
final level = _buildLeftAligned(event.level.name.toUpperCase(),
|
||||
Level.values.map((e) => e.name.length).max);
|
||||
String message = _stringifyMessage(event.message);
|
||||
final timestamp =
|
||||
_buildLeftAligned(_timestampFormat.format(event.time), 23);
|
||||
final traceRegex = RegExp(r"(.*)#(.*)\(\): (.*)");
|
||||
final match = traceRegex.firstMatch(message);
|
||||
if (match != null) {
|
||||
final className = match.group(1)!;
|
||||
final methodName = match.group(2)!;
|
||||
final remainingMessage = match.group(3)!;
|
||||
final formattedClassName = _buildRightAligned(className, 25);
|
||||
final formattedMethodName = _buildLeftAligned(methodName, 25);
|
||||
message = message.replaceFirst(traceRegex,
|
||||
"[$formattedClassName] - $formattedMethodName: $remainingMessage");
|
||||
} else {
|
||||
message = List.filled(55, " ").join("") + ": " + message;
|
||||
}
|
||||
return [
|
||||
'$timestamp\t$level --- $message',
|
||||
if (event.error != null) '\t\t${event.error}',
|
||||
if (event.stackTrace != null) '\t\t${event.stackTrace.toString()}',
|
||||
];
|
||||
}
|
||||
|
||||
String _buildLeftAligned(String message, int maxLength) {
|
||||
return message.padRight(maxLength, ' ');
|
||||
}
|
||||
|
||||
String _buildRightAligned(String message, int maxLength) {
|
||||
return message.padLeft(maxLength, ' ');
|
||||
}
|
||||
|
||||
String _stringifyMessage(dynamic message) {
|
||||
final finalMessage = message is Function ? message() : message;
|
||||
if (finalMessage is Map || finalMessage is Iterable) {
|
||||
var encoder = const JsonEncoder.withIndent(null);
|
||||
return encoder.convert(finalMessage);
|
||||
} else {
|
||||
return finalMessage.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
lib/core/logging/models/formatted_log_message.dart
Normal file
19
lib/core/logging/models/formatted_log_message.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
/// Class passed to the printer to be formatted and printed.
|
||||
class FormattedLogMessage {
|
||||
static const maxLength = 55;
|
||||
final String message;
|
||||
final String methodName;
|
||||
final String className;
|
||||
|
||||
FormattedLogMessage(
|
||||
this.message, {
|
||||
required this.methodName,
|
||||
required this.className,
|
||||
});
|
||||
|
||||
String format() {
|
||||
final formattedClassName = className.padLeft(25);
|
||||
final formattedMethodName = methodName.padRight(25);
|
||||
return '[$formattedClassName] - $formattedMethodName: $message';
|
||||
}
|
||||
}
|
||||
148
lib/core/logging/models/parsed_log_message.dart
Normal file
148
lib/core/logging/models/parsed_log_message.dart
Normal file
@@ -0,0 +1,148 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
final _newLine = Platform.lineTerminator;
|
||||
|
||||
sealed class ParsedLogMessage {
|
||||
static List<ParsedLogMessage> parse(List<String> logs) {
|
||||
List<ParsedLogMessage> messages = [];
|
||||
int offset = 0;
|
||||
while (offset < logs.length) {
|
||||
final currentLine = logs[offset];
|
||||
if (ParsedFormattedLogMessage.canConsumeFirstLine(currentLine)) {
|
||||
final (consumedLines, result) =
|
||||
ParsedFormattedLogMessage.consume(logs.sublist(offset));
|
||||
messages.add(result);
|
||||
offset += consumedLines;
|
||||
} else {
|
||||
messages.add(UnformattedLogMessage(currentLine));
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
class ParsedErrorLogMessage {
|
||||
static final RegExp _errorBeginPattern = RegExp(r"---BEGIN ERROR---\s*");
|
||||
static final RegExp _errorEndPattern = RegExp(r"---END ERROR---\s*");
|
||||
static final RegExp _stackTraceBeginPattern =
|
||||
RegExp(r"---BEGIN STACKTRACE---\s*");
|
||||
static final RegExp _stackTraceEndPattern =
|
||||
RegExp(r"---END STACKTRACE---\s*");
|
||||
final String error;
|
||||
final String? stackTrace;
|
||||
ParsedErrorLogMessage({
|
||||
required this.error,
|
||||
this.stackTrace,
|
||||
});
|
||||
static bool canConsumeFirstLine(String line) =>
|
||||
_errorBeginPattern.hasMatch(line);
|
||||
|
||||
static (int consumedLines, ParsedErrorLogMessage result) consume(
|
||||
List<String> log) {
|
||||
assert(log.isNotEmpty && canConsumeFirstLine(log.first));
|
||||
String errorText = "";
|
||||
int currentLine =
|
||||
1; // Skip first because we know that the first line is ---BEGIN ERROR---
|
||||
while (!_errorEndPattern.hasMatch(log[currentLine])) {
|
||||
errorText += log[currentLine] + _newLine;
|
||||
currentLine++;
|
||||
}
|
||||
currentLine++;
|
||||
final hasStackTrace = _stackTraceBeginPattern.hasMatch(log[currentLine]);
|
||||
String? stackTrace;
|
||||
if (hasStackTrace) {
|
||||
currentLine++;
|
||||
String stackTraceText = '';
|
||||
|
||||
while (!_stackTraceEndPattern.hasMatch(log[currentLine])) {
|
||||
stackTraceText += log[currentLine] + _newLine;
|
||||
currentLine++;
|
||||
}
|
||||
stackTrace = stackTraceText;
|
||||
}
|
||||
return (
|
||||
currentLine + 1,
|
||||
ParsedErrorLogMessage(error: errorText, stackTrace: stackTrace)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnformattedLogMessage extends ParsedLogMessage {
|
||||
final String message;
|
||||
|
||||
UnformattedLogMessage(this.message);
|
||||
}
|
||||
|
||||
class ParsedFormattedLogMessage extends ParsedLogMessage {
|
||||
static final RegExp pattern = RegExp(
|
||||
r'(?<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s*(?<level>[A-Z]*)'
|
||||
r'\s*---\s*(?:\[\s*(?<className>.*)\]\s*-\s*(?<methodName>.*)\s*)?:\s*(?<message>.+)',
|
||||
);
|
||||
|
||||
final Level level;
|
||||
final String message;
|
||||
final String? className;
|
||||
final String? methodName;
|
||||
final DateTime timestamp;
|
||||
|
||||
final ParsedErrorLogMessage? error;
|
||||
|
||||
ParsedFormattedLogMessage({
|
||||
required this.level,
|
||||
required this.message,
|
||||
this.className,
|
||||
this.methodName,
|
||||
required this.timestamp,
|
||||
this.error,
|
||||
});
|
||||
|
||||
static bool canConsumeFirstLine(String line) => pattern.hasMatch(line);
|
||||
|
||||
static (int consumedLines, ParsedFormattedLogMessage result) consume(
|
||||
List<String> log) {
|
||||
assert(log.isNotEmpty && canConsumeFirstLine(log.first));
|
||||
|
||||
final match = pattern.firstMatch(log.first)!;
|
||||
final result = ParsedFormattedLogMessage(
|
||||
level: Level.values.byName(match.namedGroup('level')!.toLowerCase()),
|
||||
message: match.namedGroup('message')!,
|
||||
className: match.namedGroup('className'),
|
||||
methodName: match.namedGroup('methodName'),
|
||||
timestamp: DateTime.parse(match.namedGroup('timestamp')!),
|
||||
);
|
||||
final updatedLog = log.sublist(1);
|
||||
if (updatedLog.isEmpty) {
|
||||
return (1, result);
|
||||
}
|
||||
if (ParsedErrorLogMessage.canConsumeFirstLine(updatedLog.first)) {
|
||||
final (consumedLines, parsedError) =
|
||||
ParsedErrorLogMessage.consume(updatedLog);
|
||||
return (
|
||||
consumedLines + 1,
|
||||
result.copyWith(error: parsedError),
|
||||
);
|
||||
}
|
||||
return (1, result);
|
||||
}
|
||||
|
||||
ParsedFormattedLogMessage copyWith({
|
||||
Level? level,
|
||||
String? message,
|
||||
String? className,
|
||||
String? methodName,
|
||||
DateTime? timestamp,
|
||||
ParsedErrorLogMessage? error,
|
||||
}) {
|
||||
return ParsedFormattedLogMessage(
|
||||
level: level ?? this.level,
|
||||
message: message ?? this.message,
|
||||
className: className ?? this.className,
|
||||
methodName: methodName ?? this.methodName,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
}
|
||||
22
lib/core/logging/utils/redaction_utils.dart
Normal file
22
lib/core/logging/utils/redaction_utils.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
(String username, String obscuredUrl) splitRedactUserId(String userId) {
|
||||
final parts = userId.split('@');
|
||||
if (parts.length != 2) {
|
||||
return ('unknown', 'unknown');
|
||||
}
|
||||
|
||||
final username = parts.first;
|
||||
final serverUrl = parts.last;
|
||||
final uri = Uri.parse(serverUrl);
|
||||
final hostLen = uri.host.length;
|
||||
final obscuredUrl = uri.scheme +
|
||||
"://" +
|
||||
uri.host.substring(0, 2) +
|
||||
List.filled(hostLen - 4, '*').join() +
|
||||
uri.host.substring(uri.host.length - 2, uri.host.length);
|
||||
return (username, obscuredUrl);
|
||||
}
|
||||
|
||||
String redactUserId(String userId) {
|
||||
final (username, obscuredUrl) = splitRedactUserId(userId);
|
||||
return '$username@$obscuredUrl';
|
||||
}
|
||||
@@ -1,19 +1,13 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:paperless_mobile/core/logging/cubit/app_logs_cubit.dart';
|
||||
import 'package:paperless_mobile/core/logging/models/parsed_log_message.dart';
|
||||
import 'package:paperless_mobile/extensions/dart_extensions.dart';
|
||||
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:rxdart/subjects.dart';
|
||||
|
||||
final _fileNameFormat = DateFormat("yyyy-MM-dd");
|
||||
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
|
||||
|
||||
class AppLogsPage extends StatefulWidget {
|
||||
const AppLogsPage({super.key});
|
||||
@@ -23,304 +17,181 @@ class AppLogsPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AppLogsPageState extends State<AppLogsPage> {
|
||||
final _fileContentStream = BehaviorSubject();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
|
||||
StreamSubscription? _fileChangesSubscription;
|
||||
|
||||
late DateTime _date;
|
||||
File? file;
|
||||
bool autoScroll = true;
|
||||
List<DateTime>? _availableLogs;
|
||||
|
||||
Future<void> _initFile() async {
|
||||
final logDir = await FileService.logDirectory;
|
||||
// logDir.listSync().whereType<File>().forEach((element) {
|
||||
// element.deleteSync();
|
||||
// });
|
||||
if (logDir.listSync().isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final filename = _fileNameFormat.format(_date);
|
||||
setState(() {
|
||||
file = File(p.join(logDir.path, '$filename.log'));
|
||||
});
|
||||
_scrollController.addListener(_initialScrollListener);
|
||||
_updateFileContent();
|
||||
_fileChangesSubscription?.cancel();
|
||||
_fileChangesSubscription = file!.watch().listen((event) async {
|
||||
await _updateFileContent();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _initialScrollListener() {
|
||||
if (_scrollController.positions.isNotEmpty) {
|
||||
_scrollController.animateTo(
|
||||
0,
|
||||
duration: 500.milliseconds,
|
||||
curve: Curves.easeIn,
|
||||
);
|
||||
_scrollController.removeListener(_initialScrollListener);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_date = DateTime.now().copyWith(
|
||||
minute: 0,
|
||||
hour: 0,
|
||||
second: 0,
|
||||
millisecond: 0,
|
||||
microsecond: 0,
|
||||
);
|
||||
_initFile();
|
||||
() async {
|
||||
final logDir = await FileService.logDirectory;
|
||||
final files = logDir.listSync(followLinks: false).whereType<File>();
|
||||
final fileNames = files.map((e) => p.basenameWithoutExtension(e.path));
|
||||
final dates =
|
||||
fileNames.map((filename) => _fileNameFormat.parseStrict(filename));
|
||||
_availableLogs = dates.toList();
|
||||
}();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_fileChangesSubscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final locale = Localizations.localeOf(context).toString();
|
||||
final theme = Theme.of(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text("Logs"),
|
||||
SizedBox(width: 16),
|
||||
DropdownButton<DateTime>(
|
||||
|
||||
value: _date,
|
||||
items: [
|
||||
for (var date in _availableLogs ?? [])
|
||||
DropdownMenuItem(
|
||||
child: Text(DateFormat.yMMMd(locale).format(date)),
|
||||
value: date,
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_date = value;
|
||||
});
|
||||
_initFile();
|
||||
}
|
||||
return BlocBuilder<AppLogsCubit, AppLogsState>(
|
||||
builder: (context, state) {
|
||||
final formattedDate = DateFormat.yMMMd(locale).format(state.date);
|
||||
return Scaffold(
|
||||
bottomNavigationBar: BottomAppBar(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: switch (state) {
|
||||
AppLogsStateInitial() => [],
|
||||
AppLogsStateLoading() => [],
|
||||
AppLogsStateLoaded() => [
|
||||
IconButton(
|
||||
tooltip: S.of(context)!.copyToClipboard,
|
||||
onPressed: () {
|
||||
context
|
||||
.read<AppLogsCubit>()
|
||||
.copyToClipboard(state.date);
|
||||
},
|
||||
icon: const Icon(Icons.copy),
|
||||
).padded(),
|
||||
IconButton(
|
||||
tooltip: S.of(context)!.saveLogsToFile,
|
||||
onPressed: () {
|
||||
context
|
||||
.read<AppLogsCubit>()
|
||||
.saveLogs(state.date, locale);
|
||||
},
|
||||
icon: const Icon(Icons.download),
|
||||
).padded(),
|
||||
IconButton(
|
||||
tooltip: S.of(context)!.clearLogs(formattedDate),
|
||||
onPressed: () {
|
||||
context.read<AppLogsCubit>().clearLogs(state.date);
|
||||
},
|
||||
icon: Icon(
|
||||
Icons.delete_sweep,
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
).padded(),
|
||||
],
|
||||
_ => [],
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: file != null
|
||||
? [
|
||||
),
|
||||
appBar: AppBar(
|
||||
title: Text(S.of(context)!.appLogs(formattedDate)),
|
||||
actions: [
|
||||
if (state is AppLogsStateLoaded)
|
||||
IconButton(
|
||||
tooltip: "Save log file to selected directory",
|
||||
onPressed: () => _saveFile(locale),
|
||||
icon: const Icon(Icons.download),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: "Copy logs to clipboard",
|
||||
onPressed: _copyToClipboard,
|
||||
icon: const Icon(Icons.copy),
|
||||
tooltip: MaterialLocalizations.of(context).datePickerHelpText,
|
||||
onPressed: () async {
|
||||
final selectedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: state.date,
|
||||
firstDate: state.availableLogs.first,
|
||||
lastDate: state.availableLogs.last,
|
||||
selectableDayPredicate: (day) => state.availableLogs
|
||||
.any((date) => day.isOnSameDayAs(date)),
|
||||
initialEntryMode: DatePickerEntryMode.calendarOnly,
|
||||
);
|
||||
if (selectedDate != null) {
|
||||
context.read<AppLogsCubit>().loadLogs(selectedDate);
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.calendar_today),
|
||||
).padded(),
|
||||
]
|
||||
: null,
|
||||
),
|
||||
body: Builder(
|
||||
builder: (context) {
|
||||
if (_availableLogs == null) {
|
||||
return Center(
|
||||
child: Text("No logs available."),
|
||||
);
|
||||
}
|
||||
return StreamBuilder(
|
||||
stream: _fileContentStream,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData || file == null) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
"Initializing logs...",
|
||||
),
|
||||
);
|
||||
}
|
||||
final messages = _transformLog(snapshot.data!).reversed.toList();
|
||||
return ColoredBox(
|
||||
color: theme.colorScheme.background,
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
reverse: true,
|
||||
controller: _scrollController,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) {
|
||||
return Center(
|
||||
child: Text(
|
||||
"End of logs.",
|
||||
style: theme.textTheme.labelLarge?.copyWith(
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
).padded(24);
|
||||
}
|
||||
final logMessage = messages[index - 1];
|
||||
final altColor = CupertinoDynamicColor.withBrightness(
|
||||
color: Colors.grey.shade200,
|
||||
darkColor: Colors.grey.shade800,
|
||||
).resolveFrom(context);
|
||||
return _LogMessageWidget(
|
||||
message: logMessage,
|
||||
backgroundColor: (index % 2 == 0)
|
||||
? theme.colorScheme.background
|
||||
: altColor,
|
||||
);
|
||||
},
|
||||
itemCount: messages.length + 1,
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
body: switch (state) {
|
||||
AppLogsStateLoaded(
|
||||
logs: var logs,
|
||||
) =>
|
||||
Builder(
|
||||
builder: (context) {
|
||||
if (state.logs.isEmpty) {
|
||||
return Center(
|
||||
child: Text(S.of(context)!.noLogsFoundOn(formattedDate)),
|
||||
);
|
||||
}
|
||||
return ListView.builder(
|
||||
reverse: true,
|
||||
controller: _scrollController,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) {
|
||||
return Center(
|
||||
child: Text(S.of(context)!.logfileBottomReached,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.disabledColor,
|
||||
)),
|
||||
).padded(24);
|
||||
}
|
||||
final messages = state.logs;
|
||||
final logMessage = messages[index - 1];
|
||||
final altColor = CupertinoDynamicColor.withBrightness(
|
||||
color: Colors.grey.shade200,
|
||||
darkColor: Colors.grey.shade800,
|
||||
).resolveFrom(context);
|
||||
return ParsedLogMessageTile(
|
||||
message: logMessage,
|
||||
backgroundColor: (index % 2 == 0)
|
||||
? theme.colorScheme.background
|
||||
: altColor,
|
||||
);
|
||||
},
|
||||
itemCount: logs.length + 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
AppLogsStateError() => Center(
|
||||
child: Text(
|
||||
S.of(context)!.couldNotLoadLogfileFrom(formattedDate),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
_ => _buildLoadingLogs(state.date)
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingLogs(DateTime date) {
|
||||
final formattedDate =
|
||||
DateFormat.yMd(Localizations.localeOf(context).toString()).format(date);
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const CircularProgressIndicator(),
|
||||
Text(S.of(context)!.loadingLogsFrom(formattedDate)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _saveFile(String locale) async {
|
||||
assert(file != null);
|
||||
var formattedDate = _fileNameFormat.format(_date);
|
||||
final filename = 'paperless_mobile_logs_$formattedDate.log';
|
||||
final parentDir = await FilePicker.platform.getDirectoryPath(
|
||||
dialogTitle: "Save log from ${DateFormat.yMd(locale).format(_date)}",
|
||||
initialDirectory:
|
||||
Platform.isAndroid ? "/storage/emulated/0/Download/" : null,
|
||||
);
|
||||
if (parentDir != null) {
|
||||
await file!.copy(p.join(parentDir, filename));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _copyToClipboard() async {
|
||||
assert(file != null);
|
||||
final content = await file!.readAsString();
|
||||
await Clipboard.setData(ClipboardData(text: content));
|
||||
}
|
||||
|
||||
List<_LogMessage> _transformLog(String log) {
|
||||
List<_LogMessage> messages = [];
|
||||
List<String> currentCoherentLines = [];
|
||||
final lines = log.split("\n");
|
||||
for (var line in lines) {
|
||||
final isMatch = _LogMessage.hasMatch(line);
|
||||
if (currentCoherentLines.isNotEmpty && isMatch) {
|
||||
messages.add(_LogMessage(message: currentCoherentLines.join("\n")));
|
||||
currentCoherentLines.clear();
|
||||
messages.add(_LogMessage.fromMessage(line));
|
||||
}
|
||||
if (_LogMessage.hasMatch(line)) {
|
||||
messages.add(_LogMessage.fromMessage(line));
|
||||
} else {
|
||||
currentCoherentLines.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
Future<void> _updateFileContent() async {
|
||||
final content = await file!.readAsString();
|
||||
_fileContentStream.add(content);
|
||||
Future.delayed(400.milliseconds, () {
|
||||
_scrollController.animateTo(
|
||||
0,
|
||||
duration: 500.milliseconds,
|
||||
curve: Curves.easeIn,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class _LogMessage {
|
||||
static final RegExp pattern = RegExp(
|
||||
r'(?<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s*(?<level>[A-Z]*)'
|
||||
r'\s+---\s*(?:\[\s*(?<className>.*)\]\s*-\s*(?<methodName>.*)\s*)?:\s*(?<message>.+)',
|
||||
);
|
||||
final Level? level;
|
||||
final String message;
|
||||
final String? className;
|
||||
final String? methodName;
|
||||
final DateTime? timestamp;
|
||||
|
||||
bool get isFormatted => level != null;
|
||||
const _LogMessage({
|
||||
this.level,
|
||||
required this.message,
|
||||
this.className,
|
||||
this.methodName,
|
||||
this.timestamp,
|
||||
});
|
||||
|
||||
static bool hasMatch(String message) => pattern.hasMatch(message);
|
||||
|
||||
factory _LogMessage.fromMessage(String message) {
|
||||
final match = pattern.firstMatch(message);
|
||||
if (match == null) {
|
||||
return _LogMessage(message: message);
|
||||
}
|
||||
return _LogMessage(
|
||||
level: Level.values.byName(match.namedGroup('level')!.toLowerCase()),
|
||||
message: match.namedGroup('message')!,
|
||||
className: match.namedGroup('className'),
|
||||
methodName: match.namedGroup('methodName'),
|
||||
timestamp: DateTime.tryParse(match.namedGroup('timestamp') ?? ''),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _LogMessageWidget extends StatelessWidget {
|
||||
final _LogMessage message;
|
||||
class ParsedLogMessageTile extends StatelessWidget {
|
||||
final ParsedLogMessage message;
|
||||
final Color backgroundColor;
|
||||
const _LogMessageWidget({
|
||||
|
||||
const ParsedLogMessageTile({
|
||||
super.key,
|
||||
required this.message,
|
||||
required this.backgroundColor,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final c = Theme.of(context).colorScheme;
|
||||
if (!message.isFormatted) {
|
||||
return Text(
|
||||
message.message,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontSize: 5,
|
||||
color: c.onBackground.withOpacity(0.7),
|
||||
),
|
||||
);
|
||||
}
|
||||
final color = switch (message.level) {
|
||||
Level.trace => c.onBackground.withOpacity(0.75),
|
||||
Level.warning => Colors.yellow.shade600,
|
||||
Level.error => Colors.red,
|
||||
Level.fatal => Colors.red.shade900,
|
||||
_ => c.onBackground,
|
||||
return switch (message) {
|
||||
ParsedFormattedLogMessage m => FormattedLogMessageWidget(
|
||||
message: m,
|
||||
backgroundColor: backgroundColor,
|
||||
),
|
||||
UnformattedLogMessage(message: var m) => Text(m),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class FormattedLogMessageWidget extends StatelessWidget {
|
||||
final ParsedFormattedLogMessage message;
|
||||
final Color backgroundColor;
|
||||
const FormattedLogMessageWidget(
|
||||
{super.key, required this.message, required this.backgroundColor});
|
||||
static final _timeFormat = DateFormat("HH:mm:ss.SSS");
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final c = Theme.of(context).colorScheme;
|
||||
|
||||
final icon = switch (message.level) {
|
||||
Level.trace => Icons.troubleshoot,
|
||||
Level.debug => Icons.bug_report,
|
||||
@@ -330,31 +201,83 @@ class _LogMessageWidget extends StatelessWidget {
|
||||
Level.fatal => Icons.error_outline,
|
||||
_ => null,
|
||||
};
|
||||
final color = switch (message.level) {
|
||||
Level.trace => c.onBackground.withOpacity(0.75),
|
||||
Level.warning => Colors.yellow.shade600,
|
||||
Level.error => Colors.red,
|
||||
Level.fatal => Colors.red.shade900,
|
||||
Level.info => Colors.blue,
|
||||
_ => c.onBackground,
|
||||
};
|
||||
|
||||
final logStyle = Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 12,
|
||||
);
|
||||
final formattedMethodName =
|
||||
message.methodName != null ? '${message.methodName!.trim()}()' : '';
|
||||
final source = switch (message.className) {
|
||||
'' || null => formattedMethodName,
|
||||
String className => '$className.$formattedMethodName',
|
||||
};
|
||||
return Material(
|
||||
child: ListTile(
|
||||
color: backgroundColor,
|
||||
child: ExpansionTile(
|
||||
leading: Text(
|
||||
_timeFormat.format(message.timestamp),
|
||||
style: logStyle?.copyWith(color: color),
|
||||
),
|
||||
title: Text(
|
||||
message.message,
|
||||
style: logStyle?.copyWith(color: color),
|
||||
),
|
||||
trailing: Icon(
|
||||
icon,
|
||||
color: color,
|
||||
),
|
||||
tileColor: backgroundColor,
|
||||
title: Text(
|
||||
message.message,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
subtitle: message.className != null
|
||||
? Text(
|
||||
"${message.className ?? ''} ${message.methodName ?? ''}",
|
||||
style: TextStyle(
|
||||
color: color.withOpacity(0.75),
|
||||
fontSize: 10,
|
||||
fontFamily: "monospace",
|
||||
expandedCrossAxisAlignment: CrossAxisAlignment.start,
|
||||
childrenPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
|
||||
expandedAlignment: Alignment.topLeft,
|
||||
children: source.isNotEmpty
|
||||
? [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.arrow_right),
|
||||
Flexible(
|
||||
child: Text(
|
||||
'In $source',
|
||||
style: logStyle?.copyWith(fontSize: 14),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: null,
|
||||
leading: message.timestamp != null
|
||||
? Text(DateFormat("HH:mm:ss.SSS").format(message.timestamp!))
|
||||
: null,
|
||||
..._buildErrorWidgets(context),
|
||||
]
|
||||
: _buildErrorWidgets(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildErrorWidgets(BuildContext context) {
|
||||
if (message.error != null) {
|
||||
return [
|
||||
Divider(),
|
||||
Text(
|
||||
message.error!.error,
|
||||
style: TextStyle(color: Colors.red),
|
||||
).padded(),
|
||||
if (message.error?.stackTrace != null) ...[
|
||||
Text(
|
||||
message.error!.stackTrace!,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 10,
|
||||
),
|
||||
).paddedOnly(left: 8),
|
||||
],
|
||||
];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,166 +1,260 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:paperless_mobile/core/logging/logger.dart';
|
||||
import 'package:paperless_mobile/core/logging/data/logger.dart';
|
||||
import 'package:paperless_mobile/core/logging/utils/redaction_utils.dart';
|
||||
import 'package:paperless_mobile/helpers/format_helpers.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class FileService {
|
||||
const FileService._();
|
||||
FileService._();
|
||||
|
||||
static Future<File> saveToFile(
|
||||
static FileService? _singleton;
|
||||
|
||||
late final Directory _logDirectory;
|
||||
late final Directory _temporaryDirectory;
|
||||
late final Directory _documentsDirectory;
|
||||
late final Directory _downloadsDirectory;
|
||||
late final Directory _uploadDirectory;
|
||||
late final Directory _temporaryScansDirectory;
|
||||
|
||||
Directory get logDirectory => _logDirectory;
|
||||
Directory get temporaryDirectory => _temporaryDirectory;
|
||||
Directory get documentsDirectory => _documentsDirectory;
|
||||
Directory get downloadsDirectory => _downloadsDirectory;
|
||||
Directory get uploadDirectory => _uploadDirectory;
|
||||
Directory get temporaryScansDirectory => _temporaryScansDirectory;
|
||||
|
||||
Future<void> initialize() async {
|
||||
try {
|
||||
await _initTemporaryDirectory();
|
||||
await _initTemporaryScansDirectory();
|
||||
await _initUploadDirectory();
|
||||
await _initLogDirectory();
|
||||
await _initDownloadsDirectory();
|
||||
await _initializeDocumentsDirectory();
|
||||
} catch (error, stackTrace) {
|
||||
debugPrint("Could not initialize directories.");
|
||||
debugPrint(error.toString());
|
||||
debugPrintStack(stackTrace: stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
/// Make sure to call and await initialize before accessing any of the instance members.
|
||||
static FileService get instance {
|
||||
_singleton ??= FileService._();
|
||||
return _singleton!;
|
||||
}
|
||||
|
||||
Future<File> saveToFile(
|
||||
Uint8List bytes,
|
||||
String filename,
|
||||
) async {
|
||||
final dir = await documentsDirectory;
|
||||
File file = File("${dir.path}/$filename");
|
||||
File file = File(p.join(_logDirectory.path, filename));
|
||||
logger.fd(
|
||||
"Writing bytes to file $filename",
|
||||
methodName: 'saveToFile',
|
||||
className: runtimeType.toString(),
|
||||
);
|
||||
return file..writeAsBytes(bytes);
|
||||
}
|
||||
|
||||
static Future<Directory?> getDirectory(PaperlessDirectoryType type) {
|
||||
Directory getDirectory(PaperlessDirectoryType type) {
|
||||
return switch (type) {
|
||||
PaperlessDirectoryType.documents => documentsDirectory,
|
||||
PaperlessDirectoryType.temporary => temporaryDirectory,
|
||||
PaperlessDirectoryType.scans => temporaryScansDirectory,
|
||||
PaperlessDirectoryType.download => downloadsDirectory,
|
||||
PaperlessDirectoryType.upload => uploadDirectory,
|
||||
PaperlessDirectoryType.documents => _documentsDirectory,
|
||||
PaperlessDirectoryType.temporary => _temporaryDirectory,
|
||||
PaperlessDirectoryType.scans => _temporaryScansDirectory,
|
||||
PaperlessDirectoryType.download => _downloadsDirectory,
|
||||
PaperlessDirectoryType.upload => _uploadDirectory,
|
||||
PaperlessDirectoryType.logs => _logDirectory,
|
||||
};
|
||||
}
|
||||
|
||||
static Future<File> allocateTemporaryFile(
|
||||
///
|
||||
/// Returns a [File] pointing to a temporary file in the directory specified by [type].
|
||||
/// If [create] is true, the file will be created.
|
||||
/// If [fileName] is left blank, a random UUID will be generated.
|
||||
///
|
||||
Future<File> allocateTemporaryFile(
|
||||
PaperlessDirectoryType type, {
|
||||
required String extension,
|
||||
String? fileName,
|
||||
bool create = false,
|
||||
}) async {
|
||||
final dir = await getDirectory(type);
|
||||
final _fileName = (fileName ?? const Uuid().v1()) + '.$extension';
|
||||
return File('${dir?.path}/$_fileName');
|
||||
}
|
||||
|
||||
static Future<Directory> get temporaryDirectory => getTemporaryDirectory();
|
||||
|
||||
static Future<Directory> get documentsDirectory async {
|
||||
if (Platform.isAndroid) {
|
||||
return (await getExternalStorageDirectories(
|
||||
type: StorageDirectory.documents,
|
||||
))!
|
||||
.first;
|
||||
} else if (Platform.isIOS) {
|
||||
final dir = await getApplicationDocumentsDirectory()
|
||||
.then((dir) => Directory('${dir.path}/documents'));
|
||||
return dir.create(recursive: true);
|
||||
} else {
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
final dir = getDirectory(type);
|
||||
final filename = (fileName ?? const Uuid().v1()) + '.$extension';
|
||||
final file = File(p.join(dir.path, filename));
|
||||
if (create) {
|
||||
await file.create(recursive: true);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
static Future<Directory> get logDirectory async {
|
||||
if (Platform.isAndroid) {
|
||||
return getExternalStorageDirectories(type: StorageDirectory.documents)
|
||||
.then((directory) async =>
|
||||
directory?.firstOrNull ??
|
||||
await getApplicationDocumentsDirectory())
|
||||
.then((directory) =>
|
||||
Directory('${directory.path}/logs').create(recursive: true));
|
||||
} else if (Platform.isIOS) {
|
||||
return getApplicationDocumentsDirectory().then(
|
||||
(value) => Directory('${value.path}/logs').create(recursive: true));
|
||||
}
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
Future<Directory> getConsumptionDirectory({required String userId}) async {
|
||||
return Directory(p.join(_uploadDirectory.path, userId))
|
||||
.create(recursive: true);
|
||||
}
|
||||
|
||||
static Future<Directory> get downloadsDirectory async {
|
||||
if (Platform.isAndroid) {
|
||||
var directory = Directory('/storage/emulated/0/Download');
|
||||
if (!directory.existsSync()) {
|
||||
final downloadsDir = await getExternalStorageDirectories(
|
||||
type: StorageDirectory.downloads,
|
||||
);
|
||||
directory = downloadsDir!.first;
|
||||
}
|
||||
return directory;
|
||||
} else if (Platform.isIOS) {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final dir = Directory('${appDir.path}/downloads');
|
||||
return dir.create(recursive: true);
|
||||
} else {
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
}
|
||||
}
|
||||
Future<void> clearUserData({required String userId}) async {
|
||||
final redactedId = redactUserId(userId);
|
||||
logger.fd(
|
||||
"Clearing data for user $redactedId...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
|
||||
static Future<Directory> get uploadDirectory async {
|
||||
final dir = await getApplicationDocumentsDirectory()
|
||||
.then((dir) => Directory('${dir.path}/upload'));
|
||||
return dir.create(recursive: true);
|
||||
}
|
||||
|
||||
static Future<Directory> getConsumptionDirectory(
|
||||
{required String userId}) async {
|
||||
final uploadDir =
|
||||
await uploadDirectory.then((dir) => Directory('${dir.path}/$userId'));
|
||||
return uploadDir.create(recursive: true);
|
||||
}
|
||||
|
||||
static Future<Directory> get temporaryScansDirectory async {
|
||||
final tempDir = await temporaryDirectory;
|
||||
final scansDir = Directory('${tempDir.path}/scans');
|
||||
return scansDir.create(recursive: true);
|
||||
}
|
||||
|
||||
static Future<void> clearUserData({required String userId}) async {
|
||||
logger.t("FileService#clearUserData(): Clearing data for user $userId...");
|
||||
|
||||
final scanDir = await temporaryScansDirectory;
|
||||
final scanDirSize = formatBytes(await getDirSizeInBytes(scanDir));
|
||||
final tempDir = await temporaryDirectory;
|
||||
final tempDirSize = formatBytes(await getDirSizeInBytes(tempDir));
|
||||
final scanDirSize =
|
||||
formatBytes(await getDirSizeInBytes(_temporaryScansDirectory));
|
||||
final tempDirSize =
|
||||
formatBytes(await getDirSizeInBytes(_temporaryDirectory));
|
||||
final consumptionDir = await getConsumptionDirectory(userId: userId);
|
||||
final consumptionDirSize =
|
||||
formatBytes(await getDirSizeInBytes(consumptionDir));
|
||||
|
||||
logger.t("FileService#clearUserData(): Removing scans...");
|
||||
await scanDir.delete(recursive: true);
|
||||
logger.t("FileService#clearUserData(): Removed $scanDirSize...");
|
||||
logger.ft(
|
||||
"Removing scans...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
await _temporaryScansDirectory.delete(recursive: true);
|
||||
logger.ft(
|
||||
"Removed $scanDirSize...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
logger.ft(
|
||||
"Removing temporary files and cache content...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
|
||||
logger.t(
|
||||
"FileService#clearUserData(): Removing temporary files and cache content...");
|
||||
await _temporaryDirectory.delete(recursive: true);
|
||||
logger.ft(
|
||||
"Removed $tempDirSize...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
|
||||
await tempDir.delete(recursive: true);
|
||||
logger.t("FileService#clearUserData(): Removed $tempDirSize...");
|
||||
|
||||
logger.t(
|
||||
"FileService#clearUserData(): Removing files waiting for consumption...");
|
||||
logger.ft(
|
||||
"Removing files waiting for consumption...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
await consumptionDir.delete(recursive: true);
|
||||
logger.t("FileService#clearUserData(): Removed $consumptionDirSize...");
|
||||
}
|
||||
|
||||
static Future<void> clearDirectoryContent(PaperlessDirectoryType type) async {
|
||||
final dir = await getDirectory(type);
|
||||
|
||||
if (dir == null || !(await dir.exists())) {
|
||||
return;
|
||||
}
|
||||
|
||||
await Future.wait(
|
||||
dir.listSync().map((item) => item.delete(recursive: true)),
|
||||
logger.ft(
|
||||
"Removed $consumptionDirSize...",
|
||||
className: runtimeType.toString(),
|
||||
methodName: "clearUserData",
|
||||
);
|
||||
}
|
||||
|
||||
static Future<List<File>> getAllFiles(Directory directory) {
|
||||
Future<int> clearDirectoryContent(
|
||||
PaperlessDirectoryType type, {
|
||||
bool filesOnly = false,
|
||||
}) async {
|
||||
final dir = getDirectory(type);
|
||||
final dirSize = await getDirSizeInBytes(dir);
|
||||
if (!await dir.exists()) {
|
||||
return 0;
|
||||
}
|
||||
final streamedEntities = filesOnly
|
||||
? dir.list().whereType<File>().cast<FileSystemEntity>()
|
||||
: dir.list();
|
||||
|
||||
final entities = await streamedEntities.toList();
|
||||
await Future.wait([
|
||||
for (var entity in entities) entity.delete(recursive: !filesOnly),
|
||||
]);
|
||||
return dirSize;
|
||||
}
|
||||
|
||||
Future<List<File>> getAllFiles(Directory directory) {
|
||||
return directory.list().whereType<File>().toList();
|
||||
}
|
||||
|
||||
static Future<List<Directory>> getAllSubdirectories(Directory directory) {
|
||||
Future<List<Directory>> getAllSubdirectories(Directory directory) {
|
||||
return directory.list().whereType<Directory>().toList();
|
||||
}
|
||||
|
||||
static Future<int> getDirSizeInBytes(Directory dir) async {
|
||||
Future<int> getDirSizeInBytes(Directory dir) async {
|
||||
return dir
|
||||
.list(recursive: true)
|
||||
.fold(0, (previous, element) => previous + element.statSync().size);
|
||||
}
|
||||
|
||||
Future<void> _initTemporaryDirectory() async {
|
||||
_temporaryDirectory = await getTemporaryDirectory();
|
||||
}
|
||||
|
||||
Future<void> _initializeDocumentsDirectory() async {
|
||||
if (Platform.isAndroid) {
|
||||
final dirs =
|
||||
await getExternalStorageDirectories(type: StorageDirectory.documents);
|
||||
_documentsDirectory = dirs!.first;
|
||||
return;
|
||||
} else if (Platform.isIOS) {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
_documentsDirectory = await Directory(p.join(dir.path, 'documents'))
|
||||
.create(recursive: true);
|
||||
return;
|
||||
} else {
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initLogDirectory() async {
|
||||
if (Platform.isAndroid) {
|
||||
_logDirectory =
|
||||
await getExternalStorageDirectories(type: StorageDirectory.documents)
|
||||
.then((directory) async =>
|
||||
directory?.firstOrNull ??
|
||||
await getApplicationDocumentsDirectory())
|
||||
.then((directory) =>
|
||||
Directory('${directory.path}/logs').create(recursive: true));
|
||||
return;
|
||||
} else if (Platform.isIOS) {
|
||||
_logDirectory = await getApplicationDocumentsDirectory().then(
|
||||
(value) => Directory('${value.path}/logs').create(recursive: true));
|
||||
return;
|
||||
}
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
}
|
||||
|
||||
Future<void> _initDownloadsDirectory() async {
|
||||
if (Platform.isAndroid) {
|
||||
var directory = Directory('/storage/emulated/0/Download');
|
||||
if (!await directory.exists()) {
|
||||
final downloadsDir = await getExternalStorageDirectories(
|
||||
type: StorageDirectory.downloads,
|
||||
);
|
||||
directory = await downloadsDir!.first.create(recursive: true);
|
||||
return;
|
||||
}
|
||||
_downloadsDirectory = directory;
|
||||
} else if (Platform.isIOS) {
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final dir = Directory('${appDir.path}/downloads');
|
||||
_downloadsDirectory = await dir.create(recursive: true);
|
||||
return;
|
||||
} else {
|
||||
throw UnsupportedError("Platform not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initUploadDirectory() async {
|
||||
final dir = await getApplicationDocumentsDirectory()
|
||||
.then((dir) => Directory('${dir.path}/upload'));
|
||||
_uploadDirectory = await dir.create(recursive: true);
|
||||
}
|
||||
|
||||
Future<void> _initTemporaryScansDirectory() async {
|
||||
_temporaryScansDirectory =
|
||||
await Directory(p.join(_temporaryDirectory.path, 'scans'))
|
||||
.create(recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
enum PaperlessDirectoryType {
|
||||
@@ -168,5 +262,6 @@ enum PaperlessDirectoryType {
|
||||
temporary,
|
||||
scans,
|
||||
download,
|
||||
upload;
|
||||
upload,
|
||||
logs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user