nc-photos/app/lib/debug_util.dart

64 lines
1.5 KiB
Dart
Raw Normal View History

import 'dart:convert';
2021-09-04 14:35:04 +02:00
import 'package:flutter/foundation.dart';
import 'package:nc_photos/mobile/platform.dart'
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
import 'package:np_common/string_extension.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() {
_isEnable = true;
}
/// Stop capturing and save the captured logs
Future<dynamic> stop() {
_isEnable = false;
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);
}
void onLog(String log) {
if (_isEnable) {
_logs.add(log);
}
}
bool get isEnable => _isEnable;
final _logs = <String>[];
bool _isEnable = false;
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-12-02 09:27:11 +01:00
@visibleForTesting
bool shouldLogFileName = kDebugMode;