nc-photos/app/lib/debug_util.dart

60 lines
1.5 KiB
Dart
Raw Normal View History

2023-08-17 18:36:51 +02:00
import 'dart:async';
import 'dart:convert';
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;
class LogCapturer {
factory LogCapturer() {
2021-09-15 08:58:06 +02:00
_inst ??= LogCapturer._();
return _inst!;
}
LogCapturer._();
/// Start capturing logs
void start() {
2023-08-17 18:36:51 +02:00
_subscription ??= LogStream().stream.listen(_logs.add);
}
/// Stop capturing and save the captured logs
Future<dynamic> stop() {
2023-08-17 18:36:51 +02:00
_subscription?.cancel();
_subscription = null;
final saver = platform.FileSaver();
2021-09-15 08:58:06 +02:00
final content = const Utf8Encoder().convert(_logs.join("\n"));
_logs.clear();
return saver.saveFile("nc-photos.log", content);
}
2023-08-17 18:36:51 +02:00
bool get isEnable => _subscription != null;
final _logs = <String>[];
2023-08-17 18:36:51 +02:00
StreamSubscription? _subscription;
static LogCapturer? _inst;
}
2021-09-04 14:35:04 +02:00
String logFilename(
String? filename, {
bool? shouldLogFileName,
}) {
if ((shouldLogFileName ?? isDevMode) || filename == null) {
2021-12-02 09:27:11 +01:00
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 "***";
}
}