2023-08-17 18:36:51 +02:00
|
|
|
import 'dart:async';
|
2021-09-10 13:46:15 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2021-09-04 14:35:04 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-09-10 13:46:15 +02:00
|
|
|
import 'package:nc_photos/mobile/platform.dart'
|
|
|
|
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
|
2023-08-17 18:36:51 +02:00
|
|
|
import 'package:np_log/np_log.dart';
|
2023-08-25 18:37:17 +02:00
|
|
|
import 'package:np_string/np_string.dart';
|
2022-01-31 10:47:37 +01:00
|
|
|
import 'package:path/path.dart' as path_lib;
|
2021-09-10 13:46:15 +02:00
|
|
|
|
|
|
|
class LogCapturer {
|
|
|
|
factory LogCapturer() {
|
2021-09-15 08:58:06 +02:00
|
|
|
_inst ??= LogCapturer._();
|
2021-09-10 13:46:15 +02:00
|
|
|
return _inst!;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogCapturer._();
|
|
|
|
|
|
|
|
/// Start capturing logs
|
|
|
|
void start() {
|
2023-08-17 18:36:51 +02:00
|
|
|
_subscription ??= LogStream().stream.listen(_logs.add);
|
2021-09-10 13:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop capturing and save the captured logs
|
|
|
|
Future<dynamic> stop() {
|
2023-08-17 18:36:51 +02:00
|
|
|
_subscription?.cancel();
|
|
|
|
_subscription = null;
|
2021-09-10 13:46:15 +02:00
|
|
|
final saver = platform.FileSaver();
|
2021-09-15 08:58:06 +02:00
|
|
|
final content = const Utf8Encoder().convert(_logs.join("\n"));
|
2021-09-10 13:46:15 +02:00
|
|
|
_logs.clear();
|
|
|
|
return saver.saveFile("nc-photos.log", content);
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:36:51 +02:00
|
|
|
bool get isEnable => _subscription != null;
|
2021-09-10 13:46:15 +02:00
|
|
|
|
|
|
|
final _logs = <String>[];
|
2023-08-17 18:36:51 +02:00
|
|
|
StreamSubscription? _subscription;
|
2021-09-10 13:46:15 +02:00
|
|
|
|
|
|
|
static LogCapturer? _inst;
|
|
|
|
}
|
2021-09-04 14:35:04 +02:00
|
|
|
|
2021-12-02 09:27:11 +01:00
|
|
|
String logFilename(String? filename) {
|
|
|
|
if (shouldLogFileName || filename == null) {
|
|
|
|
return "$filename";
|
|
|
|
}
|
|
|
|
try {
|
2022-01-31 10:47:37 +01:00
|
|
|
final basename = path_lib.basenameWithoutExtension(filename);
|
2021-12-02 09:27:11 +01:00
|
|
|
final displayName = basename.length <= 6
|
|
|
|
? basename
|
|
|
|
: "${basename.slice(0, 3)}***${basename.slice(-3)}";
|
2022-01-31 10:47:37 +01:00
|
|
|
return "${path_lib.dirname(filename) != "." ? "***/" : ""}"
|
2021-12-02 09:27:11 +01:00
|
|
|
"$displayName"
|
2022-01-31 10:47:37 +01:00
|
|
|
"${path_lib.extension(filename)}";
|
2021-12-02 09:27:11 +01:00
|
|
|
} catch (_) {
|
|
|
|
return "***";
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 12:46:06 +02:00
|
|
|
|
2021-12-02 09:27:11 +01:00
|
|
|
@visibleForTesting
|
|
|
|
bool shouldLogFileName = kDebugMode;
|