[LEDE-DEV] [PATCH ugps 2/7] Switch to ulog for logging and add few commandline params

Petr Štetiar ynezz at true.cz
Wed Jul 27 05:45:49 PDT 2016


Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 log.h  | 17 +++++++----------
 main.c | 46 ++++++++++++++++++++++++++++++++++++++++++----
 nmea.c | 20 ++++++++++----------
 3 files changed, 59 insertions(+), 24 deletions(-)

diff --git a/log.h b/log.h
index b8ae621..bf86f1e 100644
--- a/log.h
+++ b/log.h
@@ -15,18 +15,15 @@
 #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 29b341b..87d069b 100644
--- a/main.c
+++ b/main.c
@@ -27,9 +27,11 @@
 #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 438de9d..05c904d 100644
--- 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 {
-- 
1.9.1




More information about the Lede-dev mailing list