exclusive lock for logging on the same process
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run

This commit is contained in:
otsmr 2026-03-02 22:53:55 +01:00
parent fb1e286cf9
commit 6df40e93df
2 changed files with 25 additions and 18 deletions

View file

@ -2,11 +2,11 @@
## 0.0.96 ## 0.0.96
Feature: Show link in chat if the saved media file contains one - Feature: Show link in chat if the saved media file contains one
Improve: Verification badge for groups - Improve: Verification badge for groups
Improve: Huge reduction in app size - Improve: Huge reduction in app size
Fix: Crash on older devices when compressing a video - Fix: Crash on older devices when compressing a video
Fix: Problem with decrypting messages fixed - Fix: Problem with decrypting messages fixed
## 0.0.93 ## 0.0.93

View file

@ -2,6 +2,7 @@ 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';
@ -85,6 +86,8 @@ Future<String> readLast1000Lines() async {
return all.sublist(start).join('\n'); return all.sublist(start).join('\n');
} }
Mutex sameProcessProtection = Mutex();
Future<void> _writeLogToFile(LogRecord record) async { Future<void> _writeLogToFile(LogRecord record) async {
final directory = await getApplicationSupportDirectory(); final directory = await getApplicationSupportDirectory();
final logFile = File('${directory.path}/app.log'); final logFile = File('${directory.path}/app.log');
@ -95,20 +98,24 @@ Future<void> _writeLogToFile(LogRecord record) async {
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';
final raf = await logFile.open(mode: FileMode.writeOnlyAppend); // > Note that this does not actually lock the file for access. Also note that advisory locks are on a process level.
// > This means that several isolates in the same process can obtain an exclusive lock on the same file.
return sameProcessProtection.protect(() async {
final raf = await logFile.open(mode: FileMode.writeOnlyAppend);
try { try {
// Use FileLock.blockingExclusive to wait until the lock is available // Use FileLock.blockingExclusive to wait until the lock is available
await raf.lock(FileLock.blockingExclusive); await raf.lock(FileLock.blockingExclusive);
await raf.writeString(logMessage); await raf.writeString(logMessage);
await raf.flush(); await raf.flush();
} catch (e) { } catch (e) {
// ignore: avoid_print // ignore: avoid_print
print('Error during file access: $e'); print('Error during file access: $e');
} finally { } finally {
await raf.unlock(); await raf.unlock();
await raf.close(); await raf.close();
} }
});
} }
Future<void> cleanLogFile() async { Future<void> cleanLogFile() async {