Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
26 changes: 24 additions & 2 deletions SimplySerial/SimplySerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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); ;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down