make syslog.h optional

This commit is contained in:
John
2022-12-11 10:57:48 +01:00
parent 9e7565c849
commit a7de3175d8
3 changed files with 28 additions and 2 deletions
+1
View File
@@ -61,6 +61,7 @@ check_include_file(pthread.h HAVE_PTHREAD_H)
check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H) check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
check_include_file(sys/select.h HAVE_SYS_SELECT_H) check_include_file(sys/select.h HAVE_SYS_SELECT_H)
check_include_file(sys/time.h HAVE_SYS_TIME_H) check_include_file(sys/time.h HAVE_SYS_TIME_H)
check_include_file(syslog.h HAVE_SYSLOG_H)
check_include_file(time.h HAVE_TIME_H) check_include_file(time.h HAVE_TIME_H)
check_include_file(termios.h HAVE_TERMIOS_H) check_include_file(termios.h HAVE_TERMIOS_H)
+1
View File
@@ -23,6 +23,7 @@ AC_CHECK_HEADERS([arpa/inet.h \
sys/ioctl.h \ sys/ioctl.h \
sys/select.h \ sys/select.h \
sys/time.h \ sys/time.h \
syslog.h \
time.h \ time.h \
termios.h]) termios.h])
+26 -2
View File
@@ -27,7 +27,9 @@
#include <sys/time.h> #include <sys/time.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#ifdef HAVE_SYSLOG_H
#include <syslog.h> #include <syslog.h>
#endif
#include "lib/utils/clock.h" #include "lib/utils/clock.h"
namespace ebusd { namespace ebusd {
@@ -53,6 +55,7 @@ static const char* s_levelNames[] = {
nullptr nullptr
}; };
#ifdef HAVE_SYSLOG_H
/** the syslog level of each @a LogLevel. */ /** the syslog level of each @a LogLevel. */
static const int s_syslogLevels[] = { static const int s_syslogLevels[] = {
LOG_INFO, LOG_INFO,
@@ -62,6 +65,7 @@ static const int s_syslogLevels[] = {
LOG_DEBUG, LOG_DEBUG,
0 0
}; };
#endif
/** the current log level by log facility. */ /** the current log level by log facility. */
static LogLevel s_facilityLogLevel[] = { ll_notice, ll_notice, ll_notice, ll_notice, ll_notice, }; static LogLevel s_facilityLogLevel[] = { ll_notice, ll_notice, ll_notice, ll_notice, ll_notice, };
@@ -69,8 +73,10 @@ static LogLevel s_facilityLogLevel[] = { ll_notice, ll_notice, ll_notice, ll_not
/** the current log FILE, or nullptr if closed or syslog is used. */ /** the current log FILE, or nullptr if closed or syslog is used. */
static FILE* s_logFile = stdout; static FILE* s_logFile = stdout;
#ifdef HAVE_SYSLOG_H
/** whether to log to syslog. */ /** whether to log to syslog. */
static bool s_useSyslog = false; static bool s_useSyslog = false;
#endif
LogFacility parseLogFacility(const char* facility) { LogFacility parseLogFacility(const char* facility) {
if (!facility) { if (!facility) {
@@ -148,8 +154,12 @@ LogLevel getFacilityLogLevel(LogFacility facility) {
bool setLogFile(const char* filename) { bool setLogFile(const char* filename) {
if (filename[0] == 0) { if (filename[0] == 0) {
closeLogFile(); closeLogFile();
#ifdef HAVE_SYSLOG_H
openlog("ebusd", LOG_NDELAY|LOG_PID, LOG_USER); openlog("ebusd", LOG_NDELAY|LOG_PID, LOG_USER);
s_useSyslog = true; s_useSyslog = true;
#else
s_logFile = stdout;
#endif
return true; return true;
} }
FILE* newFile = fopen(filename, "a"); FILE* newFile = fopen(filename, "a");
@@ -168,28 +178,40 @@ void closeLogFile() {
} }
s_logFile = nullptr; s_logFile = nullptr;
} }
#ifdef HAVE_SYSLOG_H
if (s_useSyslog) { if (s_useSyslog) {
closelog(); closelog();
s_useSyslog = false; s_useSyslog = false;
} }
#endif
} }
bool needsLog(const LogFacility facility, const LogLevel level) { bool needsLog(const LogFacility facility, const LogLevel level) {
if (s_logFile == nullptr && !s_useSyslog) { if (s_logFile == nullptr
#ifdef HAVE_SYSLOG_H
&& !s_useSyslog
#endif
) {
return false; return false;
} }
return s_facilityLogLevel[facility] >= level; return s_facilityLogLevel[facility] >= level;
} }
void logWrite(const char* facility, const LogLevel level, const char* message, va_list ap) { void logWrite(const char* facility, const LogLevel level, const char* message, va_list ap) {
if (s_logFile == nullptr && !s_useSyslog) { if (s_logFile == nullptr
#ifdef HAVE_SYSLOG_H
&& !s_useSyslog
#endif
) {
return; return;
} }
char* buf; char* buf;
if (vasprintf(&buf, message, ap) >= 0 && buf) { if (vasprintf(&buf, message, ap) >= 0 && buf) {
#ifdef HAVE_SYSLOG_H
if (s_useSyslog) { if (s_useSyslog) {
syslog(s_syslogLevels[level], "[%s %s] %s", facility, s_levelNames[level], buf); syslog(s_syslogLevels[level], "[%s %s] %s", facility, s_levelNames[level], buf);
} else { } else {
#endif
struct timespec ts; struct timespec ts;
struct tm td; struct tm td;
clockGettime(&ts); clockGettime(&ts);
@@ -199,7 +221,9 @@ void logWrite(const char* facility, const LogLevel level, const char* message, v
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000, td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
facility, s_levelNames[level], buf); facility, s_levelNames[level], buf);
fflush(s_logFile); fflush(s_logFile);
#ifdef HAVE_SYSLOG_H
} }
#endif
} }
if (buf) { if (buf) {
free(buf); free(buf);