From 7fb2abe9cd969e9fa12be3c3a80498173c4d59c9 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 12 Sep 2019 11:12:42 +0200 Subject: [PATCH 1/2] emulation on host: option to add timestamp on console output --- tests/host/common/ArduinoMain.cpp | 33 +++++++++++++++++++------------ tests/host/common/MockUART.cpp | 26 ++++++++++++++++++++++-- tests/host/common/mock.h | 2 +- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 7fdf4b22f5..c99cf678df 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -50,6 +50,8 @@ bool restore_tty = false; bool mockdebug = false; int mock_port_shifter = MOCK_PORT_SHIFTER; +bool serial_timestamp = false; + #define STDIN STDIN_FILENO static struct termios initial_settings; @@ -129,24 +131,26 @@ void help (const char* argv0, int exitcode) " -b - blocking tty/mocked-uart (default: not blocking tty)\n" " -S - spiffs size in KBytes (default: %zd)\n" " -L - littlefs size in KBytes (default: %zd)\n" - " -v - mock verbose\n" - " (negative value will force mismatched size)\n" + "\t (spiffs, littlefs: negative value will force mismatched size)\n" + " -T - show timestamp on output\n" + " -v - verbose\n" , argv0, MOCK_PORT_SHIFTER, spiffs_kb, littlefs_kb); exit(exitcode); } static struct option options[] = { - { "help", no_argument, NULL, 'h' }, - { "fast", no_argument, NULL, 'f' }, - { "local", no_argument, NULL, 'l' }, - { "sigint", no_argument, NULL, 'c' }, - { "blockinguart", no_argument, NULL, 'b' }, - { "verbose", no_argument, NULL, 'v' }, - { "interface", required_argument, NULL, 'i' }, - { "spiffskb", required_argument, NULL, 'S' }, - { "littlefskb", required_argument, NULL, 'L' }, - { "portshifter", required_argument, NULL, 's' }, + { "help", no_argument, NULL, 'h' }, + { "fast", no_argument, NULL, 'f' }, + { "local", no_argument, NULL, 'l' }, + { "sigint", no_argument, NULL, 'c' }, + { "blockinguart", no_argument, NULL, 'b' }, + { "verbose", no_argument, NULL, 'v' }, + { "timestamp", no_argument, NULL, 'T' }, + { "interface", required_argument, NULL, 'i' }, + { "spiffskb", required_argument, NULL, 'S' }, + { "littlefskb", required_argument, NULL, 'L' }, + { "portshifter", required_argument, NULL, 's' }, }; void cleanup () @@ -182,7 +186,7 @@ int main (int argc, char* const argv []) for (;;) { - int n = getopt_long(argc, argv, "hlcfbvi:S:s:L:", options, NULL); + int n = getopt_long(argc, argv, "hlcfbvTi:S:s:L:", options, NULL); if (n < 0) break; switch (n) @@ -217,6 +221,9 @@ int main (int argc, char* const argv []) case 'v': mockdebug = true; break; + case 'T': + serial_timestamp = true; + break; default: help(argv[0], EXIT_FAILURE); } diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index 6c6a54ab35..15e9c921cb 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -29,6 +29,8 @@ */ #include // write +#include // gettimeofday +#include // localtime #include "Arduino.h" #include "uart.h" @@ -63,9 +65,29 @@ struct uart_ static void uart_do_write_char(const int uart_nr, char c) { + static bool w = false; + if (uart_nr >= UART0 && uart_nr <= UART1) - if (1 != write(uart_nr + 1, &c, 1)) - fprintf(stderr, "Unable to write character to emulated UART stream: %d\n", c); + { + if (serial_timestamp && (c == '\n' || c == '\r')) + { + if (w) + { + FILE* out = uart_nr == UART0? stdout: stderr; + timeval tv; + gettimeofday(&tv, nullptr); + const tm* tm = localtime(&tv.tv_sec); + fprintf(out, "\r\n%d:%02d:%02d.%06d: ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec); + fflush(out); + w = false; + } + } + else + { + write(uart_nr + 1, &c, 1); + w = true; + } + } } // write a new byte into the RX FIFO buffer diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index e616167374..a908b2aba4 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -85,7 +85,7 @@ int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); int mockverbose (const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); extern const char* host_interface; // cmdline parameter - +extern bool serial_timestamp; extern int mock_port_shifter; #define NO_GLOBAL_BINDING 0xffffffff From d7578086277cfafb16f0a00af4c6b45c35f0fada Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 12 Sep 2019 13:25:14 +0200 Subject: [PATCH 2/2] move variable declaration --- tests/host/common/ArduinoMain.cpp | 2 -- tests/host/common/MockUART.cpp | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index c99cf678df..565c7942d0 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -50,8 +50,6 @@ bool restore_tty = false; bool mockdebug = false; int mock_port_shifter = MOCK_PORT_SHIFTER; -bool serial_timestamp = false; - #define STDIN STDIN_FILENO static struct termios initial_settings; diff --git a/tests/host/common/MockUART.cpp b/tests/host/common/MockUART.cpp index 15e9c921cb..67af7ed2cc 100644 --- a/tests/host/common/MockUART.cpp +++ b/tests/host/common/MockUART.cpp @@ -61,6 +61,8 @@ struct uart_ struct uart_rx_buffer_ * rx_buffer; }; +bool serial_timestamp = false; + // write one byte to the emulated UART static void uart_do_write_char(const int uart_nr, char c)