Systemd can use the daemon output and send it to the journal (or a
compatible syslog implementation). Currently in the unit file we are
starting connmand with the recommended way by systemd (without forking)
but the log messages are duplicated since we are logging both to syslog
and stderr due to LOG_PERROR option in openlog() call. Example of
duplicated messages in journal:

Jul 10 20:15:37 vader connmand[194]: connmand[194]: Skipping disconnect of 
426561636843...ng
Jul 10 20:15:37 vader connmand[194]: Skipping disconnect of 
4265616368436c6173735769466...ng
Jul 10 20:17:21 vader connmand[194]: connmand[194]: Skipping disconnect of 
426561636843...ng
Jul 10 20:17:21 vader connmand[194]: Skipping disconnect of 
4265616368436c6173735769466...ng
Jul 10 20:15:37 vader connmand[194]: connmand[194]: Skipping disconnect

Now we only log to stderr, passing the priority as detailed in
sd-daemon(7).
---

There are some other options:

 * Use journal api instead
 * Continue using syslog() but without LOG_PERROR - then we would need to
   special-case when not starting as a daemon
 

 include/log.h | 20 +++++++++-------
 src/log.c     | 75 +++++------------------------------------------------------
 2 files changed, 18 insertions(+), 77 deletions(-)

diff --git a/include/log.h b/include/log.h
index 2a7de10..58ffb0a 100644
--- a/include/log.h
+++ b/include/log.h
@@ -28,19 +28,23 @@ extern "C" {
 
 /**
  * SECTION:log
- * @title: Logging premitives
+ * @title: Logging primitives
  * @short_description: Functions for logging error and debug information
  */
 
-void connman_info(const char *format, ...)
-                               __attribute__((format(printf, 1, 2)));
-void connman_warn(const char *format, ...)
-                               __attribute__((format(printf, 1, 2)));
-void connman_error(const char *format, ...)
-                               __attribute__((format(printf, 1, 2)));
-void connman_debug(const char *format, ...)
+#define CONNMAN_LOG_ERROR "<3>"
+#define CONNMAN_LOG_WARN "<4>"
+#define CONNMAN_LOG_INFO "<6>"
+#define CONNMAN_LOG_DBG "<7>"
+
+void connman_log(const char *format, ...)
                                __attribute__((format(printf, 1, 2)));
 
+#define connman_error(fmt, arg...) connman_log(CONNMAN_LOG_ERROR fmt "\n", ## 
arg)
+#define connman_info(fmt, arg...) connman_log(CONNMAN_LOG_INFO fmt "\n", ## 
arg)
+#define connman_warn(fmt, arg...) connman_log(CONNMAN_LOG_WARN fmt "\n", ## 
arg)
+#define connman_debug(fmt, arg...) connman_log(CONNMAN_LOG_DBG fmt "\n", ## 
arg)
+
 struct connman_debug_desc {
        const char *name;
        const char *file;
diff --git a/src/log.c b/src/log.c
index 7d1896c..7c870be 100644
--- a/src/log.c
+++ b/src/log.c
@@ -29,7 +29,6 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
-#include <syslog.h>
 #include <execinfo.h>
 #include <dlfcn.h>
 
@@ -39,73 +38,19 @@ static const char *program_exec;
 static const char *program_path;
 
 /**
- * connman_info:
+ * connman_log:
  * @format: format string
  * @Varargs: list of arguments
  *
- * Output general information
+ * Output logging information
  */
-void connman_info(const char *format, ...)
+void connman_log(const char *format, ...)
 {
        va_list ap;
 
        va_start(ap, format);
 
-       vsyslog(LOG_INFO, format, ap);
-
-       va_end(ap);
-}
-
-/**
- * connman_warn:
- * @format: format string
- * @Varargs: list of arguments
- *
- * Output warning messages
- */
-void connman_warn(const char *format, ...)
-{
-       va_list ap;
-
-       va_start(ap, format);
-
-       vsyslog(LOG_WARNING, format, ap);
-
-       va_end(ap);
-}
-
-/**
- * connman_error:
- * @format: format string
- * @varargs: list of arguments
- *
- * Output error messages
- */
-void connman_error(const char *format, ...)
-{
-       va_list ap;
-
-       va_start(ap, format);
-
-       vsyslog(LOG_ERR, format, ap);
-
-       va_end(ap);
-}
-
-/**
- * connman_debug:
- * @format: format string
- * @varargs: list of arguments
- *
- * Output debug message
- */
-void connman_debug(const char *format, ...)
-{
-       va_list ap;
-
-       va_start(ap, format);
-
-       vsyslog(LOG_DEBUG, format, ap);
+       vfprintf(stderr, format, ap);
 
        va_end(ap);
 }
@@ -295,7 +240,6 @@ int __connman_log_init(const char *program, const char 
*debug,
                                                connman_bool_t detach)
 {
        static char path[PATH_MAX];
-       int option = LOG_NDELAY | LOG_PID;
 
        program_exec = program;
        program_path = getcwd(path, sizeof(path));
@@ -305,23 +249,16 @@ int __connman_log_init(const char *program, const char 
*debug,
 
        __connman_log_enable(__start___debug, __stop___debug);
 
-       if (detach == FALSE)
-               option |= LOG_PERROR;
-
        signal_setup(signal_handler);
 
-       openlog(basename(program), option, LOG_DAEMON);
-
-       syslog(LOG_INFO, "Connection Manager version %s", VERSION);
+       connman_info("Connection Manager version %s", VERSION);
 
        return 0;
 }
 
 void __connman_log_cleanup(void)
 {
-       syslog(LOG_INFO, "Exit");
-
-       closelog();
+       connman_info("Exit");
 
        signal_setup(SIG_DFL);
 
-- 
1.7.11.1

_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to