diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-01-06 18:52:18 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-01-06 18:52:18 +0100 |
commit | 4c2f9d65c43160a030e64f97b2c57c211e2af909 (patch) | |
tree | 654a7cf45abc358ca25993864e95fb5670c817ca | |
parent | 34fc75f992fbe1620f01e924ff5a8a4fe64de416 (diff) | |
download | rvnit-4c2f9d65c43160a030e64f97b2c57c211e2af909.tar.gz rvnit-4c2f9d65c43160a030e64f97b2c57c211e2af909.tar.xz rvnit-4c2f9d65c43160a030e64f97b2c57c211e2af909.zip |
print logs linewise, with timestamp
-rw-r--r-- | rvnit.c | 51 |
1 files 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 = "<unknown>"; + 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; } } } |