writeToLog static method

Future<void> writeToLog(
  1. String logMsg, {
  2. required String from,
  3. required String to,
  4. required String logSuffix,
  5. String header = "",
})

Implementation

static Future<void> writeToLog(String logMsg,
    {required String from, required String to, required String logSuffix, String header = ""}) async {
  String today = DateTime.now().toString().substring(0, 10);
  final path = await _localPath;
  if(!Platform.isWindows) {
    File platformLog = File('$path/${logSuffix}_$today.txt');

    if (openedFiles.contains('$path/${logSuffix}_$today.txt')) {
      writeToLog(logMsg, from: from, to: to, logSuffix: logSuffix, header: header);
    } else {
      String logHeader =

          '$header\n${DateTime.now().toString().substring(0, 20)} (${DateTime.now().difference(
          lastLogs[logSuffix] ?? intiLogTime)}) '
          'Send'
          ' to: $to From'
          ' $from\n';
      String logFooter = "\n" + List.generate(150, (index) => "=").join() + "\n";
      lastLogs[logSuffix] = DateTime.now();
      String logText = logHeader + logMsg + logFooter;
      log(logText);
      openedFiles.add('$path/${logSuffix}_$today.txt');
      await platformLog.writeAsString(logText, mode: FileMode.append);
      openedFiles.remove('$path/${logSuffix}_$today.txt');
    }
  }else{
    String logHeader =
        '$header\n${DateTime.now().toString().substring(0, 20)} (${DateTime.now().difference(
        lastLogs[logSuffix] ?? intiLogTime)}) '
        'Send'
        ' to: $to From'
        ' $from\n';
    String logFooter = "\n" + List.generate(150, (index) => "=").join() + "\n";
    String logText = logHeader + logMsg + logFooter;
    log(logText);
  }
}