libdlog: Unify common parts of dlog API 42/167842/4
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 25 Jan 2018 14:37:41 +0000 (15:37 +0100)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 25 Jan 2018 15:15:08 +0000 (16:15 +0100)
This commit reworks logic behind (__)dlog_(v)print
family of functions, removing copy paste code.

Change-Id: I76cdb39c6831c325427383f00f027bb4d6fe27bf

src/libdlog/log.c
src/libdlog/log_android.c
src/libdlog/log_pipe.c

index bd77f16..2aadaea 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <pthread.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <stdio.h>
 
 #include <logcommon.h>
@@ -31,6 +32,8 @@
 #define DEFAULT_CONFIG_DEBUGMODE 0
 #define DEBUGMODE_FILE TZ_SYS_ETC"/.debugmode"
 
+static int __write_to_log_null(log_id_t, log_priority, const char *, const char *);
+
 /**
  * @brief Points to a function which writes a log message
  * @details The function pointed to depends on the backend used
@@ -41,7 +44,7 @@
  * @return Returns the number of bytes written on success and a negative error value on error.
  * @see __dlog_init_backend
  */
-int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg);
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = __write_to_log_null;
 pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
 extern void __dlog_init_pipe();
 extern void __dlog_init_android();
@@ -60,7 +63,7 @@ static int fatal_assert;
  * @param[in] msg The contents of the message.
  * @return DLOG_ERROR_NOT_PERMITTED
  */
-int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+static int __write_to_log_null(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
 {
        return DLOG_ERROR_NOT_PERMITTED;
 }
@@ -162,7 +165,6 @@ static void __configure(void)
        return;
 
 failure:
-       write_to_log = __write_to_log_null;
        log_config_free(&config);
        return;
 }
@@ -171,10 +173,13 @@ failure:
  * @brief DLog init
  * @details Initializes the library
  */
-static void __dlog_init(void)
+static inline void __dlog_init(void)
 {
        static int is_initialized = 0;
 
+       if (is_initialized)
+               return;
+
        pthread_mutex_lock(&log_init_lock);
 
        if (!is_initialized) {
@@ -236,6 +241,21 @@ static int dlog_should_log(log_id_t log_id, const char* tag, int prio)
        return DLOG_ERROR_NONE;
 }
 
+static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log)
+{
+       char buf[LOG_MAX_SIZE];
+
+       __dlog_init();
+
+       int ret = check_should_log ? dlog_should_log(log_id, tag, prio) : 0;
+       if (ret < 0)
+               return ret;
+
+       vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+
+       return write_to_log(log_id, prio, tag, buf);
+}
+
 /**
  * @brief Print log
  * @details Print a log line
@@ -248,20 +268,9 @@ static int dlog_should_log(log_id_t log_id, const char* tag, int prio)
  */
 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap)
 {
-       int ret;
-       char buf[LOG_MAX_SIZE];
-
-       if (write_to_log == NULL)
-               __dlog_init();
-
-       ret = dlog_should_log(log_id, tag, prio);
-
-       if (ret < 0)
-               return ret;
-
-       vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
-       ret = write_to_log(log_id, prio, tag, buf);
+       int ret = __write_to_log(log_id, prio, tag, fmt, ap, true);
        __dlog_fatal_assert(prio);
+
        return ret;
 }
 
@@ -276,52 +285,29 @@ int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, v
  */
 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...)
 {
-       int ret;
        va_list ap;
-       char buf[LOG_MAX_SIZE];
-
-       if (write_to_log == NULL)
-               __dlog_init();
-
-       ret = dlog_should_log(log_id, tag, prio);
-
-       if (ret < 0)
-               return ret;
 
        va_start(ap, fmt);
-       vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+       int ret = __dlog_vprint(log_id, prio, tag, fmt, ap);
        va_end(ap);
 
-       ret = write_to_log(log_id, prio, tag, buf);
-       __dlog_fatal_assert(prio);
        return ret;
 }
 
 int dlog_vprint(log_priority prio, const char *tag, const char *fmt, va_list ap)
 {
-       char buf[LOG_MAX_SIZE];
-
-       if (write_to_log == NULL)
-               __dlog_init();
-
-       vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
-
-       return write_to_log(LOG_ID_APPS, prio, tag, buf);
+       return __write_to_log(LOG_ID_APPS, prio, tag, fmt, ap, false);
 }
 
 int dlog_print(log_priority prio, const char *tag, const char *fmt, ...)
 {
        va_list ap;
-       char buf[LOG_MAX_SIZE];
-
-       if (write_to_log == NULL)
-               __dlog_init();
 
        va_start(ap, fmt);
-       vsnprintf(buf, LOG_MAX_SIZE, fmt, ap);
+       int ret = dlog_vprint(prio, tag, fmt, ap);
        va_end(ap);
 
-       return write_to_log(LOG_ID_APPS, prio, tag, buf);
+       return ret;
 }
 
 /**
index abad875..535ce26 100644 (file)
@@ -5,7 +5,6 @@
 #include <logcommon.h>
 #include <logconfig.h>
 
-extern int __write_to_log_null(log_id_t, log_priority, const char *tag, const char *msg);
 extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
 
 /* TODO: initialize in for loop to be sure of initializing all items */
@@ -63,10 +62,8 @@ void __dlog_init_android()
        struct log_config conf;
        log_id_t buf_id;
 
-       if (log_config_read(&conf) < 0) {
-               write_to_log = __write_to_log_null;
+       if (log_config_read(&conf) < 0)
                return;
-       }
 
        for (buf_id = 0; buf_id < LOG_ID_MAX; ++buf_id) {
                const char * const buffer_name = log_config_get(&conf, log_name_by_id(buf_id));
@@ -98,8 +95,6 @@ failure:
                if (log_fds[buf_id] >= 0)
                        close(log_fds[buf_id]);
        }
-       write_to_log = __write_to_log_null;
-
 cleanup:
        log_config_free(&conf);
        return;
index 7189174..de95eb1 100644 (file)
@@ -8,7 +8,6 @@
 #include <logconfig.h>
 #include <assert.h>
 
-extern int __write_to_log_null(log_id_t, log_priority, const char *tag, const char *msg);
 extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
 extern pthread_mutex_t log_init_lock;
 /* TODO: initialize in for loop to be sure of initializing all items */
@@ -164,10 +163,8 @@ void __dlog_init_pipe()
         */
        signal(SIGPIPE, SIG_IGN);
 
-       if (log_config_read(&conf) < 0) {
-               write_to_log = __write_to_log_null;
+       if (log_config_read(&conf) < 0)
                return;
-       }
 
        for (i = 0; i < LOG_ID_MAX; ++i) {
                char conf_key[MAX_CONF_KEY_LEN];
@@ -175,7 +172,6 @@ void __dlog_init_pipe()
                const char * conf_val = log_config_get(&conf, conf_key);
                if (!conf_val) {
                        syslog_critical_failure("DLog config lacks the \"%s\" entry!");
-                       write_to_log = __write_to_log_null;
                        log_config_free(&conf);
                        return;
                }