log static method

void log({
  1. required String tag,
  2. required String logMessage,
})

Log a logMessage to a log tag file

Implementation

static void log({
  /// Log tag (file name) where to store the log message
  required String tag,
  required String logMessage,
}) async {
  debugPrint(
    '[$tag] $logMessage',
  );

  var file = await _localFile(tag);
  final timestamp = DateTime.now().toIso8601String();

  file.writeAsString(
    '$timestamp: $logMessage\n',
    mode: FileMode.append,
  );

  int sizeInBytes = file.lengthSync();
  double sizeInMb = sizeInBytes / (1024 * 1024);
  if (sizeInMb > _maxFileSizeMb) {
    while (sizeInMb > _maxFileSizeMb / 2) {
      final fileContent = file.readAsLinesSync();
      final cleanedUpContent = fileContent.sublist(fileContent.length ~/ 10);
      file.writeAsStringSync(cleanedUpContent.join('\n'),
          mode: FileMode.write);

      file = await _localFile(tag);
      sizeInBytes = file.lengthSync();
      sizeInMb = sizeInBytes / (1024 * 1024);
    }
  }
}