mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-03-03 12:16:47 +00:00
fix: logging issues
This commit is contained in:
parent
61a5352bb8
commit
58dd5bb122
4 changed files with 21 additions and 16 deletions
|
|
@ -189,8 +189,7 @@ class ApiService {
|
||||||
bool get isConnected => _channel != null && _channel!.closeCode != null;
|
bool get isConnected => _channel != null && _channel!.closeCode != null;
|
||||||
|
|
||||||
Future<void> _onDone() async {
|
Future<void> _onDone() async {
|
||||||
Log.info('websocket closed without error');
|
_reconnectionDelay = 3;
|
||||||
_reconnectionDelay = 60 * 2; // the server closed the connection...
|
|
||||||
await onClosed();
|
await onClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -408,7 +407,7 @@ class ApiService {
|
||||||
}
|
}
|
||||||
if (result.isError) {
|
if (result.isError) {
|
||||||
if (result.error != ErrorCode.AuthTokenNotValid) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ Future<(Uint8List, Uint8List?)?> tryToSendCompleteMessage({
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.info('Uploading $receiptId (Message to ${receipt.contactId})');
|
Log.info('Uploading $receiptId');
|
||||||
|
|
||||||
final message = pb.Message.fromBuffer(receipt.message)
|
final message = pb.Message.fromBuffer(receipt.message)
|
||||||
..receiptId = receiptId;
|
..receiptId = receiptId;
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,7 @@ Future<void> initFCMService() async {
|
||||||
@pragma('vm:entry-point')
|
@pragma('vm:entry-point')
|
||||||
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||||
initLogger();
|
initLogger();
|
||||||
Log.info('Handling a background message: ${message.messageId}');
|
// Log.info('Handling a background message: ${message.messageId}');
|
||||||
await handleRemoteMessage(message);
|
await handleRemoteMessage(message);
|
||||||
// make sure every thing run...
|
// make sure every thing run...
|
||||||
await Future.delayed(const Duration(milliseconds: 2000));
|
await Future.delayed(const Duration(milliseconds: 2000));
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import 'dart:io';
|
||||||
import 'package:clock/clock.dart';
|
import 'package:clock/clock.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:mutex/mutex.dart';
|
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||||
import 'package:twonly/globals.dart';
|
import 'package:twonly/globals.dart';
|
||||||
|
|
@ -66,8 +65,6 @@ class Log {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mutex writeToLogGuard = Mutex();
|
|
||||||
|
|
||||||
Future<String> loadLogFile() async {
|
Future<String> loadLogFile() async {
|
||||||
final directory = await getApplicationSupportDirectory();
|
final directory = await getApplicationSupportDirectory();
|
||||||
final logFile = File('${directory.path}/app.log');
|
final logFile = File('${directory.path}/app.log');
|
||||||
|
|
@ -95,14 +92,23 @@ Future<void> _writeLogToFile(LogRecord record) async {
|
||||||
logFile.createSync(recursive: true);
|
logFile.createSync(recursive: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the log message
|
|
||||||
final logMessage =
|
final logMessage =
|
||||||
'${clock.now().toString().split(".")[0]} ${record.level.name} [twonly] ${record.loggerName} > ${record.message}\n';
|
'${clock.now().toString().split(".")[0]} ${record.level.name} [twonly] ${record.loggerName} > ${record.message}\n';
|
||||||
|
|
||||||
await writeToLogGuard.protect(() async {
|
final raf = await logFile.open(mode: FileMode.writeOnlyAppend);
|
||||||
// Append the log message to the file
|
|
||||||
await logFile.writeAsString(logMessage, mode: FileMode.append);
|
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 {
|
Future<void> cleanLogFile() async {
|
||||||
|
|
@ -112,10 +118,10 @@ Future<void> cleanLogFile() async {
|
||||||
if (logFile.existsSync()) {
|
if (logFile.existsSync()) {
|
||||||
final lines = await logFile.readAsLines();
|
final lines = await logFile.readAsLines();
|
||||||
|
|
||||||
if (lines.length <= 5000) return;
|
if (lines.length <= 10000) return;
|
||||||
|
|
||||||
final removeCount = lines.length - 5000;
|
final removeCount = lines.length - 10000;
|
||||||
final remaining = lines.sublist(removeCount);
|
final remaining = lines.sublist(removeCount, lines.length);
|
||||||
|
|
||||||
final sink = logFile.openWrite()..writeAll(remaining, '\n');
|
final sink = logFile.openWrite()..writeAll(remaining, '\n');
|
||||||
await sink.close();
|
await sink.close();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue