fix: logging issues

This commit is contained in:
otsmr 2026-02-12 21:54:23 +01:00
parent 61a5352bb8
commit 58dd5bb122
4 changed files with 21 additions and 16 deletions

View file

@ -189,8 +189,7 @@ class ApiService {
bool get isConnected => _channel != null && _channel!.closeCode != null;
Future<void> _onDone() async {
Log.info('websocket closed without error');
_reconnectionDelay = 60 * 2; // the server closed the connection...
_reconnectionDelay = 3;
await onClosed();
}
@ -408,7 +407,7 @@ class ApiService {
}
if (result.isError) {
if (result.error != ErrorCode.AuthTokenNotValid) {
Log.error('got error while authenticating to the server', result);
Log.error('got error while authenticating to the server: $result');
return false;
}
}

View file

@ -89,7 +89,7 @@ Future<(Uint8List, Uint8List?)?> tryToSendCompleteMessage({
return null;
}
Log.info('Uploading $receiptId (Message to ${receipt.contactId})');
Log.info('Uploading $receiptId');
final message = pb.Message.fromBuffer(receipt.message)
..receiptId = receiptId;

View file

@ -109,7 +109,7 @@ Future<void> initFCMService() async {
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
initLogger();
Log.info('Handling a background message: ${message.messageId}');
// Log.info('Handling a background message: ${message.messageId}');
await handleRemoteMessage(message);
// make sure every thing run...
await Future.delayed(const Duration(milliseconds: 2000));

View file

@ -2,7 +2,6 @@ import 'dart:io';
import 'package:clock/clock.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:mutex/mutex.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:twonly/globals.dart';
@ -66,8 +65,6 @@ class Log {
}
}
Mutex writeToLogGuard = Mutex();
Future<String> loadLogFile() async {
final directory = await getApplicationSupportDirectory();
final logFile = File('${directory.path}/app.log');
@ -95,14 +92,23 @@ Future<void> _writeLogToFile(LogRecord record) async {
logFile.createSync(recursive: true);
}
// Prepare the log message
final logMessage =
'${clock.now().toString().split(".")[0]} ${record.level.name} [twonly] ${record.loggerName} > ${record.message}\n';
await writeToLogGuard.protect(() async {
// Append the log message to the file
await logFile.writeAsString(logMessage, mode: FileMode.append);
});
final raf = await logFile.open(mode: FileMode.writeOnlyAppend);
try {
// Use FileLock.blockingExclusive to wait until the lock is available
await raf.lock(FileLock.blockingExclusive);
await raf.writeString(logMessage);
await raf.flush();
} catch (e) {
// ignore: avoid_print
print('Error during file access: $e');
} finally {
await raf.unlock();
await raf.close();
}
}
Future<void> cleanLogFile() async {
@ -112,10 +118,10 @@ Future<void> cleanLogFile() async {
if (logFile.existsSync()) {
final lines = await logFile.readAsLines();
if (lines.length <= 5000) return;
if (lines.length <= 10000) return;
final removeCount = lines.length - 5000;
final remaining = lines.sublist(removeCount);
final removeCount = lines.length - 10000;
final remaining = lines.sublist(removeCount, lines.length);
final sink = logFile.openWrite()..writeAll(remaining, '\n');
await sink.close();