mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Consolidate timestamp implementation in one file
This commit is contained in:
parent
a85cfcf3ef
commit
ba2d49d2f7
10 changed files with 202 additions and 139 deletions
|
|
@ -43,6 +43,8 @@
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
|
#include "timestamp.h"
|
||||||
|
#include "alert.h"
|
||||||
|
|
||||||
static struct config_t *c;
|
static struct config_t *c;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
#include "timestamp.h"
|
||||||
|
|
||||||
static char error[2][1000];
|
static char error[2][1000];
|
||||||
static bool in_session = false;
|
static bool in_session = false;
|
||||||
|
|
@ -54,7 +55,7 @@ void error_printf_(const char *format, ...)
|
||||||
{
|
{
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
ansi_error_printf("[%s] %s", current_time(), line);
|
ansi_error_printf("[%s] %s", timestamp_current_time(), line);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ tio_sources = [
|
||||||
'socket.c',
|
'socket.c',
|
||||||
'setspeed.c',
|
'setspeed.c',
|
||||||
'rs485.c',
|
'rs485.c',
|
||||||
|
'timestamp.c',
|
||||||
'alert.c'
|
'alert.c'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
64
src/misc.c
64
src/misc.c
|
|
@ -25,75 +25,11 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "options.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)
|
void delay(long ms)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "tty.h"
|
#include "tty.h"
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
|
#include "timestamp.h"
|
||||||
#include "alert.h"
|
#include "alert.h"
|
||||||
|
|
||||||
enum opt_t
|
enum opt_t
|
||||||
|
|
@ -130,63 +131,6 @@ void print_help(char *argv[])
|
||||||
printf("See the man page for more details.\n");
|
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)
|
void line_pulse_duration_option_parse(const char *arg)
|
||||||
{
|
{
|
||||||
bool token_found = true;
|
bool token_found = true;
|
||||||
|
|
|
||||||
|
|
@ -26,20 +26,9 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include "timestamp.h"
|
||||||
#include "alert.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 */
|
/* Options */
|
||||||
struct option_t
|
struct option_t
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
#include "timestamp.h"
|
||||||
|
|
||||||
extern bool print_tainted;
|
extern bool print_tainted;
|
||||||
extern char ansi_format[];
|
extern char ansi_format[];
|
||||||
|
|
@ -69,9 +70,9 @@ extern char ansi_format[];
|
||||||
if (print_tainted) \
|
if (print_tainted) \
|
||||||
putchar('\n'); \
|
putchar('\n'); \
|
||||||
if (option.color < 0) \
|
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 \
|
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 (!option.mute) { \
|
||||||
if (print_tainted) \
|
if (print_tainted) \
|
||||||
putchar('\n'); \
|
putchar('\n'); \
|
||||||
ansi_printf("[%s] " format, current_time(), ## args); \
|
ansi_printf("[%s] " format, timestamp_current_time(), ## args); \
|
||||||
print_tainted = false; \
|
print_tainted = false; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +91,7 @@ extern char ansi_format[];
|
||||||
if (!option.mute) { \
|
if (!option.mute) { \
|
||||||
if (print_tainted) \
|
if (print_tainted) \
|
||||||
putchar('\n'); \
|
putchar('\n'); \
|
||||||
ansi_printf_raw("[%s] " format, current_time(), ## args); \
|
ansi_printf_raw("[%s] " format, timestamp_current_time(), ## args); \
|
||||||
print_tainted = false; \
|
print_tainted = false; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
|
||||||
152
src/timestamp.c
Normal file
152
src/timestamp.c
Normal file
|
|
@ -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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
36
src/timestamp.h
Normal file
36
src/timestamp.h
Normal file
|
|
@ -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);
|
||||||
|
|
@ -52,6 +52,7 @@
|
||||||
#include "setspeed.h"
|
#include "setspeed.h"
|
||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
#include "alert.h"
|
#include "alert.h"
|
||||||
|
#include "timestamp.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
|
|
@ -1188,7 +1189,7 @@ int tty_connect(void)
|
||||||
/* Print timestamp on new line if enabled */
|
/* Print timestamp on new line if enabled */
|
||||||
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
||||||
{
|
{
|
||||||
now = current_time();
|
now = timestamp_current_time();
|
||||||
if (now)
|
if (now)
|
||||||
{
|
{
|
||||||
ansi_printf_raw("[%s] ", now);
|
ansi_printf_raw("[%s] ", now);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue