From 90e99a1c18705f654c61e3e96939b2e053baab1e Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Mon, 7 Oct 2013 20:58:23 +0300 Subject: [PATCH] Add timestamp to the output data. Proper timestamps are required in order to print time on the X axis of the output graphs. Signed-off-by: Timo Kokkonen --- main.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index 6cfdd87..388f63e 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,7 @@ static int read_log_line(int infd, char *buf, size_t bufsize, int *offset) static int read_data(int infd, int outfd) { struct epoll_event ev; + time_t start_time = 0, cur_time; int epoll_fd; int ret; char buf[256]; @@ -86,6 +88,9 @@ static int read_data(int infd, int outfd) } while (1) { + char str[320]; + int len; + ret = epoll_wait(epoll_fd, &ev, 1, -1); if (ret == 0) continue; @@ -95,6 +100,9 @@ static int read_data(int infd, int outfd) return -1; } + if (!start_time) + start_time = time(NULL); + ret = read_log_line(infd, buf, sizeof(buf), &offset); if (ret < 0) return ret; @@ -102,21 +110,21 @@ static int read_data(int infd, int outfd) if (ret == 0) continue; - if (outfd) { - char newline = '\n'; - ret = write(outfd, buf, strlen(buf)); - if (read < 0) { - pr_err("write: %m\n"); - break; - } - ret = write(outfd, &newline, 1); - if (read < 0) { - pr_err("write: %m\n"); - break; - } - } + cur_time = time(NULL); pr_info("%s\n", buf); + + if (!outfd) + continue; + + len = snprintf(str, sizeof(str), + "%ld;%s\n", cur_time - start_time, buf); + + ret = write(outfd, str, len); + if (read < 0) { + pr_err("write: %m\n"); + break; + } } return 0; -- 2.45.0