Implemented '--syslog' and '--syslog-facility'.
[captive.git] / src / libcaptive / client / init.c
index d49eed3..e5649cc 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "config.h"
 
+#include "init.h"      /* self */
 #include "captive/ldr.h"
 #include "captive/ldr_exports.h"
 #include "captive/unicode.h"
@@ -49,6 +50,7 @@
 #include <libgnomevfs/gnome-vfs-result.h>
 #include "lib.h"
 #include <reactos/ddk/obfuncs.h>
+#include <syslog.h>
 
 
 struct captive_options *captive_options;
@@ -212,11 +214,94 @@ NTSTATUS err;
 }
 
 
-static void    log_discard_func(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
+static void    captive_log_init_g_log_func_discard
+               (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
 {
+       g_return_if_fail(message!=NULL);
+
        /* NOP */
 }
 
+static void captive_log_init_g_log_func
+               (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data /* unused */)
+{
+int priority;
+
+       g_return_if_fail(message!=NULL);
+
+               /* unused: LOG_EMERG */
+               /* unused: LOG_ALERT */
+       /**/ if (log_level&G_LOG_LEVEL_ERROR)
+               priority=LOG_CRIT;
+       else if (log_level&G_LOG_LEVEL_CRITICAL)
+               priority=LOG_ERR;
+       else if (log_level&G_LOG_LEVEL_WARNING)
+               priority=LOG_WARNING;
+       else if (log_level&G_LOG_LEVEL_MESSAGE)
+               priority=LOG_NOTICE;
+       else if (log_level&G_LOG_LEVEL_INFO)
+               priority=LOG_INFO;
+       else if (log_level&G_LOG_LEVEL_DEBUG)
+               priority=LOG_DEBUG;
+       else /* bogus? */
+               priority=LOG_WARNING;
+
+       syslog(priority,"%s%s%s",
+                       (!(log_level&G_LOG_FLAG_RECURSION) ? "" : "RECURSION: "),
+                       (!(log_level&G_LOG_FLAG_FATAL    ) ? "" : "FATAL: "),
+                       message);
+}
+
+void captive_log_init(const struct captive_options *captive_options)
+{
+       g_return_if_fail(captive_options!=NULL);
+
+       if (!captive_options->debug_messages) {
+               /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
+               g_log_set_handler(
+                               G_LOG_DOMAIN,   /* log_domain; "Captive" */
+                               0       /* log_levels */
+                                               | G_LOG_FLAG_RECURSION
+                                               | G_LOG_FLAG_FATAL
+                                               /* The same mask is in:
+                                                * libcaptive/sandbox/server-GLogFunc.c
+                                                * libcaptive/client/init.c
+                                                */
+                                               | G_LOG_LEVEL_MESSAGE
+                                               | G_LOG_LEVEL_INFO
+                                               | G_LOG_LEVEL_DEBUG,
+                               captive_log_init_g_log_func_discard,    /* log_func */
+                               NULL);  /* user_data */
+               }
+       /* We are not the sandboxed slave;
+        * 'syslog_facility' would be '-1' in slave anyway as it is not transferred through CORBA.
+        */
+       if (!captive_options->sandbox || (captive_options->sandbox_server_argv || captive_options->sandbox_server_ior)) {
+               if (captive_options->syslog_facility!=-1) {
+                       openlog(
+                                       /* FIXME: Prefix 'ident' by device/mountpoint. */
+                                       G_LOG_DOMAIN,   /* ident; "Captive"; FIXME: lowercase it for syslog(3)? */
+                                       LOG_CONS|LOG_PID,       /* options */
+                                       captive_options->syslog_facility);      /* facility */
+                       /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
+                       g_log_set_handler(
+                                       G_LOG_DOMAIN,   /* log_domain; "Captive" */
+                                       0       /* log_levels */
+                                                       | 0     /* !G_LOG_FLAG_RECURSION */
+                                                       | G_LOG_FLAG_FATAL
+                                                       | (G_LOG_LEVEL_MASK & ~(captive_options->debug_messages ? 0 : 0
+                                                                       /* The same mask is in:
+                                                                        * libcaptive/sandbox/server-GLogFunc.c
+                                                                        * libcaptive/client/init.c
+                                                                        */
+                                                                       | G_LOG_LEVEL_MESSAGE
+                                                                       | G_LOG_LEVEL_INFO
+                                                                       | G_LOG_LEVEL_DEBUG)),
+                                       (GLogFunc)captive_log_init_g_log_func,  /* log_func */
+                                       NULL);  /* user_data */
+                       }
+               }
+}
 
 /**
  * captive_init:
@@ -247,23 +332,7 @@ gboolean errbool;
        /* Initialize GObject subsystem of GLib. */
        g_type_init();
 
-       if (!captive_options->debug_messages) {
-               g_log_set_handler(
-                               G_LOG_DOMAIN,   /* log_domain; "Captive" */
-                               0       /* log_levels */
-                                               | G_LOG_FLAG_RECURSION
-                                               | G_LOG_FLAG_FATAL
-                                               /* The same mask is in:
-                                                * libcaptive/sandbox/server-GLogFunc.c
-                                                * libcaptive/client/init.c
-                                                * libcaptive/client/vfs.c
-                                                */
-                                               | G_LOG_LEVEL_MESSAGE
-                                               | G_LOG_LEVEL_INFO
-                                               | G_LOG_LEVEL_DEBUG,
-                               log_discard_func,       /* log_func */
-                               NULL);  /* user_data */
-               }
+       captive_log_init(captive_options);
 
        if (captive_options->rwmode==CAPTIVE_OPTION_RWMODE_BLIND)
                captive_image_iochannel=(GIOChannel *)captive_giochannel_blind_new(captive_options->image_iochannel,