add README
[trinked.git] / main.c
diff --git a/main.c b/main.c
index 7c48a37..f283be0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,6 +26,7 @@
  *-------------------------------------------------------------------------
  */
 
+#include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <hidapi/hidapi.h>
 #include <sys/param.h>
 
+#include <tlog.h>
 #include <trinket.h>
 
 static char*   dbdir = NULL;
 static int             period = 300; /* in seconds */
 static int             tics = 1;
 static char*   pidfile = NULL;
-static char*   logfile = NULL;
 static bool            daemonize = false;
-static bool            workaround = false;
 static char            tfile[MAXPATHLEN],
                                ofile[MAXPATHLEN];
 
@@ -53,7 +53,7 @@ static void
 usage(const char *errmsg) {
        puts("trinketd - collecting info from AtomPro");
        puts("Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>");
-       puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-i tics] [-w]");
+       puts("trinketd [-d] [-p pidfile] [-P period] [-D datadir] [-i tics] [-l logfile");
 
        if (errmsg) {
                puts("");
@@ -68,35 +68,31 @@ main_loop() {
        double  *prevDose,
                        curDose;
        int             i;
-       int             collected;
+       int             collected = 0;
 
        prevDose = malloc(sizeof(*prevDose) * (tics + 1));
        if (!prevDose) {
-               fprintf(stderr, "could not allocate array, exiting...\n");
+               tlog(TL_CRIT, "could not allocate array, exiting...");
                return;
        }
 
-restart:
-       collected = 0;
-
-       if (trinketOpen() != ERR_OK) {
-               fprintf(stderr, "trinketOpen fails\n");
-               sleep(1);
-               goto restart;
-       }
-
        while(42) {
 
-               if (workaround)
-                       trinketPing();
+               if (trinketOpen() != ERR_OK) {
+                       tlog(TL_ALARM, "trinketOpen fails");
+                       sleep(1);
+                       continue;
+               }
 
                if (trinketGetCumDose(&curDose) != ERR_OK) {
-                       fprintf(stderr, "trinketGetCumDose fails\n");
+                       tlog(TL_ALARM, "trinketGetCumDose fails");
                        trinketClose();
                        sleep(1);
-                       goto restart;
+                       continue;
                }
 
+               trinketClose();
+
                if (collected <= tics) {
                        prevDose[collected++] = curDose;
                } else {
@@ -114,7 +110,7 @@ restart:
 
                        if (daemonize) {
                                if ((fh = fopen(tfile, "w")) == NULL) {
-                                       fprintf(stderr, "fopen fails\n");
+                                       tlog(TL_ALARM, "fopen fails: %s", strerror(errno));
                                        successOpen = false;
                                }
                        }
@@ -135,8 +131,6 @@ restart:
 
                sleep(period);
        }
-
-       trinketClose();
 }
 
 
@@ -145,12 +139,12 @@ extern int opterr, optind;
 
 int
 main(int argn, char* argv[]) {
-       int i;
-       int     pidfd,
-               logfd;
+       int             i;
+       int             pidfd;
+       char    *logfile = NULL;
 
        opterr = 0;
-       while((i=getopt(argn,argv,"dD:i:l:p:P:wh")) != EOF) {
+       while((i=getopt(argn,argv,"dD:i:l:p:P:h")) != EOF) {
                switch(i) {
                        case 'd':
                                daemonize = true;
@@ -170,9 +164,6 @@ main(int argn, char* argv[]) {
                        case 'P':
                                period = atoi(optarg);
                                break;
-                       case 'w':
-                               workaround = true;
-                               break;
                        case 'h':
                        default:
                                usage(NULL);
@@ -200,35 +191,31 @@ main(int argn, char* argv[]) {
                        usage("could not write pidfile");
        }
 
-       if (logfile) {
-               logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0666);
-               if (logfd < 0)
-                       usage("could not write logfile");
-       }
-
        if (daemonize) {
+               opentlog((logfile == NULL) ? TL_OPEN_SYSLOG : TL_OPEN_FILE,
+                                TL_INFO, logfile);
+
                if (daemon(0, 0) == -1)
-                       usage("could not daemonize");
+                       tlog(TL_CRIT | TL_EXIT, "could not daemonize");
+       } else {
+               opentlog(TL_OPEN_STDERR | TL_OPEN_FILE, TL_INFO, logfile);
        }
 
+
+
        if (pidfile) {
                char pid[64];
 
                snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
                if (write(pidfd, pid, strlen(pid)) != strlen(pid))
-                       usage("could not write pid");
+                       tlog(TL_CRIT | TL_EXIT, "could not write pid");
                close(pidfd);
        }
 
-       if (logfile) {
-               dup2(logfd, fileno(stderr));
-               close(logfd);
-       }
+       tlog(TL_CRIT, "trinketd started, pid: %lld", (long long)getpid());
 
-       if (hid_init() < 0) {
-               fprintf(stderr, "hid_init fails\n");
-               exit(1);
-       }
+       if (hid_init() < 0)
+               tlog(TL_CRIT | TL_EXIT, "hid_init fails");
 
        if (dbdir) {
                snprintf(tfile, MAXPATHLEN, "%s/trinket.tmp", dbdir);