Refine timestamps with milliseconds and ISO-8601 format (#129).

* Show milliseconds too in the timestamp (#114) and log file (#124)
* Change timestamp format to ISO-8601.
Co-authored-by: Attila Veghelyi <aveghelyi@dension.com>
Co-authored-by: Sylvain LAFRASSE <sly74fr@users.noreply.github.com>
This commit is contained in:
attila-v 2022-02-07 17:18:36 +01:00 committed by GitHub
parent 101e32749b
commit 65153c0d03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 20 deletions

View file

@ -22,27 +22,36 @@
#include "config.h" #include "config.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
#include "tio/error.h" #include "tio/error.h"
#include "tio/print.h" #include "tio/print.h"
// "YYYY-MM-DDThh:mm:ss.sss" (ISO-8601 format).
#define TIME_STRING_SIZE 24
char * current_time(void) char * current_time(void)
{ {
static char time_string[20]; static char time_string[TIME_STRING_SIZE];
time_t t;
struct tm *tmp; struct tm *tmp;
t = time(NULL); struct timeval tv;
tmp = localtime(&t); gettimeofday(&tv,NULL);
tmp = localtime(&tv.tv_sec);
if (tmp == NULL) if (tmp == NULL)
{ {
error_printf("Retrieving local time failed"); error_printf("Retrieving local time failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strftime(time_string, sizeof(time_string), "%H:%M:%S", tmp); size_t len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tmp);
if (len) {
len = snprintf(time_string + len, TIME_STRING_SIZE - len, ".%03ld", (long)tv.tv_usec / 1000);
}
return time_string; return (len >= 0) && (len < TIME_STRING_SIZE) ? time_string : NULL;
} }
void delay(long ms) void delay(long ms)

View file

@ -619,7 +619,7 @@ int tty_connect(void)
static char previous_char = 0; static char previous_char = 0;
static bool first = true; static bool first = true;
int status; int status;
time_t next_timestamp = 0; bool next_timestamp = false;
char* now = NULL; char* now = NULL;
/* Open tty device */ /* Open tty device */
@ -658,7 +658,7 @@ int tty_connect(void)
tainted = false; tainted = false;
if (option.timestamp) if (option.timestamp)
next_timestamp = time(NULL); next_timestamp = true;
/* Save current port settings */ /* Save current port settings */
if (tcgetattr(fd, &tio_old) < 0) if (tcgetattr(fd, &tio_old) < 0)
@ -715,6 +715,7 @@ int tty_connect(void)
if (next_timestamp && input_char != '\n' && input_char != '\r') if (next_timestamp && input_char != '\n' && input_char != '\r')
{ {
now = current_time(); now = current_time();
if (now) {
fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now); fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now);
if (option.log) if (option.log)
{ {
@ -727,7 +728,8 @@ int tty_connect(void)
log_write(']'); log_write(']');
log_write(' '); log_write(' ');
} }
next_timestamp = 0; next_timestamp = false;
}
} }
/* Map input character */ /* Map input character */
@ -736,7 +738,7 @@ int tty_connect(void)
print('\r'); print('\r');
print('\n'); print('\n');
if (option.timestamp) if (option.timestamp)
next_timestamp = time(NULL); next_timestamp = true;
} else } else
{ {
/* Print received tty character to stdout */ /* Print received tty character to stdout */
@ -751,7 +753,7 @@ int tty_connect(void)
tainted = true; tainted = true;
if (input_char == '\n' && option.timestamp) if (input_char == '\n' && option.timestamp)
next_timestamp = time(NULL); next_timestamp = true;
} else } else
{ {
/* Error reading - device is likely unplugged */ /* Error reading - device is likely unplugged */