libdlog: reinitialize on each log if need be 33/206833/2 accepted/tizen/unified/20190530.043130 submit/tizen/20190529.011726 submit/tizen/20190603.232156
authorMichal Bloch <m.bloch@samsung.com>
Fri, 24 May 2019 14:27:18 +0000 (16:27 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 28 May 2019 10:16:34 +0000 (10:16 +0000)
Change-Id: I613cba9e29cbbe1581912cb1fccdce8c504ac536
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
include/libdlog.h
src/libdlog/log.c
src/tests/libdlog_base.c

index 39ac016..ce4e28f 100644 (file)
@@ -22,5 +22,5 @@ void __update_plog(const struct log_config *conf);
 #ifdef UNIT_TEST
 void __dlog_fini(void);
 void __dlog_destroy(void);
-void __configure(void);
+bool __configure(void);
 #endif
index c1906b1..67f3916 100644 (file)
@@ -165,32 +165,32 @@ void __update_plog(const struct log_config *conf)
 #ifndef UNIT_TEST
 static
 #endif
-void __configure(void)
+bool __configure(void)
 {
-       struct log_config config;
+       __attribute__((cleanup(log_config_free))) struct log_config config;
 
        if (log_config_read(&config) < 0)
-               goto out;
+               return false;
 
        dynamic_config = __dynamic_config_create(&config);
 
        __configure_parameters(&config);
 
-       if (!__configure_backend(&config))
-               goto out;
+       if (!__configure_backend(&config)) {
+               __dynamic_config_destroy();
+               dynamic_config = false;
+               return false;
+       }
 
        __configure_limiter(&config);
-
-out:
-       log_config_free(&config);
-       return;
+       return true;
 }
 
 static bool first = true;
-static void initialize()
+static bool initialize()
 {
        if (is_initialized)
-               return;
+               return true;
 
        /* The mutex acts as a barrier, but otherwise the C language's
         * machine abstraction is single-threaded. This means that the
@@ -213,14 +213,15 @@ static void initialize()
         * and the simplest way to achieve that is to just wait until
         * the second entry into the mutex. */
 
+       bool ret;
        pthread_mutex_lock(&log_construction_lock);
-               if (first) {
-                       __configure();
-                       first = false; // pop this cherry
-               } else {
+               if (first)
+                       first = !__configure();
+               else
                        is_initialized = true;
-               }
+               ret = !first;
        pthread_mutex_unlock(&log_construction_lock);
+       return ret;
 }
 
 /**
@@ -314,8 +315,10 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
         * but giving this guarantee makes everything a lot simpler as it removes
         * the risk of something suddenly becoming NULL during processing. */
        pthread_rwlock_rdlock(&log_destruction_lock);
-       initialize();
-       ret = write_to_log ? __write_to_log_critical_section(log_id, prio, tag, fmt, ap, check_should_log) : DLOG_ERROR_NOT_PERMITTED;
+               ret = !initialize() || !write_to_log
+                       ? DLOG_ERROR_NOT_PERMITTED
+                       : __write_to_log_critical_section(log_id, prio, tag, fmt, ap, check_should_log)
+               ;
        pthread_rwlock_unlock(&log_destruction_lock);
 
        return ret;
index aeee5ab..9d86f44 100644 (file)
@@ -150,7 +150,7 @@ int main()
        assert(write_to_log != wtl_pipe);
        conf_read_called = false;
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
-       assert(!conf_read_called); // no duplicate init
+       assert(conf_read_called); // should retry reinitializing
        __dlog_fini();
 
        log_config_set(&CONFIG, "backend", "Hej, zamawiam Bentoye. Czekam do 11:15.");