From 9f870a11218d0bc6364d6b333fd878106746adf9 Mon Sep 17 00:00:00 2001 From: Robert Norris Date: Sun, 5 Mar 2017 18:43:33 +0000 Subject: OS syslog compatibility fallback. 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. TESTED: Functions build on Windows cross build 'scons build-all check' passes on Linux Signed-off-by: Fred Wright --- SConstruct | 2 +- gpxlogger.c | 1 - os_compat.c | 30 ++++++++++++++++++++++++++++++ os_compat.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/SConstruct b/SConstruct index f443184d..a39912fd 100644 --- a/SConstruct +++ b/SConstruct @@ -761,7 +761,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/gpxlogger.c b/gpxlogger.c index d93d527b..141b7486 100644 --- a/gpxlogger.c +++ b/gpxlogger.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include diff --git a/os_compat.c b/os_compat.c index fa4d85b2..8c117379 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 +#include +/* + * 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 00e56559..9321809f 100644 --- a/os_compat.h +++ b/os_compat.h @@ -57,6 +57,38 @@ int clock_gettime(clockid_t, struct timespec *); int os_daemon(int nochdir, int noclose); + +#ifdef HAVE_SYSLOG_H +#include +#else +/* + * Substitutes for syslog functions + * (only subset of defines used by gpsd components listed) + * + */ +/* 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_PERROR 0x20 /* log to stderr as well */ +/* facility codes */ +#define LOG_USER (1<<3) /* random user-level messages */ +#define LOG_DAEMON (3<<3) /* system daemons */ + +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 -- cgit v1.2.1