Files
TTSTDConnect/flutter_sender/lib/core/diagnostic_log.dart
T
tongtong 3cd72d70e1 feat(desktop): 支持粘贴配对并修复串流与设备切换
- 支持 Ctrl+V 粘贴配对文本和二维码图片,预览确认后识别校验
- 修复控制发送阻塞、TLS 读写争锁、设备切换沿用旧连接及串流误关窗
- 优化媒体加密上下文、UDP 接收缓冲和异步诊断日志
- 安装器按路径停止主控与信令进程,统一递增版本、打包和清理旧产物
- 发布 0.1.3+4,通过三端编译、20 项 Flutter 测试和 58 项 Rust 单元测试
2026-11-28 16:08:07 +08:00

72 lines
1.9 KiB
Dart

import 'dart:io';
class DiagnosticLog {
DiagnosticLog([this.fileName = 'ttstd_connect.log']);
final String fileName;
File? _file;
Future<void> _writes = Future<void>.value();
int _pendingCharacters = 0;
String get path => _file?.path ?? '';
void start() {
final directories = <Directory>[
File(Platform.resolvedExecutable).parent,
Directory.current,
Directory.systemTemp,
];
for (final directory in directories) {
try {
final candidate = File(
'${directory.path}${Platform.pathSeparator}$fileName',
);
candidate.writeAsStringSync(
'=== TTSTD Connect diagnostic log ===\n'
'started: ${DateTime.now().toIso8601String()}\n',
mode: FileMode.write,
flush: true,
);
_file = candidate;
return;
} on Object catch (_) {}
}
}
void write(String message) {
message =
message.replaceAll(RegExp(r'#token=[^\s;)]*'), '#token=[redacted]');
final file = _file;
if (file == null) return;
final line = '${DateTime.now().toIso8601String()} $message\n';
// Slow disks must not block input/painting or create an unbounded log queue.
if (_pendingCharacters + line.length > 256 * 1024) return;
_pendingCharacters += line.length;
_writes = _writes.then((_) async {
try {
if (await file.length() > 2 * 1024 * 1024) {
await file.writeAsString(
'=== log rotated: ${DateTime.now().toIso8601String()} ===\n',
mode: FileMode.write,
);
}
await file.writeAsString(
line,
mode: FileMode.append,
);
} on Object catch (_) {
// Logging failure must not interrupt the stream or poison later writes.
} finally {
_pendingCharacters -= line.length;
}
});
}
Future<void> flush() => _writes;
void dispose() {
write('=== stopped ===');
_file = null;
}
}