Skip to content

Commit 0474eb9

Browse files
authored
emulation on host: show timestamp on console output (#6507)
* emulation on host: option to add timestamp on console output
1 parent d8531cb commit 0474eb9

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

tests/host/common/ArduinoMain.cpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,26 @@ void help (const char* argv0, int exitcode)
129129
" -b - blocking tty/mocked-uart (default: not blocking tty)\n"
130130
" -S - spiffs size in KBytes (default: %zd)\n"
131131
" -L - littlefs size in KBytes (default: %zd)\n"
132-
" -v - mock verbose\n"
133-
" (negative value will force mismatched size)\n"
132+
"\t (spiffs, littlefs: negative value will force mismatched size)\n"
133+
" -T - show timestamp on output\n"
134+
" -v - verbose\n"
134135
, argv0, MOCK_PORT_SHIFTER, spiffs_kb, littlefs_kb);
135136
exit(exitcode);
136137
}
137138

138139
static struct option options[] =
139140
{
140-
{ "help", no_argument, NULL, 'h' },
141-
{ "fast", no_argument, NULL, 'f' },
142-
{ "local", no_argument, NULL, 'l' },
143-
{ "sigint", no_argument, NULL, 'c' },
144-
{ "blockinguart", no_argument, NULL, 'b' },
145-
{ "verbose", no_argument, NULL, 'v' },
146-
{ "interface", required_argument, NULL, 'i' },
147-
{ "spiffskb", required_argument, NULL, 'S' },
148-
{ "littlefskb", required_argument, NULL, 'L' },
149-
{ "portshifter", required_argument, NULL, 's' },
141+
{ "help", no_argument, NULL, 'h' },
142+
{ "fast", no_argument, NULL, 'f' },
143+
{ "local", no_argument, NULL, 'l' },
144+
{ "sigint", no_argument, NULL, 'c' },
145+
{ "blockinguart", no_argument, NULL, 'b' },
146+
{ "verbose", no_argument, NULL, 'v' },
147+
{ "timestamp", no_argument, NULL, 'T' },
148+
{ "interface", required_argument, NULL, 'i' },
149+
{ "spiffskb", required_argument, NULL, 'S' },
150+
{ "littlefskb", required_argument, NULL, 'L' },
151+
{ "portshifter", required_argument, NULL, 's' },
150152
};
151153

152154
void cleanup ()
@@ -182,7 +184,7 @@ int main (int argc, char* const argv [])
182184

183185
for (;;)
184186
{
185-
int n = getopt_long(argc, argv, "hlcfbvi:S:s:L:", options, NULL);
187+
int n = getopt_long(argc, argv, "hlcfbvTi:S:s:L:", options, NULL);
186188
if (n < 0)
187189
break;
188190
switch (n)
@@ -217,6 +219,9 @@ int main (int argc, char* const argv [])
217219
case 'v':
218220
mockdebug = true;
219221
break;
222+
case 'T':
223+
serial_timestamp = true;
224+
break;
220225
default:
221226
help(argv[0], EXIT_FAILURE);
222227
}

tests/host/common/MockUART.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030

3131
#include <unistd.h> // write
32+
#include <sys/time.h> // gettimeofday
33+
#include <time.h> // localtime
3234

3335
#include "Arduino.h"
3436
#include "uart.h"
@@ -59,13 +61,35 @@ struct uart_
5961
struct uart_rx_buffer_ * rx_buffer;
6062
};
6163

64+
bool serial_timestamp = false;
65+
6266
// write one byte to the emulated UART
6367
static void
6468
uart_do_write_char(const int uart_nr, char c)
6569
{
70+
static bool w = false;
71+
6672
if (uart_nr >= UART0 && uart_nr <= UART1)
67-
if (1 != write(uart_nr + 1, &c, 1))
68-
fprintf(stderr, "Unable to write character to emulated UART stream: %d\n", c);
73+
{
74+
if (serial_timestamp && (c == '\n' || c == '\r'))
75+
{
76+
if (w)
77+
{
78+
FILE* out = uart_nr == UART0? stdout: stderr;
79+
timeval tv;
80+
gettimeofday(&tv, nullptr);
81+
const tm* tm = localtime(&tv.tv_sec);
82+
fprintf(out, "\r\n%d:%02d:%02d.%06d: ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec);
83+
fflush(out);
84+
w = false;
85+
}
86+
}
87+
else
88+
{
89+
write(uart_nr + 1, &c, 1);
90+
w = true;
91+
}
92+
}
6993
}
7094

7195
// write a new byte into the RX FIFO buffer

tests/host/common/mock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
8585
int mockverbose (const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
8686

8787
extern const char* host_interface; // cmdline parameter
88-
88+
extern bool serial_timestamp;
8989
extern int mock_port_shifter;
9090

9191
#define NO_GLOBAL_BINDING 0xffffffff

0 commit comments

Comments
 (0)