Switch to ulog for logging and add few commandline params
authorPetr Štetiar <ynezz@true.cz>
Wed, 27 Jul 2016 12:45:49 +0000 (14:45 +0200)
committerJohn Crispin <john@phrozen.org>
Sun, 24 Jul 2016 04:12:26 +0000 (06:12 +0200)
Signed-off-by: Petr Štetiar <ynezz@true.cz>
log.h
main.c
nmea.c

diff --git a/log.h b/log.h
index b8ae62180475466d02fc60a9298abe040a2326b7..bf86f1efb908cd247ff6f4486553fc98112964d6 100644 (file)
--- a/log.h
+++ b/log.h
 #ifndef __LOG_H
 #define __LOG_H
 
-#include <stdio.h>
-#include <syslog.h>
+#include <libubox/ulog.h>
 
-#define LOG(fmt, ...) do { \
-               syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
-               fprintf(stderr, "ugps: "fmt, ## __VA_ARGS__); \
-       } while (0)
+#define DEBUG(level, fmt, ...) do { \
+       if (debug >= level) { \
+               ulog(LOG_DEBUG, fmt, ## __VA_ARGS__); \
+       } while (0)
 
-#define ERROR(fmt, ...) do { \
-               syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
-               fprintf(stderr, "ugps: "fmt, ## __VA_ARGS__); \
-       } while (0)
+#define LOG   ULOG_INFO
+#define ERROR ULOG_ERR
 
 extern unsigned int debug;
 
diff --git a/main.c b/main.c
index 29b341b2dcc696102a9cf2d3f765a7d38cdfee83..87d069be02112ecd1e1d75884bf9bbc713ce85a4 100644 (file)
--- a/main.c
+++ b/main.c
 #include "log.h"
 #include "nmea.h"
 
+unsigned int debug;
 static struct ustream_fd stream;
 static struct ubus_auto_conn conn;
 static struct blob_buf b;
+static char *ubus_socket;
 struct timespec stamp = { 0 };
 
 void
@@ -89,22 +91,58 @@ ubus_connect_handler(struct ubus_context *ctx)
 }
 
 static int
-usage(void)
+usage(const char *prog)
 {
-       LOG("ugps <device>\n");
+       fprintf(stderr, "Usage: %s [options] <device>\n"
+               "Options:\n"
+               "       -s <path>       Path to ubus socket\n"
+               "       -d <level>      Enable debug messages\n"
+               "       -S              Print messages to stdout\n"
+               "\n", prog);
        return -1;
 }
 
 int
 main(int argc, char ** argv)
 {
+       int ch;
+       char *device = NULL;
+       char *dbglvl = getenv("DBGLVL");
+       int ulog_channels = ULOG_KMSG;
 
        signal(SIGPIPE, SIG_IGN);
 
-       if (argc != 2)
-               return usage();
+       if (dbglvl) {
+               debug = atoi(dbglvl);
+               unsetenv("DBGLVL");
+       }
+
+       while ((ch = getopt(argc, argv, "d:D:s:S")) != -1) {
+               switch (ch) {
+               case 's':
+                       ubus_socket = optarg;
+                       break;
+               case 'd':
+                       debug = atoi(optarg);
+                       break;
+               case 'S':
+                       ulog_channels = ULOG_STDIO;
+                       break;
+               default:
+                       return usage(argv[0]);
+               }
+       }
+
+       if (argc - optind < 1) {
+               fprintf(stderr, "ERROR: missing device parameter\n");
+               return usage(argv[0]);
+       }
+
+       device = argv[optind];
+       ulog_open(ulog_channels, LOG_DAEMON, "ugps");
 
        uloop_init();
+       conn.path = ubus_socket;
        conn.cb = ubus_connect_handler;
        ubus_auto_connect(&conn);
        nmea_open(argv[1], &stream, B4800);
diff --git a/nmea.c b/nmea.c
index 438de9d7042c635d9ab756389b2aee3a0f1c1fb2..05c904d6426668a59cc3bb4a763779e7d89c4d32 100644 (file)
--- a/nmea.c
+++ b/nmea.c
@@ -60,7 +60,7 @@ nmea_txt_cb(void)
        if (nmea_params[3].num < 0 || nmea_params[3].num > 2)
                nmea_params[3].num = 0;
 
-       LOG("%s: %s\n", ids[nmea_params[3].num], nmea_params[4].str);
+       DEBUG(3, "%s: %s\n", ids[nmea_params[3].num], nmea_params[4].str);
 }
 
 static void
@@ -71,7 +71,7 @@ nmea_rmc_cb(void)
 
        if (*nmea_params[2].str != 'A') {
                gps_valid = 0;
-               fprintf(stderr, "waiting for valid signal\n");
+               DEBUG(4, "waiting for valid signal\n");
                return;
        }
 
@@ -89,7 +89,7 @@ nmea_rmc_cb(void)
                struct timeval cur;
 
                strftime(tmp, 256, "%D %02H:%02M:%02S", &tm);
-               LOG("date: %s UTC\n", tmp);
+               DEBUG(3, "date: %s UTC\n", tmp);
 
                tv.tv_sec -= timezone;
                if (daylight)
@@ -113,7 +113,7 @@ nmea_rmc_cb(void)
                int latd, latm, lats;
                int lngd, lngm, lngs;
                float flats, flngs;
-               LOG("position: %s, %s\n",
+               DEBUG(4, "position: %s, %s\n",
                        nmea_params[3].str, nmea_params[5].str);
                latm = atoi(&nmea_params[3].str[2]);
                nmea_params[3].str[2] = '\0';
@@ -139,14 +139,14 @@ nmea_rmc_cb(void)
 
 #define ms_to_deg(x, y) (((x * 10000) + y) / 60)
 
-               LOG("position: %d°%d.%04d, %d°%d.%04d\n",
+               DEBUG(4, "position: %d°%d.%04d, %d°%d.%04d\n",
                        latd, latm, lats, lngd, lngm, lngs);
-               LOG("position: %d°%d'%.1f\" %d°%d'%.1f\"\n",
+               DEBUG(4, "position: %d°%d'%.1f\" %d°%d'%.1f\"\n",
                        latd, latm, flats, lngd, lngm, flngs);
 
                snprintf(latitude, sizeof(latitude), "%d.%04d", latd, ms_to_deg(latm, lats));
                snprintf(longitude, sizeof(longitude), "%d.%04d", lngd, ms_to_deg(lngm, lngs));
-               LOG("position: %s %s\n", latitude, longitude);
+               DEBUG(3, "position: %s %s\n", latitude, longitude);
                gps_timestamp();
        }
 }
@@ -157,7 +157,7 @@ nmea_gga_cb(void)
        if (!gps_valid)
                return;
        strncpy(elivation, nmea_params[9].str, sizeof(elivation));
-       LOG("height: %s\n", elivation);
+       DEBUG(4, "height: %s\n", elivation);
 }
 
 static void
@@ -167,8 +167,8 @@ nmea_vtg_cb(void)
                return;
        strncpy(course, nmea_params[1].str, sizeof(course));
        strncpy(speed, nmea_params[6].str, sizeof(speed));
-       LOG("course: %s\n", course);
-       LOG("speed: %s\n", speed);
+       DEBUG(4, "course: %s\n", course);
+       DEBUG(4, "speed: %s\n", speed);
 }
 
 static struct nmea_msg {