diff --git a/README.md b/README.md index df16489..57416a0 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ `-logmode` instructs SimplySerial to either `APPEND` to an existing log file, or `OVERWRITE` an existing log file. In either case, if the specified log file does not exist, it will be created. If neither option is specified, `OVERWRITE` is assumed. (ex. `-logmode:APPEND`) + `-logtime` enable/disable prepending of timestamps before each logged line. Defaults to `off`. (ex. `-logtime:on`) + `-quiet` prevents any application messages (connection banner, error messages, etc.) from printing out to the console `-forcenewline` replaces carriage returns with linefeeds in received data. (ex. `-forcenewline:on`) diff --git a/SimplySerial/SimplySerial.cs b/SimplySerial/SimplySerial.cs index 5fa9388..994975f 100644 --- a/SimplySerial/SimplySerial.cs +++ b/SimplySerial/SimplySerial.cs @@ -55,6 +55,7 @@ class SimplySerial static int dataBits = 8; static StopBits stopBits = StopBits.One; static bool logging = false; + static bool logTime = false; static FileMode logMode = FileMode.Create; static string logFile = string.Empty; static string logData = string.Empty; @@ -352,7 +353,7 @@ static void Main(string[] args) port.board.make, port.board.model, (port.isCircuitPython) ? " (CircuitPython-capable)" : "", - (logging == true) ? ($"Logfile : {logFile} (Mode = " + ((logMode == FileMode.Create) ? "OVERWRITE" : "APPEND") + ")\n") : "", + (logging == true) ? ($"Logfile : {logFile} (Mode = " + ((logMode == FileMode.Create) ? "OVERWRITE" : "APPEND") + ", Timestamps = " + ((logTime) ? "on" : "off") + ")\n") : "", exitKey ), flush: true); ; @@ -711,6 +712,11 @@ static void ArgHandler_Log(string value) logFile = value; } + static void ArgHandler_LogTime(string value) + { + logTime = ArgProcessor_OnOff(value); + } + static void ArgHandler_LogMode(string value) { value = value.ToLower(); @@ -909,6 +915,7 @@ static void ProcessArguments(string[] args) CommandLineArguments.Add("exitkey", new CommandLineArgument("exitkey", handler: ArgHandler_ExitKey)); CommandLineArguments.Add("forcenewline", new CommandLineArgument("forcenewline", handler: ArgHandler_ForceNewLine)); CommandLineArguments.Add("logmode", new CommandLineArgument("logmode", handler: ArgHandler_LogMode)); + CommandLineArguments.Add("logtime", new CommandLineArgument("logtime", handler: ArgHandler_LogTime)); CommandLineArguments.Add("parity", new CommandLineArgument("parity", handler: ArgHandler_Parity)); CommandLineArguments.Add("txonenter", new CommandLineArgument("txonenter", handler: ArgHandler_TXOnEnter)); @@ -1009,7 +1016,21 @@ static void Output(string message, bool force = false, bool newline = true, bool if (logging) { - logData += message; + if (logTime && !String.IsNullOrEmpty(message)) + { + // Add timestamp if start of line + if (string.IsNullOrEmpty(logData) || logData[logData.Length - 1] == '\n') + logData += $"{DateTime.Now}: "; + // Add timestamp for intermediate lines + logData += message.Substring(0, message.Length - 1).Replace("\n", $"\n{DateTime.Now}: "); + // No timestamp, if last character is a newline + // Timestamp will than be added in next call + logData += message[logData.Length - 1]; + } + else + { + logData += message; + } if ((logData.Length >= bufferSize) || flush) { try @@ -1064,6 +1085,7 @@ static void ShowHelp() Console.WriteLine(" -echo:VAL ON | OFF enable or disable printing typed characters locally"); Console.WriteLine(" -log:LOGFILE Logs all output to the specified file."); Console.WriteLine(" -logmode:MODE APPEND | OVERWRITE, default is OVERWRITE"); + Console.WriteLine(" -logtime:VAL ON | OFF enable/disable prepending timestamps to every logged line, default is OFF"); Console.WriteLine(" -quiet:VAL ON | OFF when enabled, don't print any application messages/errors to console"); Console.WriteLine(" -forcenewline:VAL ON | OFF enable/disable forcing of linefeeds (newline) in place of carriage returns in received data."); Console.WriteLine(" -encoding:ENC UTF8 | ASCII | RAW");