From ba2d49d2f7cf35bd60a46bdbfd4f717aeb86e859 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Sat, 10 Sep 2022 23:29:23 +0200 Subject: [PATCH] Consolidate timestamp implementation in one file --- src/configfile.c | 2 + src/error.c | 3 +- src/meson.build | 1 + src/misc.c | 64 -------------------- src/options.c | 58 +----------------- src/options.h | 13 +--- src/print.h | 9 +-- src/timestamp.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++ src/timestamp.h | 36 +++++++++++ src/tty.c | 3 +- 10 files changed, 202 insertions(+), 139 deletions(-) create mode 100644 src/timestamp.c create mode 100644 src/timestamp.h diff --git a/src/configfile.c b/src/configfile.c index 5375e73..864380e 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -43,6 +43,8 @@ #include "error.h" #include "print.h" #include "rs485.h" +#include "timestamp.h" +#include "alert.h" static struct config_t *c; diff --git a/src/error.c b/src/error.c index 4e0142f..43da8dd 100644 --- a/src/error.c +++ b/src/error.c @@ -31,6 +31,7 @@ #include "options.h" #include "print.h" #include "error.h" +#include "timestamp.h" static char error[2][1000]; static bool in_session = false; @@ -54,7 +55,7 @@ void error_printf_(const char *format, ...) { putchar('\n'); } - ansi_error_printf("[%s] %s", current_time(), line); + ansi_error_printf("[%s] %s", timestamp_current_time(), line); } else { diff --git a/src/meson.build b/src/meson.build index f5ee7a3..0077733 100644 --- a/src/meson.build +++ b/src/meson.build @@ -16,6 +16,7 @@ tio_sources = [ 'socket.c', 'setspeed.c', 'rs485.c', + 'timestamp.c', 'alert.c' ] diff --git a/src/misc.c b/src/misc.c index 22be237..1983950 100644 --- a/src/misc.c +++ b/src/misc.c @@ -25,75 +25,11 @@ #include #include #include -#include #include #include "error.h" #include "print.h" #include "options.h" -#define TIME_STRING_SIZE_MAX 24 - -char *current_time(void) -{ - static char time_string[TIME_STRING_SIZE_MAX]; - static struct timeval tv, tv_now, tv_start, tv_previous; - static bool first = true; - struct tm *tm; - size_t len; - - // Get current time value - gettimeofday(&tv_now, NULL); - - if (first) - { - tv_start = tv_now; - first = false; - } - - // Add formatted timestap - switch (option.timestamp) - { - case TIMESTAMP_NONE: - case TIMESTAMP_24HOUR: - // "hh:mm:ss.sss" (24 hour format) - tv = tv_now; - tm = localtime(&tv.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_now, &tv_start, &tv); - tm = gmtime(&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); - tm = gmtime(&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) - tv = tv_now; - tm = localtime(&tv.tv_sec); - len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm); - break; - default: - return NULL; - } - - // Append milliseconds to all timestamps - if (len) - { - 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; -} - void delay(long ms) { struct timespec ts; diff --git a/src/options.c b/src/options.c index 633cca4..52fd00d 100644 --- a/src/options.c +++ b/src/options.c @@ -36,6 +36,7 @@ #include "print.h" #include "tty.h" #include "rs485.h" +#include "timestamp.h" #include "alert.h" enum opt_t @@ -130,63 +131,6 @@ void print_help(char *argv[]) printf("See the man page for more details.\n"); } -const char* timestamp_state_to_string(enum timestamp_t timestamp) -{ - switch (timestamp) - { - case TIMESTAMP_NONE: - return "disabled"; - break; - - case TIMESTAMP_24HOUR: - return "24hour"; - break; - - case TIMESTAMP_24HOUR_START: - return "24hour-start"; - break; - - case TIMESTAMP_24HOUR_DELTA: - return "24hour-delta"; - break; - - case TIMESTAMP_ISO8601: - return "iso8601"; - break; - - default: - return "unknown"; - break; - } -} - -enum timestamp_t timestamp_option_parse(const char *arg) -{ - enum timestamp_t timestamp = TIMESTAMP_24HOUR; // Default - - if (arg != NULL) - { - 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; - } - } - - return timestamp; -} - void line_pulse_duration_option_parse(const char *arg) { bool token_found = true; diff --git a/src/options.h b/src/options.h index 8ce6ec0..f7a28e3 100644 --- a/src/options.h +++ b/src/options.h @@ -26,20 +26,9 @@ #include #include #include +#include "timestamp.h" #include "alert.h" -enum timestamp_t -{ - TIMESTAMP_NONE, - TIMESTAMP_24HOUR, - TIMESTAMP_24HOUR_START, - TIMESTAMP_24HOUR_DELTA, - TIMESTAMP_ISO8601, - TIMESTAMP_END, -}; - -enum timestamp_t timestamp_option_parse(const char *arg); - /* Options */ struct option_t { diff --git a/src/print.h b/src/print.h index 84eef2b..e3f6193 100644 --- a/src/print.h +++ b/src/print.h @@ -26,6 +26,7 @@ #include "misc.h" #include "error.h" #include "options.h" +#include "timestamp.h" extern bool print_tainted; extern char ansi_format[]; @@ -69,9 +70,9 @@ extern char ansi_format[]; if (print_tainted) \ putchar('\n'); \ if (option.color < 0) \ - fprintf (stdout, "\r[%s] Warning: " format "\r\n", current_time(), ## args); \ + fprintf (stdout, "\r[%s] Warning: " format "\r\n", timestamp_current_time(), ## args); \ else \ - ansi_printf("[%s] Warning: " format, current_time(), ## args); \ + ansi_printf("[%s] Warning: " format, timestamp_current_time(), ## args); \ } \ } @@ -80,7 +81,7 @@ extern char ansi_format[]; if (!option.mute) { \ if (print_tainted) \ putchar('\n'); \ - ansi_printf("[%s] " format, current_time(), ## args); \ + ansi_printf("[%s] " format, timestamp_current_time(), ## args); \ print_tainted = false; \ } \ } @@ -90,7 +91,7 @@ extern char ansi_format[]; if (!option.mute) { \ if (print_tainted) \ putchar('\n'); \ - ansi_printf_raw("[%s] " format, current_time(), ## args); \ + ansi_printf_raw("[%s] " format, timestamp_current_time(), ## args); \ print_tainted = false; \ } \ } diff --git a/src/timestamp.c b/src/timestamp.c new file mode 100644 index 0000000..b8a2198 --- /dev/null +++ b/src/timestamp.c @@ -0,0 +1,152 @@ +/* + * tio - a simple serial terminal I/O tool + * + * Copyright (c) 2014-2022 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include "error.h" +#include "print.h" +#include "options.h" +#include "timestamp.h" + +#define TIME_STRING_SIZE_MAX 24 + +char *timestamp_current_time(void) +{ + static char time_string[TIME_STRING_SIZE_MAX]; + static struct timeval tv, tv_now, tv_start, tv_previous; + static bool first = true; + struct tm *tm; + size_t len; + + // Get current time value + gettimeofday(&tv_now, NULL); + + if (first) + { + tv_start = tv_now; + first = false; + } + + // Add formatted timestap + switch (option.timestamp) + { + case TIMESTAMP_NONE: + case TIMESTAMP_24HOUR: + // "hh:mm:ss.sss" (24 hour format) + tv = tv_now; + tm = localtime(&tv.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_now, &tv_start, &tv); + tm = gmtime(&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); + tm = gmtime(&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) + tv = tv_now; + tm = localtime(&tv.tv_sec); + len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm); + break; + default: + return NULL; + } + + // Append milliseconds to all timestamps + if (len) + { + 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; +} + +const char* timestamp_state_to_string(enum timestamp_t timestamp) +{ + switch (timestamp) + { + case TIMESTAMP_NONE: + return "disabled"; + break; + + case TIMESTAMP_24HOUR: + return "24hour"; + break; + + case TIMESTAMP_24HOUR_START: + return "24hour-start"; + break; + + case TIMESTAMP_24HOUR_DELTA: + return "24hour-delta"; + break; + + case TIMESTAMP_ISO8601: + return "iso8601"; + break; + + default: + return "unknown"; + break; + } +} + +enum timestamp_t timestamp_option_parse(const char *arg) +{ + enum timestamp_t timestamp = TIMESTAMP_24HOUR; // Default + + if (arg != NULL) + { + 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; + } + } + + return timestamp; +} diff --git a/src/timestamp.h b/src/timestamp.h new file mode 100644 index 0000000..ee23ce6 --- /dev/null +++ b/src/timestamp.h @@ -0,0 +1,36 @@ +/* + * tio - a simple serial terminal I/O tool + * + * Copyright (c) 2014-2022 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#pragma once + +enum timestamp_t +{ + TIMESTAMP_NONE, + TIMESTAMP_24HOUR, + TIMESTAMP_24HOUR_START, + TIMESTAMP_24HOUR_DELTA, + TIMESTAMP_ISO8601, + TIMESTAMP_END, +}; + +char *timestamp_current_time(void); +const char* timestamp_state_to_string(enum timestamp_t timestamp); +enum timestamp_t timestamp_option_parse(const char *arg); diff --git a/src/tty.c b/src/tty.c index 004e52a..d2cc5a9 100644 --- a/src/tty.c +++ b/src/tty.c @@ -52,6 +52,7 @@ #include "setspeed.h" #include "rs485.h" #include "alert.h" +#include "timestamp.h" #include "misc.h" #ifdef __APPLE__ @@ -1188,7 +1189,7 @@ int tty_connect(void) /* Print timestamp on new line if enabled */ if (next_timestamp && input_char != '\n' && input_char != '\r') { - now = current_time(); + now = timestamp_current_time(); if (now) { ansi_printf_raw("[%s] ", now);