Queue logs to be sent to file

This commit is contained in:
Vishnu 2021-06-13 20:21:15 +05:30
parent a1aafad475
commit d3b7288edb

View file

@ -2,6 +2,7 @@ library super_logging;
import 'dart:async';
import 'dart:io';
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
@ -218,11 +219,12 @@ class SuperLogging {
// write to stdout
printLog(str);
// write to logfile
// push to log queue
if (fileIsEnabled) {
final strForLogFile = str + '\n';
await logFile.writeAsString(strForLogFile,
mode: FileMode.append, flush: true);
fileQueueEntries.add(str + '\n');
if (fileQueueEntries.length == 1) {
flushQueue();
}
}
// add error to sentry queue
@ -231,6 +233,22 @@ class SuperLogging {
}
}
static final Queue<String> fileQueueEntries = Queue();
static bool isFlushing = false;
static void flushQueue() async {
if (isFlushing) {
return;
}
isFlushing = true;
final entry = fileQueueEntries.removeFirst();
await logFile.writeAsString(entry, mode: FileMode.append, flush: true);
isFlushing = false;
if (fileQueueEntries.isNotEmpty) {
flushQueue();
}
}
// Logs on must be chunked or they get truncated otherwise
// See https://github.com/flutter/flutter/issues/22665
static var logChunkSize = 800;