From 4c2f9d65c43160a030e64f97b2c57c211e2af909 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 6 Jan 2022 18:52:18 +0100 Subject: print logs linewise, with timestamp --- rvnit.c | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/rvnit.c b/rvnit.c index 729d831..0cd8d3d 100644 --- a/rvnit.c +++ b/rvnit.c @@ -309,30 +309,47 @@ logger_loop(void* ignored) if (!(fds[j].revents & POLLIN)) continue; + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + char timestamp[32]; + strftime(timestamp, sizeof timestamp, + "%Y-%m-%dT%H:%M:%S", gmtime(&now.tv_sec)); + char buf[4096]; ssize_t rd = read(fds[j].fd, buf, sizeof buf); if (rd < 0) { perror("read"); continue; } - if (fds[j].fd == selflogfd[0]) { - printf("%s[%d]: %.*s", - "rvnit", - (int)getpid(), - (int)rd, - buf); - continue; - } - for (int i = 0; i < MAX_SV; i++) { - if (services[i].logfd[0] == fds[j].fd) { - printf("%s[%d]: %.*s", - services[i].name, - services[i].display_pid, - (int)rd, - buf - ); - break; + + char *s = buf; + char *e = buf + rd; + while (s < e) { + char *eol = memchr(s, '\n', e - s); + if (!eol) + eol = e; + + const char *sv = ""; + long pid = -1; + + if (fds[j].fd == selflogfd[0]) { + sv = "rvnit"; + pid = getpid(); + } else { + for (int i = 0; i < MAX_SV; i++) { + if (services[i].logfd[0] == fds[j].fd) { + sv = services[i].name; + pid = services[i].display_pid; + break; + } + } } + + printf("%s.%05ld %s[%ld]: %.*s\n", + timestamp, now.tv_nsec / 10000, + sv, pid, (int)(eol - s), s); + + s = eol + 1; } } } -- cgit 1.4.1