From 05380113b084bb9412db2fbb86b7927f79589a7d Mon Sep 17 00:00:00 2001
Subject: [PATCH] Added support for syslog msg

---
 WINGs/WINGs/WUtil.h |  3 +++
 WINGs/error.c       | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 configure.ac        |  5 +++++
 src/startup.c       |  5 +++++
 4 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 246ef2d..b2e76ab 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -243,6 +243,9 @@ enum {
 void __wmessage(const char *func, const char *file, int line, int type, const char *msg, ...)
 	__attribute__((__format__(printf,5,6)));
 
+void syslog_shutdown(void);
+
+
 /* ---[ WINGs/findfile.c ]------------------------------------------------ */
 
 /* For the 4 function below, you have to free the returned string when you no longer need it */
diff --git a/WINGs/error.c b/WINGs/error.c
index 2d5a588..d966d99 100644
--- a/WINGs/error.c
+++ b/WINGs/error.c
@@ -30,6 +30,46 @@
 #include <WUtil.h>
 #include <WINGsP.h>
 
+#ifdef HAVE_SYSLOG_H
+#include <syslog.h>
+static Bool syslog_initialized = False;
+#endif
+
+void syslog_open(char *prog_name)
+{
+#ifdef HAVE_SYSLOG
+	int options;
+		
+	if (!prog_name)
+		prog_name = "WINGs";
+	
+	options = LOG_PID;
+	openlog(prog_name, options, LOG_DAEMON);	
+	syslog_initialized = True;
+#endif
+}
+
+void syslog_message(int prio, char *prog_name, char *msg) 
+{
+#ifdef HAVE_SYSLOG
+	if (!syslog_initialized)
+		syslog_open(prog_name);
+
+	//jump other the program name cause syslog is already displaying it
+	syslog(prio, "%s", msg+strlen(prog_name));
+#endif
+}
+
+void syslog_shutdown(void)
+{
+#ifdef HAVE_SYSLOG
+	if (syslog_initialized)
+	{
+		closelog();
+		syslog_initialized = False;
+	}
+#endif
+}
 
 void __wmessage(const char *func, const char *file, int line, int type, const char *msg, ...)
 {
@@ -37,6 +77,10 @@ void __wmessage(const char *func, const char *file, int line, int type, const ch
 	char *buf;
 	static int linemax = 0;
 	int truncated = 0;
+#ifdef HAVE_SYSLOG	
+	int syslog_priority = LOG_INFO;
+	const char *syslog_prefix = "INFO";
+#endif
 
 	if (linemax == 0) {
 #ifdef HAVE_SYSCONF
@@ -65,13 +109,25 @@ void __wmessage(const char *func, const char *file, int line, int type, const ch
 
 	switch (type) {
 		case WMESSAGE_TYPE_FATAL:
-			strncat(buf, _("fatal error: "), linemax - 1 - strlen(buf));
+			strncat(buf, _("FATAL: "), linemax - 1 - strlen(buf));
+#ifdef HAVE_SYSLOG			
+			syslog_priority = LOG_CRIT;
+			syslog_prefix = "FATAL";
+#endif
 		break;
 		case WMESSAGE_TYPE_ERROR:
-			strncat(buf, _("error: "), linemax - 1 - strlen(buf));
+			strncat(buf, _("ERROR: "), linemax - 1 - strlen(buf));
+#ifdef HAVE_SYSLOG			
+			syslog_priority = LOG_ERR;
+			syslog_prefix = "ERROR";
+#endif
 		break;
 		case WMESSAGE_TYPE_WARNING:
-			strncat(buf, _("warning: "), linemax - 1 - strlen(buf));
+			strncat(buf, _("WARN: "), linemax - 1 - strlen(buf));
+#ifdef HAVE_SYSLOG			
+			syslog_priority = LOG_WARNING;
+			syslog_prefix = "WARNING";
+#endif
 		break;
 		case WMESSAGE_TYPE_MESSAGE:
 			/* FALLTHROUGH */
@@ -86,6 +142,9 @@ void __wmessage(const char *func, const char *file, int line, int type, const ch
 	va_end(args);
 
 	fputs(buf, stderr);
+#ifdef HAVE_SYSLOG
+	syslog_message(syslog_priority, _WINGS_progname ? _WINGS_progname : "WINGs", buf);
+#endif
 
 	if (truncated)
 		fputs("*** message truncated ***", stderr);
diff --git a/configure.ac b/configure.ac
index aa6b5b5..d642489 100644
--- a/configure.ac
+++ b/configure.ac
@@ -293,6 +293,11 @@ dnl =================
 AC_CHECK_HEADERS(sys/inotify.h, AC_DEFINE(HAVE_INOTIFY, 1, Check for inotify))
 
 
+dnl Check for syslog 
+dnl =================
+AC_CHECK_HEADERS(syslog.h, AC_DEFINE(HAVE_SYSLOG, 1, Check for syslog))
+
+
 dnl Checks for header files.
 dnl =======================
 AC_HEADER_SYS_WAIT
diff --git a/src/startup.c b/src/startup.c
index 6ede6f5..34e3635 100644
--- a/src/startup.c
+++ b/src/startup.c
@@ -160,6 +160,11 @@ static RETSIGTYPE handleExitSig(int sig)
 	}
 
 	sigprocmask(SIG_UNBLOCK, &sigs, NULL);
+
+#ifdef HAVE_SYSLOG
+	syslog_shutdown();
+#endif
+
 	DispatchEvent(NULL);	/* Dispatch events imediately. */
 }
 
-- 
1.8.3.2

