]> git.itanic.dy.fi Git - log-plotter/commitdiff
refactor data handling into data.c
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Tue, 8 Oct 2013 18:55:31 +0000 (21:55 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 9 Oct 2013 19:54:18 +0000 (22:54 +0300)
Let's keep main.c clean and make new file for data handling.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
Makefile
data.c [new file with mode: 0644]
data.h [new file with mode: 0644]
main.c

index 9ccc202bf760f9f802e4c6bd47fdc9c2703a1d33..bb7cfecf437e9e75476ee102f8d16a4aaaad81bb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC=gcc
 LD=ld
 CFLAGS=-Wall -O2 -g -fPIC
 
-LOG-PLOTTER_OBJS = baud.o main.o options.o debug.o
+LOG-PLOTTER_OBJS = baud.o main.o options.o debug.o data.o
 
 ALL_OBJS = $(LOG-PLOTTER_OBJS)
 ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
diff --git a/data.c b/data.c
new file mode 100644 (file)
index 0000000..ef11dfa
--- /dev/null
+++ b/data.c
@@ -0,0 +1,135 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <sys/epoll.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "debug.h"
+
+/**
+ * Read data from a slow device
+ *
+ * return 1 when a complete NULL terminated line has been read
+ *
+ * return 0 when a partial line has been read and appended to the
+ *              buffer at @offset
+ *
+ * return negative on error
+ */
+static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
+{
+       int ret;
+       int i;
+
+       ret = read(infd, buf + *offset, bufsize - *offset - 1);
+       if (ret < 0) {
+               pr_err("read: %m\n");
+               return -1;
+       }
+
+       if (ret == 0) {
+               pr_err("Read EOF, stopping\n");
+               return -1;
+       }
+       buf[*offset + ret] = 0;
+
+       for (i = 0; i < ret; i++) {
+               if (buf[i + *offset] == '\n' ||
+                   buf[i + *offset] == '\r') {
+                       /*
+                        * Got a complete line when there is a newline
+                        * at the end. Remove the newline and possible
+                        * other junk, such as '\r'
+                        */
+                       buf[i + *offset] = 0;
+                       *offset = 0;
+
+                       return 1;
+               }
+
+               /*
+                * Fixme! Nothing guarantees that there isn't actually
+                * more data (a part of a new log entry perhaps) after
+                * the newline. So in rare cases (we are prevented
+                * from reading the serial line in very long time) we
+                * might lose data from the stream..
+                */
+       }
+
+       *offset += ret;
+       return 0;
+}
+
+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];
+       int offset = 0;
+
+       epoll_fd = epoll_create(1);
+       if (epoll_fd < 0) {
+               pr_err("Failed to create epoll socket: %m\n");
+               return -1;
+       }
+
+       ev.events = EPOLLIN;
+       ev.data.fd = infd;
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
+               pr_err("epoll_ctl: %m\n");
+               return 1;
+       }
+
+       while (1) {
+               char str[320];
+               int len;
+
+               ret = epoll_wait(epoll_fd, &ev, 1, -1);
+               if (ret == 0)
+                       continue;
+
+               if (ret < 0) {
+                       pr_err("epoll: %m\n");
+                       return -1;
+               }
+
+               ret = read_log_line(infd, buf, sizeof(buf), &offset);
+               if (ret < 0)
+                       return ret;
+
+               if (ret == 0)
+                       continue;
+
+               if (strlen(buf) < 5) {
+                       pr_debug("discarding truncated log entry\n");
+                       offset = 0;
+                       continue;
+               }
+
+               if (!start_time)
+                       start_time = time(NULL);
+
+               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 (ret < 0) {
+                       pr_err("write: %m\n");
+                       break;
+               }
+       }
+
+       return 0;
+}
+
diff --git a/data.h b/data.h
new file mode 100644 (file)
index 0000000..fa3b055
--- /dev/null
+++ b/data.h
@@ -0,0 +1,6 @@
+#ifndef _DATA_H_
+#define _DATA_H_
+
+int read_data(int infd, int outfd);
+
+#endif
diff --git a/main.c b/main.c
index 4832e6a12063e85555091d4e37ee4a0d1b369246..51e52605dafc2590a9cd160da1f2d014e0516d98 100644 (file)
--- a/main.c
+++ b/main.c
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <string.h>
-#include <time.h>
-#include <sys/epoll.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 
 #include "options.h"
 #include "baud.h"
 #include "debug.h"
-
-/**
- * Read data from a slow device
- *
- * return 1 when a complete NULL terminated line has been read
- *
- * return 0 when a partial line has been read and appended to the
- *              buffer at @offset
- *
- * return negative on error
- */
-static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
-{
-       int ret;
-       int i;
-
-       ret = read(infd, buf + *offset, bufsize - *offset - 1);
-       if (ret < 0) {
-               pr_err("read: %m\n");
-               return -1;
-       }
-
-       if (ret == 0) {
-               pr_err("Read EOF, stopping\n");
-               return -1;
-       }
-       buf[*offset + ret] = 0;
-
-       for (i = 0; i < ret; i++) {
-               if (buf[i + *offset] == '\n' ||
-                   buf[i + *offset] == '\r') {
-                       /*
-                        * Got a complete line when there is a newline
-                        * at the end. Remove the newline and possible
-                        * other junk, such as '\r'
-                        */
-                       buf[i + *offset] = 0;
-                       *offset = 0;
-
-                       return 1;
-               }
-
-               /*
-                * Fixme! Nothing guarantees that there isn't actually
-                * more data (a part of a new log entry perhaps) after
-                * the newline. So in rare cases (we are prevented
-                * from reading the serial line in very long time) we
-                * might lose data from the stream..
-                */
-       }
-
-       *offset += ret;
-       return 0;
-}
-
-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];
-       int offset = 0;
-
-       epoll_fd = epoll_create(1);
-       if (epoll_fd < 0) {
-               pr_err("Failed to create epoll socket: %m\n");
-               return -1;
-       }
-
-       ev.events = EPOLLIN;
-       ev.data.fd = infd;
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
-               pr_err("epoll_ctl: %m\n");
-               return 1;
-       }
-
-       while (1) {
-               char str[320];
-               int len;
-
-               ret = epoll_wait(epoll_fd, &ev, 1, -1);
-               if (ret == 0)
-                       continue;
-
-               if (ret < 0) {
-                       pr_err("epoll: %m\n");
-                       return -1;
-               }
-
-               ret = read_log_line(infd, buf, sizeof(buf), &offset);
-               if (ret < 0)
-                       return ret;
-
-               if (ret == 0)
-                       continue;
-
-               if (strlen(buf) < 5) {
-                       pr_debug("discarding truncated log entry\n");
-                       offset = 0;
-                       continue;
-               }
-
-               if (!start_time)
-                       start_time = time(NULL);
-
-               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 (ret < 0) {
-                       pr_err("write: %m\n");
-                       break;
-               }
-       }
-
-       return 0;
-}
+#include "data.h"
 
 int main(int argc, char *argv[])
 {