From 7b0feb181260ff4e005547a3ae870ebc545b1e85 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 27 Aug 2014 20:15:46 -0400 Subject: Reverse linkage of gpsd_report() is abolished. All regression tests pass. Some cleanup and testing is still required. --- gpsctl.c | 13 ++--------- gpsd.c | 13 +---------- gpsd.h-tail | 7 +++--- gpsdecode.c | 17 +++++--------- gpsmon.c | 71 +++++++++++++++++----------------------------------------- gpspacket.c | 26 --------------------- libgpsd_core.c | 51 +++++++++++++++++------------------------ test_packet.c | 17 -------------- 8 files changed, 53 insertions(+), 162 deletions(-) diff --git a/gpsctl.c b/gpsctl.c index a8edf599..08ad274e 100644 --- a/gpsctl.c +++ b/gpsctl.c @@ -36,17 +36,6 @@ static bool hunting = true; */ #define REDIRECT_SNIFF 15 -void gpsd_report(const int debuglevel, const int errlevel, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - gpsd_labeled_report(debuglevel, errlevel, "gpsctl:", fmt, ap); - va_end(ap); - -} - static void settle(struct gps_device_t *session) /* allow the device to settle after a control operation */ { @@ -315,6 +304,8 @@ int main(int argc, char **argv) } } + context.errout.label = "gpsctl"; + if (optind < argc) device = argv[optind]; diff --git a/gpsd.c b/gpsd.c index 69cdcae8..165a137c 100644 --- a/gpsd.c +++ b/gpsd.c @@ -165,17 +165,6 @@ static void onsig(int sig) signalled = (sig_atomic_t) sig; } -void gpsd_report(const int debuglevel, const int errlevel, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - gpsd_labeled_report(debuglevel, errlevel, "gpsd:", fmt, ap); - va_end(ap); - -} - static void typelist(void) /* list installed drivers and enabled features */ { @@ -1823,8 +1812,8 @@ int main(int argc, char *argv[]) bool go_background = true; volatile bool in_restart; - context.errout.debug = 0; gps_context_init(&context); + context.errout.label = "gpsd"; #ifdef CONTROL_SOCKET_ENABLE INVALIDATE_SOCKET(csock); diff --git a/gpsd.h-tail b/gpsd.h-tail index 718e5ba4..b76a7145 100644 --- a/gpsd.h-tail +++ b/gpsd.h-tail @@ -139,6 +139,7 @@ enum isgpsstat_t { struct errout_t { int debug; /* lexer debug level */ void (*report)(const char *); /* reporting hook for lexer errors */ + char *label; }; struct gps_lexer_t { @@ -905,8 +906,6 @@ extern void pps_thread_deactivate(struct gps_device_t *); extern int pps_thread_lastpps(struct gps_device_t *, struct timedrift_t *); extern void errout_reset(struct errout_t *errout); -extern void gpsd_notify(const struct errout_t *, - const int, const char *, ...); extern void gpsd_acquire_reporting_lock(void); extern void gpsd_release_reporting_lock(void); @@ -1004,9 +1003,9 @@ extern void libgps_dump_state(struct gps_data_t *); void gpsd_labeled_report(const int, const int, const char *, const char *, va_list); # if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) -__attribute__((__format__(__printf__, 3, 4))) void gpsd_report(const int, const int, const char *, ...); +__attribute__((__format__(__printf__, 3, 4))) void gpsd_notify(const struct errout_t *, const int, const char *, ...); # else /* not a new enough GCC, use the unprotected prototype */ -void gpsd_report(const int, const int, const char *, ...); +void gpsd_notify(const struct *errout_t, const int, const char *, ...); #endif #ifdef S_SPLINT_S diff --git a/gpsdecode.c b/gpsdecode.c index 62ebaa77..68e4c68b 100644 --- a/gpsdecode.c +++ b/gpsdecode.c @@ -27,17 +27,6 @@ static unsigned int typelist[32]; * **************************************************************************/ -void gpsd_report(const int debuglevel, const int errlevel, - const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - gpsd_labeled_report(debuglevel, errlevel, "gpsdecode:", fmt, ap); - va_end(ap); - -} - #ifdef AIVDM_ENABLE static void aivdm_csv_dump(struct ais_t *ais, char *buf, size_t buflen) { @@ -537,6 +526,7 @@ static void decode(FILE *fpin, FILE*fpout) gps_context_init(&context); gpsd_time_init(&context, time(NULL)); context.readonly = true; + context.errout.label = "gpsdecode"; gpsd_init(&session, &context, NULL); gpsd_clear(&session); session.gpsdata.gps_fd = fileno(fpin); @@ -598,9 +588,14 @@ static void encode(FILE *fpin, FILE *fpout) struct policy_t policy; struct gps_device_t session; int lineno = 0; + struct gps_context_t context; memset(&policy, '\0', sizeof(policy)); memset(&session, '\0', sizeof(session)); + memset(&context, '\0', sizeof(context)); + session.context = &context; + context.errout.debug = 0; + context.errout.label = "gpsdecode"; (void)strlcpy(session.gpsdata.dev.path, "stdin", sizeof(session.gpsdata.dev.path)); diff --git a/gpsmon.c b/gpsmon.c index fd74beae..fb597ebc 100644 --- a/gpsmon.c +++ b/gpsmon.c @@ -249,6 +249,24 @@ static void monitor_dump_send(/*@in@*/ const char *buf, size_t len) } #endif /* defined(CONTROLSEND_ENABLE) || defined(RECONFIGURE_ENABLE) */ +/*@-compdef@*/ +static void report_hook(const char *buf) +{ + char buf2[BUFSIZ]; + + visibilize(buf2, sizeof(buf2), buf); + + report_lock(); + if (!curses_active) + (void)fputs(buf2, stdout); + else if (packetwin != NULL) + (void)waddstr(packetwin, buf2); + if (logfile != NULL) + (void)fputs(buf2, logfile); + report_unlock(); +} +/*@+compdef@*/ + /*@-compdef@*/ static void packet_vlog(/*@out@*/char *buf, size_t len, const char *fmt, va_list ap) { @@ -588,57 +606,6 @@ static void packet_log(const char *fmt, ...) } #endif /* PPS_ENABLE */ -void gpsd_report(const int debuglevel, const int errlevel, const char *fmt, ...) -/* our version of the logger */ -{ - char buf[BUFSIZ]; - char *err_str; - - switch ( errlevel ) { - case LOG_ERROR: - err_str = "ERROR: "; - break; - case LOG_SHOUT: - err_str = "SHOUT: "; - break; - case LOG_WARN: - err_str = "WARN: "; - break; - case LOG_CLIENT: - err_str = "CLIENT: "; - break; - case LOG_INF: - err_str = "INFO: "; - break; - case LOG_PROG: - err_str = "PROG: "; - break; - case LOG_IO: - err_str = "IO: "; - break; - case LOG_DATA: - err_str = "DATA: "; - break; - case LOG_SPIN: - err_str = "SPIN: "; - break; - case LOG_RAW: - err_str = "RAW: "; - break; - default: - err_str = "UNK: "; - } - - (void)strlcpy(buf, "gpsmon:", BUFSIZ); - (void)strncat(buf, err_str, BUFSIZ - strlen(buf)); - if (errlevel <= debuglevel) { - va_list ap; - va_start(ap, fmt); - packet_vlog(buf, sizeof(buf), fmt, ap); - va_end(ap); - } -} - static ssize_t gpsmon_serial_write(struct gps_device_t *session, const char *buf, const size_t len) @@ -1100,6 +1067,8 @@ int main(int argc, char **argv) /*@ +observertrans @*/ gps_context_init(&context); // initialize the report mutex context.serial_write = gpsmon_serial_write; + context.errout.label = "gpsmon"; + context.errout.report = report_hook; while ((option = getopt(argc, argv, "aD:LVhl:nt:?")) != -1) { switch (option) { case 'a': diff --git a/gpspacket.c b/gpspacket.c index 9d89ec20..4410e4fa 100644 --- a/gpspacket.c +++ b/gpspacket.c @@ -14,32 +14,6 @@ static PyObject *ErrorObject = NULL; static PyObject *report_callback = NULL; -void gpsd_report(int unused UNUSED, int errlevel, const char *fmt, ... ) -{ - char buf[BUFSIZ]; - PyObject *args; - va_list ap; - - if (!report_callback) /* no callback defined, exit early */ - return; - - if (!PyCallable_Check(report_callback)) { - PyErr_SetString(ErrorObject, "Cannot call Python callback function"); - return; - } - - va_start(ap, fmt); - (void)vsnprintf(buf, sizeof(buf), fmt, ap); - va_end(ap); - - args = Py_BuildValue("(is)", errlevel, buf); - if (!args) - return; - - PyObject_Call(report_callback, args, NULL); - Py_DECREF(args); -} - static void basic_report(const char *buf) { (void)fputs(buf, stderr); diff --git a/libgpsd_core.c b/libgpsd_core.c index 9e5bf8e0..52a3aadb 100644 --- a/libgpsd_core.c +++ b/libgpsd_core.c @@ -24,6 +24,7 @@ #include #include #include +#include #ifndef S_SPLINT_S #include #include @@ -54,22 +55,6 @@ void errout_reset(struct errout_t *errout) errout->report = basic_report; } -void gpsd_notify(const struct errout_t *errout, - const int errlevel, - const char *fmt, ...) -{ - if (errout->debug >= errlevel) - { - char buf[128]; - va_list ap; - - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - errout->report(buf); - va_end(ap); - } -} - #if defined(PPS_ENABLE) static pthread_mutex_t report_mutex; @@ -102,23 +87,16 @@ static void visibilize(/*@out@*/char *buf2, size_t len, const char *buf) 0x00ff & (unsigned)*sp); } -const char *gpsd_prettydump(struct gps_device_t *session) -/* dump the current packet in a form optimised for eyeballs */ -{ - return gpsd_packetdump(session->msgbuf, sizeof(session->msgbuf), - (char *)session->lexer.outbuffer, - session->lexer.outbuflen); -} - - -void gpsd_labeled_report(const int debuglevel, const int errlevel, - const char *label, const char *fmt, va_list ap) -/* assemble command in printf(3) style, use stderr or syslog */ +void gpsd_notify(const struct errout_t *errout, + const int errlevel, + const char *fmt, ...) +/* assemble msg in printf(3) style, use errout hook or syslog for delivery */ { #ifndef SQUELCH_ENABLE - if (errlevel <= debuglevel) { + if (errout->debug >= errlevel) { char buf[BUFSIZ], buf2[BUFSIZ]; char *err_str; + va_list ap; #if defined(PPS_ENABLE) gpsd_acquire_reporting_lock(); @@ -158,9 +136,13 @@ void gpsd_labeled_report(const int debuglevel, const int errlevel, err_str = "UNK: "; } - (void)strlcpy(buf, label, sizeof(buf)); + assert(errout->label != NULL); + (void)strlcpy(buf, errout->label, sizeof(buf)); + (void)strlcat(buf, ":", sizeof(buf)); (void)strncat(buf, err_str, sizeof(buf) - 1 - strlen(buf)); + va_start(ap, fmt); (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, ap); + va_end(ap); visibilize(buf2, sizeof(buf2), buf); @@ -175,6 +157,15 @@ void gpsd_labeled_report(const int debuglevel, const int errlevel, #endif /* !SQUELCH_ENABLE */ } +const char *gpsd_prettydump(struct gps_device_t *session) +/* dump the current packet in a form optimised for eyeballs */ +{ + return gpsd_packetdump(session->msgbuf, sizeof(session->msgbuf), + (char *)session->lexer.outbuffer, + session->lexer.outbuflen); +} + + static void gpsd_run_device_hook(struct errout_t *errout, char *device_name, char *hook) { diff --git a/test_packet.c b/test_packet.c index f0e5b503..da52c033 100644 --- a/test_packet.c +++ b/test_packet.c @@ -17,23 +17,6 @@ static int verbose = 0; -void gpsd_report(int debuglevel, int errlevel, const char *fmt, ...) -/* assemble command in printf(3) style, use stderr or syslog */ -{ - if (errlevel <= debuglevel) { - char buf[BUFSIZ]; - va_list ap; - - buf[0] = '\0'; - va_start(ap, fmt); - (void)vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), fmt, - ap); - va_end(ap); - - (void)fputs(buf, stderr); - } -} - struct map { char *legend; -- cgit v1.2.1