From 4c6d27617a28f83230786c62a52735a9a720f4ea Mon Sep 17 00:00:00 2001 From: Karol Lassak Date: Wed, 2 Oct 2024 18:49:25 +0200 Subject: [PATCH] Add timestamps before lines in super snoop (#18) Co-authored-by: root --- comm1.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/comm1.c b/comm1.c index 6e7e029..d8096a2 100644 --- a/comm1.c +++ b/comm1.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "config.h" #include "lint.h" @@ -207,6 +208,22 @@ write_gmcp(struct object *ob, char *data) } +void write_current_time_with_milliseconds(int fd) { + struct timeval tv; + gettimeofday(&tv, NULL); + + struct tm *tm_info; + char buffer[64]; + tm_info = localtime(&tv.tv_sec); + + // Format time with milliseconds + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info); + int len = snprintf(buffer + 19, sizeof(buffer) - 19, ".%03ld:", tv.tv_usec / 1000); + + // Write to the given file descriptor + write(fd, buffer, 19 + len); +} + /* * Will output to telnet on every newline to avoid overflow. */ @@ -228,8 +245,10 @@ write_socket(char *cp, struct object *ob) } #ifdef SUPER_SNOOP - if (ip->snoop_fd != -1) - (void)write(ip->snoop_fd, cp, strlen(cp)); + if (ip->snoop_fd != -1) { + write_current_time_with_milliseconds(ip->snoop_fd); + (void) write(ip->snoop_fd, cp, strlen(cp)); + } #endif if (ip->snoop_by != NULL) @@ -1054,6 +1073,7 @@ interactive_input(struct interactive *ip, char *cp) #ifdef SUPER_SNOOP if (ip->snoop_fd != -1) { + write_current_time_with_milliseconds(ip->snoop_fd); (void)write(ip->snoop_fd, cp, strlen(cp)); (void)write(ip->snoop_fd, "\n", 1); }