]> git.itanic.dy.fi Git - log-plotter/commitdiff
Add timestamp to the output data.
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Mon, 7 Oct 2013 17:58:23 +0000 (20:58 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Mon, 7 Oct 2013 17:58:23 +0000 (20:58 +0300)
Proper timestamps are required in order to print time on the X axis of
the output graphs.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
main.c

diff --git a/main.c b/main.c
index 6cfdd879d35692e9c5ccbcfa615ecb3dd249dc74..388f63e0bfdba2a7eb3655182a284d9f17fc75a2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <string.h>
+#include <time.h>
 #include <sys/epoll.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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;