Add logging infrastructure
authorMarcel Holtmann <marcel@holtmann.org>
Fri, 22 Feb 2008 07:42:19 +0000 (08:42 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 22 Feb 2008 07:42:19 +0000 (08:42 +0100)
include/Makefile.am
include/log.h [new file with mode: 0644]
src/Makefile.am
src/connman.h
src/log.c [new file with mode: 0644]
src/main.c

index 2d28a82..ecca928 100644 (file)
@@ -1,7 +1,7 @@
 
 includedir = @includedir@/connman
 
-noinst_HEADERS = plugin.h iface.h rtnl.h dhcp.h
+noinst_HEADERS = log.h plugin.h iface.h rtnl.h dhcp.h
 
 MAINTAINERCLEANFILES = Makefile.in
 
diff --git a/include/log.h b/include/log.h
new file mode 100644 (file)
index 0000000..e87e38a
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CONNMAN_LOG_H
+#define __CONNMAN_LOG_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define DBG(fmt, arg...) connman_debug(fmt, ## arg)
+
+extern void connman_info(const char *format, ...);
+extern void connman_error(const char *format, ...);
+extern void connman_debug(const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONNMAN_LOG_H */
index 1a09a0f..516c1b8 100644 (file)
@@ -5,9 +5,9 @@ dbus_DATA = connman.conf
 
 sbin_PROGRAMS = connmand
 
-connmand_SOURCES = main.c connman.h manager.c agent.c plugin.c \
-                       iface.c iface-storage.c iface-helper.c \
-                                       iface-inet.c rtnl.c dhcp.c
+connmand_SOURCES = main.c connman.h log.c manager.c agent.c plugin.c \
+                               iface.c iface-storage.c iface-helper.c \
+                                               iface-inet.c rtnl.c dhcp.c
 
 connmand_LDADD = @HAL_LIBS@ @GDBUS_LIBS@ @GMODULE_LIBS@
  
index 703b8c9..da1693b 100644 (file)
@@ -21,9 +21,6 @@
 
 #include <stdio.h>
 
-#define DBG(fmt, arg...)  printf("%s: " fmt "\n" , __FUNCTION__ , ## arg)
-//#define DBG(fmt, arg...)
-
 #include <dbus/dbus.h>
 
 #define CONNMAN_SERVICE  "org.freedesktop.connman"
@@ -50,6 +47,11 @@ void __connman_agent_cleanup(void);
 int __connman_agent_register(const char *sender, const char *path);
 int __connman_agent_unregister(const char *sender, const char *path);
 
+#include <connman/log.h>
+
+int __connman_log_init(int syslog, int debug);
+void __connman_log_cleanup(void);
+
 #include <connman/plugin.h>
 
 int __connman_plugin_init(void);
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..867928c
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,90 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdarg.h>
+#include <syslog.h>
+
+#include "connman.h"
+
+static volatile int debug_enabled = 0;
+
+void connman_info(const char *format, ...)
+{
+       va_list ap;
+
+       va_start(ap, format);
+
+       vsyslog(LOG_INFO, format, ap);
+
+       va_end(ap);
+}
+
+void connman_error(const char *format, ...)
+{
+       va_list ap;
+
+       va_start(ap, format);
+
+       vsyslog(LOG_ERR, format, ap);
+
+       va_end(ap);
+}
+
+void connman_debug(const char *format, ...)
+{
+       va_list ap;
+
+       if (!debug_enabled)
+               return;
+
+       va_start(ap, format);
+
+       vsyslog(LOG_DEBUG, format, ap);
+
+       va_end(ap);
+}
+
+int __connman_log_init(int syslog, int debug)
+{
+       int option = LOG_NDELAY | LOG_PID;
+
+       if (!syslog)
+               option |= LOG_PERROR;
+
+       openlog("connmand", option, LOG_DAEMON);
+
+       syslog(LOG_INFO, "Connection Manager version %s", VERSION);
+
+       debug_enabled = debug;
+
+       return 0;
+}
+
+void __connman_log_cleanup(void)
+{
+       syslog(LOG_INFO, "Exit");
+
+       closelog();
+}
index 0fcf614..a4962b2 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <syslog.h>
 #include <signal.h>
 #include <getopt.h>
 #include <sys/stat.h>
@@ -61,6 +60,7 @@ static void usage(void)
 static struct option options[] = {
        { "nodaemon", 0, 0, 'n' },
        { "compat",   0, 0, 'c' },
+       { "debug",    0, 0, 'd' },
        { "help",     0, 0, 'h' },
        { }
 };
@@ -70,17 +70,19 @@ int main(int argc, char *argv[])
        DBusConnection *conn;
        DBusError err;
        struct sigaction sa;
-       int log_option = LOG_NDELAY | LOG_PID;
-       int opt, detach = 1, compat = 0;
+       int opt, detach = 1, compat = 0, debug = 0;
 
-       while ((opt = getopt_long(argc, argv, "+nch", options, NULL)) != EOF) {
-               switch(opt) {
+       while ((opt = getopt_long(argc, argv, "+ncdh", options, NULL)) != EOF) {
+               switch (opt) {
                case 'n':
                        detach = 0;
                        break;
                case 'c':
                        compat = 1;
                        break;
+               case 'd':
+                       debug = 1;
+                       break;
                case 'h':
                default:
                        usage();
@@ -97,10 +99,7 @@ int main(int argc, char *argv[])
                        perror("Can't start daemon");
                        exit(1);
                }
-       } else
-               log_option |= LOG_PERROR;
-
-       openlog("connmand", log_option, LOG_DAEMON);
+       }
 
        mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
                        S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
@@ -127,6 +126,8 @@ int main(int argc, char *argv[])
                        compat = 0;
        }
 
+       __connman_log_init(detach, debug);
+
        __connman_agent_init(conn);
 
        __connman_manager_init(conn, compat);
@@ -154,6 +155,8 @@ int main(int argc, char *argv[])
 
        __connman_agent_cleanup();
 
+       __connman_log_cleanup();
+
        g_dbus_cleanup_connection(conn);
 
        g_main_loop_unref(main_loop);
@@ -162,7 +165,5 @@ int main(int argc, char *argv[])
 
        rmdir(STATEDIR);
 
-       closelog();
-
        return 0;
 }