]> www.average.org Git - pulsecounter.git/blobdiff - linux/pulsecounter.c
daemon option
[pulsecounter.git] / linux / pulsecounter.c
index 5f629f14b96b41125c9023687e1502cb5b6eeded..89568c98597a972c45fb24e562c03642558e58a9 100644 (file)
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <stdbool.h>
 #include <time.h>
+#include <syslog.h>
 
 #include <glib.h>
 
@@ -32,6 +33,7 @@ static int opt_mtu = 0;
 static int opt_psm = 0;
 static char *opt_sec_level = NULL;
 static char *opt_dbconffile = NULL;
+static gboolean opt_daemon = FALSE;
 
 static GMainLoop *event_loop;
 
@@ -48,11 +50,33 @@ static GOptionEntry options[] = {
                "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
        { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
                "Set security level. Default: low", "[low | medium | high]"},
-       { "dbconfig", 'c', 0, G_OPTION_ARG_STRING, &opt_dbconffile,
+       { "dbconfig", 'c', 0, G_OPTION_ARG_FILENAME, &opt_dbconffile,
+               "Specify file name with database configuration", "cfile"},
+       { "daemon", 'd', 0, G_OPTION_ARG_NONE, &opt_daemon,
                "Specify file name with database configuration", "cfile"},
        { NULL },
 };
 
+void local_log_handler(const gchar *log_domain, GLogLevelFlags log_level,
+                       const gchar *message, gpointer log_context)
+{
+       int syslog_level;
+
+       switch (log_level) {
+       case G_LOG_LEVEL_CRITICAL:      syslog_level = LOG_CRIT;        break;
+       case G_LOG_LEVEL_ERROR:         syslog_level = LOG_ERR;         break;
+       case G_LOG_LEVEL_WARNING:       syslog_level = LOG_WARNING;     break;
+       case G_LOG_LEVEL_MESSAGE:       syslog_level = LOG_NOTICE;      break;
+       case G_LOG_LEVEL_INFO:          syslog_level = LOG_INFO;        break;
+       case G_LOG_LEVEL_DEBUG:         syslog_level = LOG_DEBUG;       break;
+       default:                        syslog_level = LOG_INFO;
+       }
+       if (!log_domain || (log_domain[0] == '\0'))
+               syslog(syslog_level, "%s", message);
+       else
+               syslog(syslog_level, "%s: %s", log_domain, message);
+}
+
 static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
                                gpointer user_data)
 {
@@ -132,7 +156,7 @@ int main(int argc, char *argv[])
        gboolean got_error = FALSE;
 
        g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
-                         | G_LOG_FLAG_RECURSION, g_log_default_handler, NULL);
+                         | G_LOG_FLAG_RECURSION, local_log_handler, NULL);
        opt_dst_type = g_strdup("public");
        opt_sec_level = g_strdup("low");
        opt_dbconffile = g_strdup("/etc/pulsecounter.db");
@@ -154,6 +178,7 @@ int main(int argc, char *argv[])
                got_error = TRUE;
                goto done;
        }
+       if (opt_daemon) daemon(0, 0);
        while (1) {
                chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
                        opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);