mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Show milliseconds too in the timestamp
This commit is contained in:
parent
95389f4dde
commit
39144cdf79
2 changed files with 30 additions and 20 deletions
20
src/time.c
20
src/time.c
|
|
@ -22,27 +22,35 @@
|
|||
#include "config.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include "tio/error.h"
|
||||
#include "tio/print.h"
|
||||
|
||||
#define TIME_STRING_SIZE 20
|
||||
|
||||
char * current_time(void)
|
||||
{
|
||||
static char time_string[20];
|
||||
time_t t;
|
||||
static char time_string[TIME_STRING_SIZE];
|
||||
struct tm *tmp;
|
||||
|
||||
t = time(NULL);
|
||||
tmp = localtime(&t);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,NULL);
|
||||
|
||||
tmp = localtime(&tv.tv_sec);
|
||||
if (tmp == NULL)
|
||||
{
|
||||
error_printf("Retrieving local time failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
strftime(time_string, sizeof(time_string), "%H:%M:%S", tmp);
|
||||
size_t len = strftime(time_string, sizeof(time_string), "%H:%M:%S", tmp);
|
||||
if (len) {
|
||||
len = snprintf(time_string + len, TIME_STRING_SIZE - len, ".%03ld", tv.tv_usec / 1000);
|
||||
}
|
||||
|
||||
return time_string;
|
||||
return (len >= 0) && (len < TIME_STRING_SIZE) ? time_string : NULL;
|
||||
}
|
||||
|
||||
void delay(long ms)
|
||||
|
|
|
|||
30
src/tty.c
30
src/tty.c
|
|
@ -619,7 +619,7 @@ int tty_connect(void)
|
|||
static char previous_char = 0;
|
||||
static bool first = true;
|
||||
int status;
|
||||
time_t next_timestamp = 0;
|
||||
bool next_timestamp = false;
|
||||
char* now = NULL;
|
||||
|
||||
/* Open tty device */
|
||||
|
|
@ -658,7 +658,7 @@ int tty_connect(void)
|
|||
tainted = false;
|
||||
|
||||
if (option.timestamp)
|
||||
next_timestamp = time(NULL);
|
||||
next_timestamp = true;
|
||||
|
||||
/* Save current port settings */
|
||||
if (tcgetattr(fd, &tio_old) < 0)
|
||||
|
|
@ -715,19 +715,21 @@ int tty_connect(void)
|
|||
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
||||
{
|
||||
now = current_time();
|
||||
fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now);
|
||||
if (option.log)
|
||||
{
|
||||
log_write('[');
|
||||
while (*now != '\0')
|
||||
if (now) {
|
||||
fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now);
|
||||
if (option.log)
|
||||
{
|
||||
log_write(*now);
|
||||
++now;
|
||||
log_write('[');
|
||||
while (*now != '\0')
|
||||
{
|
||||
log_write(*now);
|
||||
++now;
|
||||
}
|
||||
log_write(']');
|
||||
log_write(' ');
|
||||
}
|
||||
log_write(']');
|
||||
log_write(' ');
|
||||
next_timestamp = false;
|
||||
}
|
||||
next_timestamp = 0;
|
||||
}
|
||||
|
||||
/* Map input character */
|
||||
|
|
@ -736,7 +738,7 @@ int tty_connect(void)
|
|||
print('\r');
|
||||
print('\n');
|
||||
if (option.timestamp)
|
||||
next_timestamp = time(NULL);
|
||||
next_timestamp = true;
|
||||
} else
|
||||
{
|
||||
/* Print received tty character to stdout */
|
||||
|
|
@ -751,7 +753,7 @@ int tty_connect(void)
|
|||
tainted = true;
|
||||
|
||||
if (input_char == '\n' && option.timestamp)
|
||||
next_timestamp = time(NULL);
|
||||
next_timestamp = true;
|
||||
} else
|
||||
{
|
||||
/* Error reading - device is likely unplugged */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue