From: David Maciejak <[email protected]>

This patch is used to add support for syslog messaging implemented in
WINGs lib directly, so available from the lib itself and wmaker too.
I believe it will in a first time help to get some logging info
centralized in one point, and in a second time maybe add some info
level messages like wmaker is starting, stopping, restarting and else.

For now, it's built by default when the syslog support is found, maybe
we could also disable it by default.
---
 WINGs/Makefile.am   |  1 +
 WINGs/WINGs/WUtil.h |  6 +++++-
 WINGs/error.c       | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 WINGs/error.h       | 36 ++++++++++++++++++++++++++++++++++
 WINGs/misc.c        | 12 ++++++++++++
 configure.ac        |  5 +++++
 src/main.c          |  2 ++
 7 files changed, 116 insertions(+), 2 deletions(-)
 create mode 100644 WINGs/error.h

diff --git a/WINGs/Makefile.am b/WINGs/Makefile.am
index d28aff9..3dac104 100644
--- a/WINGs/Makefile.am
+++ b/WINGs/Makefile.am
@@ -67,6 +67,7 @@ libWUtil_la_SOURCES =         \
        bagtree.c \
        data.c \
        error.c \
+       error.h \
        findfile.c \
        handlers.c \
        hashtable.c \
diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h
index 246ef2d..da74dfc 100644
--- a/WINGs/WINGs/WUtil.h
+++ b/WINGs/WINGs/WUtil.h
@@ -305,10 +305,14 @@ char* wtrimspace(const char *s);
  */
 char *wshellquote(const char *s);
 
-/* ---[ WINGs/wmisc.c ]--------------------------------------------------- */
+/* ---[ WINGs/misc.c ]--------------------------------------------------- */
 
 WMRange wmkrange(int start, int count);
 
+/* An application must call this function before exiting, to let WUtil do some 
internal cleanup */
+void wutil_shutdown(void);
+
+
 /* ---[ WINGs/usleep.c ]-------------------------------------------------- */
 
 void wusleep(unsigned int usec);
diff --git a/WINGs/error.c b/WINGs/error.c
index 2d5a588..e94c428 100644
--- a/WINGs/error.c
+++ b/WINGs/error.c
@@ -30,6 +30,41 @@
 #include <WUtil.h>
 #include <WINGsP.h>
 
+#ifdef HAVE_SYSLOG_H
+#include <syslog.h>
+
+static Bool syslog_initialized = False;
+
+
+static void syslog_open(char *prog_name)
+{
+       int options;
+
+       if (!prog_name)
+               prog_name = "WINGs";
+
+       options = LOG_PID;
+       openlog(prog_name, options, LOG_DAEMON);
+       syslog_initialized = True;
+}
+
+static void syslog_message(int prio, char *prog_name, char *msg)
+{
+       if (!syslog_initialized)
+               syslog_open(prog_name);
+
+       //jump over the program name cause syslog is already displaying it
+       syslog(prio, "%s", msg + strlen(prog_name));
+}
+
+void w_syslog_close(void)
+{
+       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 +72,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 +104,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));
+#ifdef HAVE_SYSLOG
+                       syslog_priority = LOG_ERR;
+                       syslog_prefix = "ERROR";
+#endif
                break;
                case WMESSAGE_TYPE_WARNING:
                        strncat(buf, _("warning: "), linemax - 1 - strlen(buf));
+#ifdef HAVE_SYSLOG
+                       syslog_priority = LOG_WARNING;
+                       syslog_prefix = "WARNING";
+#endif
                break;
                case WMESSAGE_TYPE_MESSAGE:
                        /* FALLTHROUGH */
@@ -86,6 +137,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/WINGs/error.h b/WINGs/error.h
new file mode 100644
index 0000000..341c909
--- /dev/null
+++ b/WINGs/error.h
@@ -0,0 +1,36 @@
+/* WUtil / error.h
+ *
+ *  Copyright (c) 2014 Window Maker Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef WUTIL_ERROR_H
+#define WUTIL_ERROR_H
+
+/*
+ * This file is not part of WUtil public API
+ *
+ * It defines internal things for the error message display functions
+ */
+
+
+#ifdef HAVE_SYSLOG_H
+/* Function to cleanly close the syslog stuff, called by wutil_shutdown from 
user side */
+void w_syslog_close(void);
+#endif
+
+
+#endif /* WUTIL_ERROR_H */
diff --git a/WINGs/misc.c b/WINGs/misc.c
index 48f281d..f5a8c91 100644
--- a/WINGs/misc.c
+++ b/WINGs/misc.c
@@ -3,6 +3,8 @@
 
 #include "WINGsP.h"
 
+#include "error.h"
+
 WMRange wmkrange(int start, int count)
 {
        WMRange range;
@@ -12,3 +14,13 @@ WMRange wmkrange(int start, int count)
 
        return range;
 }
+
+/*
+ * wutil_shutdown - cleanup in WUtil when user program wants to exit
+ */
+void wutil_shutdown(void)
+{
+#ifdef HAVE_SYSLOG
+       w_syslog_close();
+#endif
+}
diff --git a/configure.ac b/configure.ac
index aa6b5b5..e3108f4 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/main.c b/src/main.c
index 59a21f7..a21290e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -201,6 +201,8 @@ noreturn void Exit(int status)
        if (dpy)
                XCloseDisplay(dpy);
 
+       wutil_shutdown();  /* WUtil clean-up */
+
        exit(status);
 }
 
-- 
1.8.5.3


-- 
To unsubscribe, send mail to [email protected].

Reply via email to