Added a logging facility (MoonshotLogger, in moonshot-logger.vala). dbreslau-logging
authorDan Breslau <dbreslau@painless-security.com>
Wed, 6 Apr 2016 21:24:28 +0000 (17:24 -0400)
committerDan Breslau <dbreslau@painless-security.com>
Wed, 6 Apr 2016 21:24:28 +0000 (17:24 -0400)
By default, this does nothing. If enabled at compile time, logging
is implemented via Log4Vala.

To enable logging, run the ./configure script with the --enable-log4vala argument.
You must have Log4Vala installed on your system.
See https://github.com/nmelnick/Log4Vala.

Makefile.am
configure.ac
src/moonshot-logger.vala [new file with mode: 0644]

index f5ecce4..f480c26 100644 (file)
@@ -24,11 +24,21 @@ AM_CPPFLAGS =  \
        -I$(top_srcdir)/libmoonshot \
        -I$(top_builddir)/libmoonshot
 
+
+if LOG4VALA
+MOONSHOT_LOG_PKG= --pkg log4vala-0.1 -X -llog4vala-0.1 --define=USE_LOG4VALA
+MOONSHOT_LOG_LIBS= -llog4vala-0.1
+else
+MOONSHOT_LOG_PKG=
+MOONSHOT_LOG_LIBS=
+endif
+
+
 AM_VALAFLAGS = -g \
         config.vapi \
-       --vapidir=$(top_srcdir)/vapi \
-        --pkg gio-2.0
-
+        --vapidir=$(top_srcdir)/vapi \
+        --pkg gio-2.0 \
+        $(MOONSHOT_LOG_PKG)
 
 libmoonshot_libmoonshot_la_CPPFLAGS = \
         $(libmoonshot_CFLAGS) \
@@ -58,17 +68,18 @@ src_moonshot_SOURCES = \
         src/moonshot-password-dialog.vala \
         src/moonshot-provisioning-common.vala \
         src/moonshot-utils.vala \
-        src/moonshot-futils.c
+        src/moonshot-futils.c \
+        src/moonshot-logger.vala
 
 src_moonshot_webp_SOURCES = \
         src/moonshot-webp-parser.vala \
         src/moonshot-provisioning-common.vala \
         src/moonshot-id.vala
 
-src_moonshot_VALAFLAGS = --pkg gdk-2.0 --pkg gtk+-2.0 --pkg gee-1.0  --pkg log4vala-0.1 -X -llog4vala-0.1 $(AM_VALAFLAGS)
+src_moonshot_VALAFLAGS = --pkg gdk-2.0 --pkg gtk+-2.0 --pkg gee-1.0   $(AM_VALAFLAGS)
 src_moonshot_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS)  
-src_moonshot_LDADD = $(moonshot_LIBS) 
-src_moonshot_LDFLAGS = -g -O0  -llog4vala-0.1
+src_moonshot_LDADD = $(moonshot_LIBS)
+src_moonshot_LDFLAGS = -g -O0 $(MOONSHOT_LOG_LIBS)
 
 src_moonshot_webp_VALAFLAGS = --vapidir=$(top_srcdir)/libmoonshot  --pkg gtk+-2.0 --pkg gdk-2.0 --pkg libmoonshot $(AM_VALAFLAGS)
 src_moonshot_webp_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS)
@@ -168,7 +179,7 @@ $(dbusservice_DATA): $(dbusservice_in_files) Makefile
 
 libmoonshot_libmoonshot_la_SOURCES += libmoonshot/libmoonshot-dbus.c
 
-CLEANFILES = $(dbusservice_DATA)
+CLEANFILES = $(dbusservice_DATA)  src_moonshot_vala.stamp src_moonshot_vala.stamp-t
 
 if IPC_DBUS_GLIB
 AM_VALAFLAGS += \
index c9f63b4..5ff6644 100644 (file)
@@ -94,6 +94,17 @@ case "$host" in
     ;;
 esac
 
+#enable the optional use of Log4Vala (Must have the package installed!)
+AC_ARG_ENABLE([log4vala],
+[  --enable-log4vala    Enable use of log4vala for logging],
+[case "${enableval}" in
+  yes) log4vala=true ;;
+  no)  log4vala=false ;;
+  *) AC_MSG_ERROR([bad value ${enableval} for --enable-log4vala]) ;;
+esac],[log4vala=false])
+AM_CONDITIONAL([LOG4VALA], [test x$log4vala = xtrue])
+
+
 AM_CONDITIONAL([OS_LINUX], [test "$linux" = "yes"])
 AM_CONDITIONAL([OS_WIN32], [test "$win32" = "yes"])
 AM_CONDITIONAL([OS_MACOS], [test "$macos" = "yes"])
diff --git a/src/moonshot-logger.vala b/src/moonshot-logger.vala
new file mode 100644 (file)
index 0000000..e0ccef3
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2011-2014, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+*/
+
+
+public MoonshotLogger get_logger(string name) {
+       return new MoonshotLogger(name);
+}
+
+#if USE_LOG4VALA
+
+/** Logger class that wraps the Log4Vala logger */
+public class MoonshotLogger : Object {
+       static bool logger_is_initialized = false;
+
+       private Log4Vala.Logger logger;
+
+       public MoonshotLogger(string name) {
+               if (!logger_is_initialized) {
+                       //!! TODO: Don't hard-code the pathname.
+                       Log4Vala.init("/home/dbreslau/log4vala.conf");
+                       logger_is_initialized = true;
+               }
+
+               logger = Log4Vala.Logger.get_logger(name);
+       }
+
+       /**
+        * Log a trace message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void trace(string message, Error? e = null) {
+               logger.trace(message, e);
+       }
+
+
+       /**
+        * Log a debug message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void debug(string message, Error? e = null) {
+               logger.debug(message, e);
+       }
+
+
+       /**
+        * Log an info message.
+        * @param e optional Error to be logged
+        */
+       public void info(string message, Error? e = null) {
+               logger.info(message, e);
+       }
+
+       /**
+        * Log a warning message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void warn(string message, Error? e = null) {
+               logger.warn(message, e);
+       }
+
+       /**
+        * Log an error message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void error(string message, Error? e = null) {
+               logger.error(message, e);
+       }
+
+       /**
+        * Log a fatal message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void fatal(string message, Error? e = null) {
+               logger.fatal(message, e);
+       }
+
+}
+
+
+#else
+
+/** Logger that currently does nothing, but may eventually write to stdout or a file */
+public class MoonshotLogger : Object {
+
+       internal MoonshotLogger(string name) {
+       }
+
+       /**
+        * Log a trace message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void trace(string message, Error? e = null) {
+       }
+
+
+       /**
+        * Log a debug message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void debug(string message, Error? e = null) {
+       }
+
+
+       /**
+        * Log an info message.
+        * @param e optional Error to be logged
+        */
+       public void info(string message, Error? e = null) {
+       }
+
+       /**
+        * Log a warning message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void warn(string message, Error? e = null) {
+       }
+
+       /**
+        * Log an error message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void error(string message, Error? e = null) {
+       }
+
+       /**
+        * Log a fatal message.
+        * @param message log message
+        * @param e optional Error to be logged
+        */
+       public void fatal(string message, Error? e = null) {
+       }
+}
+
+#endif
+