gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH 1/3] OS syslog compatibility fallback.


From: Robert Norris
Subject: [gpsd-dev] [PATCH 1/3] OS syslog compatibility fallback.
Date: Fri, 24 Feb 2017 22:55:11 +0000

Mainly for Windows systems which doesn't have syslog().

Implement simple fallback to print to stderr,
 rather than a more complicated use of Windows Event Log.

Also basic fallbacks for openlog() and closelog() which
 don't really do anything.

Wrap all includes of syslog.h with a checking #ifdef
This shouldn't effect current systems as it should exist

TESTED:
Functions build on Windows cross build
'scons build-all check' passes on Linux
---
 SConstruct     |  2 +-
 gpsd.c         |  4 ++++
 gpsdctl.c      |  3 +++
 gpxlogger.c    |  5 ++++-
 libgps_dbus.c  |  2 ++
 libgpsd_core.c |  3 +++
 os_compat.c    | 30 ++++++++++++++++++++++++++++++
 os_compat.h    | 44 ++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/SConstruct b/SConstruct
index f3416ba7..c5eb13eb 100644
--- a/SConstruct
+++ b/SConstruct
@@ -750,7 +750,7 @@ else:
             announce("You do not have the endian.h header file. RTCM V2 
support disabled.")
             env["rtcm104v2"] = False
 
-    for hdr in ("sys/un", "sys/socket", "sys/select", "netdb", "netinet/in", 
"netinet/ip", "arpa/inet", "termios", "winsock2"):
+    for hdr in ("sys/un", "sys/socket", "sys/select", "netdb", "netinet/in", 
"netinet/ip", "arpa/inet", "syslog", "termios", "winsock2"):
         if config.CheckHeader(hdr + ".h"):
             confdefs.append("#define HAVE_%s_H 1\n" % 
hdr.replace("/","_").upper())
         else:
diff --git a/gpsd.c b/gpsd.c
index c14e09a5..1d267989 100644
--- a/gpsd.c
+++ b/gpsd.c
@@ -20,6 +20,8 @@
 /* strlcpy() needs _DARWIN_C_SOURCE */
 #define _DARWIN_C_SOURCE
 
+#include "os_compat.h"
+
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/time.h>          /* for select() */
@@ -34,7 +36,9 @@
 #include <setjmp.h>
 #include <assert.h>
 #include <math.h>
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 #include <errno.h>
 #include <signal.h>
 #include <ctype.h>
diff --git a/gpsdctl.c b/gpsdctl.c
index cd794184..06e7cdb6 100644
--- a/gpsdctl.c
+++ b/gpsdctl.c
@@ -4,11 +4,14 @@
  * BSD terms apply: see the file COPYING in the distribution root for details.
  *
  */
+#include "os_compat.h"
 #include <string.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/gpxlogger.c b/gpxlogger.c
index 5cd2734e..e34c2132 100644
--- a/gpxlogger.c
+++ b/gpxlogger.c
@@ -3,11 +3,15 @@
  * BSD terms apply: see the file COPYING in the distribution root for details.
  */
 
+#include "gpsd_config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 #include <math.h>
 #include <time.h>
 #include <errno.h>
@@ -17,7 +21,6 @@
 #include <unistd.h>
 
 #include "gps.h"
-#include "gpsd_config.h"
 #include "gpsdclient.h"
 #include "revision.h"
 #include "os_compat.h"
diff --git a/libgps_dbus.c b/libgps_dbus.c
index 29cd0dd0..53d35a44 100644
--- a/libgps_dbus.c
+++ b/libgps_dbus.c
@@ -14,7 +14,9 @@
 #include "libgps.h"
 
 #if defined(DBUS_EXPORT_ENABLE)
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 
 struct privdata_t
 {
diff --git a/libgpsd_core.c b/libgpsd_core.c
index ddb22a6d..78e493db 100644
--- a/libgpsd_core.c
+++ b/libgpsd_core.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "os_compat.h"
 #include <stdbool.h>
 #include <libgen.h>
 #include <math.h>
@@ -32,7 +33,9 @@
 #include <stdarg.h>
 #include <fcntl.h>
 #include <ctype.h>
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/select.h>
diff --git a/os_compat.c b/os_compat.c
index 790aaf94..1617b544 100644
--- a/os_compat.c
+++ b/os_compat.c
@@ -130,6 +130,36 @@ int os_daemon(int nochdir, int noclose)
 
 /* Provide BSD strlcat()/strlcpy() on platforms that don't have it */
 
+#ifndef HAVE_SYSLOG_H
+#include "compiler.h"
+#include <stdarg.h>
+#include <stdio.h>
+/*
+ * Minimal syslog() fallback to print to stderr
+ *
+ */
+PRINTF_FUNC(2, 3) void syslog(int priority UNUSED, const char *format, ...)
+{
+  /* ATM ignore priority (i.e. don't even both prepending to output) */
+  char buf[BUFSIZ];
+  va_list ap;
+  va_start(ap, format);
+  /* Always append a new line to the message */
+  (void)vsnprintf(buf, sizeof(buf) - 2, format, ap);
+  (void)fprintf(stderr, "%s\n", buf);
+  va_end(ap);
+}
+
+void openlog (const char *__ident UNUSED, int __option UNUSED, int __facility 
UNUSED)
+{
+  (void)fprintf(stderr, "Warning openlog() not available\n");
+}
+
+void closelog (void)
+{
+}
+#endif /* !HAVE_SYSLOG_H */
+
 /*
  * These versions use memcpy and strlen() because they are often
  * heavily optimized down to assembler level. Thus, likely to be
diff --git a/os_compat.h b/os_compat.h
index 93ea8b06..18ce4985 100644
--- a/os_compat.h
+++ b/os_compat.h
@@ -57,6 +57,50 @@ int clock_gettime(clockid_t, struct timespec *);
 
 int os_daemon(int nochdir, int noclose);
 
+
+#ifndef HAVE_SYSLOG_H
+/*
+ * Substitutes for syslog functions
+ *
+ */
+/* Copy of syslog.h defines when otherwise not available */
+/* priorities (these are ordered) */
+#define        LOG_EMERG       0       /* system is unusable */
+#define        LOG_ALERT       1       /* action must be taken immediately */
+#define        LOG_CRIT        2       /* critical conditions */
+#define        LOG_ERR         3       /* error conditions */
+#define        LOG_WARNING     4       /* warning conditions */
+#define        LOG_NOTICE      5       /* normal but significant condition */
+#define        LOG_INFO        6       /* informational */
+#define        LOG_DEBUG       7       /* debug-level messages */
+/* Option flags for openlog */
+#define        LOG_PID         0x01    /* log the pid with each message */
+#define        LOG_CONS        0x02    /* log on the console if errors in 
sending */
+#define        LOG_ODELAY      0x04    /* delay open until first syslog() 
(default) */
+#define        LOG_NDELAY      0x08    /* don't delay open */
+#define        LOG_NOWAIT      0x10    /* don't wait for console forks: 
DEPRECATED */
+#define        LOG_PERROR      0x20    /* log to stderr as well */
+/* facility codes */
+#define        LOG_KERN        (0<<3)  /* kernel messages */
+#define        LOG_USER        (1<<3)  /* random user-level messages */
+#define        LOG_MAIL        (2<<3)  /* mail system */
+#define        LOG_DAEMON      (3<<3)  /* system daemons */
+#define        LOG_AUTH        (4<<3)  /* security/authorization messages */
+#define        LOG_SYSLOG      (5<<3)  /* messages generated internally by 
syslogd */
+#define        LOG_LPR         (6<<3)  /* line printer subsystem */
+#define        LOG_NEWS        (7<<3)  /* network news subsystem */
+#define        LOG_UUCP        (8<<3)  /* UUCP subsystem */
+#define        LOG_CRON        (9<<3)  /* clock daemon */
+#define        LOG_AUTHPRIV    (10<<3) /* security/authorization messages 
(private) */
+#define        LOG_FTP         (11<<3) /* ftp daemon */
+
+void syslog(int priority, const char *format, ...);
+void openlog(const char *__ident, int __option, int __facility);
+void closelog(void);
+
+#endif /* HAVE_SYSLOG_H */
+
+
 /* Provide BSD strlcat()/strlcpy() on platforms that don't have it */
 
 #ifndef HAVE_STRLCAT
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]