Add '24hour-delta' timestamp option

When enabled this option will timestamp new lines with the time elapsed
since the line before.

This is a very useful feature to identify which events takes the most
time.
This commit is contained in:
Martin Lund 2022-07-05 16:05:35 +02:00
parent eecfa6485c
commit 732c0c3f89
4 changed files with 38 additions and 8 deletions

View file

@ -35,17 +35,17 @@
char *current_time(void)
{
static char time_string[TIME_STRING_SIZE_MAX];
static struct timeval tv_start;
static struct timeval tv, tv_now, tv_start, tv_previous;
static bool first = true;
struct tm *tm;
struct timeval tv;
size_t len;
gettimeofday(&tv, NULL);
// Get current time value
gettimeofday(&tv_now, NULL);
if (first)
{
tv_start = tv;
tv_start = tv_now;
first = false;
}
@ -55,19 +55,26 @@ char *current_time(void)
case TIMESTAMP_NONE:
case TIMESTAMP_24HOUR:
// "hh:mm:ss.sss" (24 hour format)
tm = localtime(&tv.tv_sec);
tm = localtime(&tv_now.tv_sec);
len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm);
break;
case TIMESTAMP_24HOUR_START:
// "hh:mm:ss.sss" (24 hour format relative to start time)
timersub(&tv, &tv_start, &tv);
timersub(&tv_now, &tv_start, &tv);
tv.tv_sec -= 3600; // Why is this needed??
tm = localtime(&tv.tv_sec);
len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm);
break;
case TIMESTAMP_24HOUR_DELTA:
// "hh:mm:ss.sss" (24 hour format relative to previous time stamp)
timersub(&tv_now, &tv_previous, &tv);
tv.tv_sec -= 3600; // Why is this needed??
tm = localtime(&tv.tv_sec);
len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tm);
break;
case TIMESTAMP_ISO8601:
// "YYYY-MM-DDThh:mm:ss.sss" (ISO-8601)
tm = localtime(&tv.tv_sec);
tm = localtime(&tv_now.tv_sec);
len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm);
break;
default:
@ -80,6 +87,9 @@ char *current_time(void)
len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000);
}
// Save previous time value for next run
tv_previous = tv_now;
return (len < TIME_STRING_SIZE_MAX) ? time_string : NULL;
}

View file

@ -117,6 +117,10 @@ const char* timestamp_token(enum timestamp_t timestamp)
return "24hour-start";
break;
case TIMESTAMP_24HOUR_DELTA:
return "24hour-delta";
break;
case TIMESTAMP_ISO8601:
return "iso8601";
break;
@ -133,10 +137,22 @@ enum timestamp_t timestamp_option_parse(const char *arg)
if (arg != NULL)
{
if (strcmp(arg, "24hour-start") == 0)
if (strcmp(arg, "none") == 0)
{
return TIMESTAMP_NONE;
}
else if (strcmp(arg, "24hour") == 0)
{
return TIMESTAMP_24HOUR;
}
else if (strcmp(arg, "24hour-start") == 0)
{
return TIMESTAMP_24HOUR_START;
}
else if (strcmp(arg, "24hour-delta") == 0)
{
return TIMESTAMP_24HOUR_DELTA;
}
else if (strcmp(arg, "iso8601") == 0)
{
return TIMESTAMP_ISO8601;

View file

@ -31,6 +31,7 @@ enum timestamp_t
TIMESTAMP_NONE,
TIMESTAMP_24HOUR,
TIMESTAMP_24HOUR_START,
TIMESTAMP_24HOUR_DELTA,
TIMESTAMP_ISO8601,
TIMESTAMP_END,
};

View file

@ -355,6 +355,9 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c
case TIMESTAMP_24HOUR_START:
tio_printf("Switched to 24hour-start timestamp mode");
break;
case TIMESTAMP_24HOUR_DELTA:
tio_printf("Switched to 24hour-delta timestamp mode");
break;
case TIMESTAMP_ISO8601:
tio_printf("Switched to iso8601 timestamp mode");
break;