Added timeouts to force-flush the log buffer

This commit is contained in:
Edward Wright 2021-05-10 21:22:02 -04:00
parent 32902034ed
commit 36e4916280

View File

@ -32,6 +32,7 @@ namespace SimplySerial
static string logFile = string.Empty;
static string logData = string.Empty;
static int bufferSize = 4096;
static DateTime lastFlush = DateTime.Now;
static void Main(string[] args)
@ -184,6 +185,12 @@ namespace SimplySerial
(logging == true) ? ($"Logfile : {logFile} (Mode = " + ((logMode == FileMode.Create) ? "OVERWRITE" : "APPEND") + ")\n" ) : ""
), flush: true);
lastFlush = DateTime.Now;
DateTime start = DateTime.Now;
TimeSpan timeSinceRX = new TimeSpan();
TimeSpan timeSinceFlush = new TimeSpan();
// this is the core functionality - loop while the serial port is open
while (serialPort.IsOpen)
{
@ -226,6 +233,22 @@ namespace SimplySerial
// write what was received to console
Output(received, force: true, newline: false);
start = DateTime.Now;
}
else
{
Thread.Sleep(1);
if (logging)
{
timeSinceRX = DateTime.Now - start;
timeSinceFlush = DateTime.Now - lastFlush;
if ((timeSinceRX.TotalSeconds >= 2) || (timeSinceFlush.TotalSeconds >= 10))
{
if (logData.Length > 0)
Output("", force: true, newline: false, flush: true);
start = DateTime.Now;
}
}
}
}
catch (Exception e)
@ -490,6 +513,7 @@ namespace SimplySerial
if (newline)
message += "\n";
if (message.Length > 0)
Console.Write(message);
if (logging)
@ -504,6 +528,7 @@ namespace SimplySerial
{
writer.Write(logData);
}
lastFlush = DateTime.Now;
}
catch
{