Revert "libdlog: initialise through attribute constructor" 15/199815/3 accepted/tizen/unified/20190218.163242 submit/tizen/20190218.011546
authorMichal Bloch <m.bloch@samsung.com>
Thu, 14 Feb 2019 11:57:21 +0000 (12:57 +0100)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Mon, 18 Feb 2019 01:13:48 +0000 (01:13 +0000)
This reverts commit a1ffe213e5c242a7e10e65e49cb50b6fae945ed8.

Multiple Tizen daemons close all descriptors and only then try
to do logging. Initialising the library on first use means that
this use case works correctly. Meanwhile, with constructor-based
initialisation, this mass close affects all dlog FDs. While in
general this won't help if somebody sends a log and then closes
FDs, it means that the most common use case (somewhat enforced
by previous dlog behaviour) is solved.

Change-Id: Ic4e72a5d88c2050bcd20f781793a563ddbc49347

include/libdlog.h
src/libdlog/log.c
src/tests/libdlog_base.c

index 6e62899..0dcf656 100644 (file)
@@ -19,5 +19,4 @@ void __update_plog(const struct log_config *conf);
 
 #ifdef UNIT_TEST
 void __dlog_fini(void);
-void __configure(void);
 #endif
index 08aaebf..22509f9 100644 (file)
@@ -38,6 +38,8 @@
 #define DEFAULT_CONFIG_DEBUGMODE 0
 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
 
+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
@@ -48,7 +50,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) = NULL;
+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(const struct log_config *conf);
 extern void __dlog_init_android(const struct log_config *conf);
@@ -62,6 +64,20 @@ static int debugmode;
 static int fatal_assert;
 static int limiter_apply_to_all_buffers;
 
+/**
+ * @brief Null handler
+ * @details Ignores a log
+ * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
+ * @param[in] prio Priority of the message.
+ * @param[in] tag The message tag, identifies the sender.
+ * @param[in] msg The contents of the message.
+ * @return DLOG_ERROR_NOT_PERMITTED
+ */
+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;
+}
+
 static void __configure_limiter(struct log_config *config)
 {
        assert(config);
@@ -140,11 +156,7 @@ void __update_plog(const struct log_config *conf)
  * @brief Configure the library
  * @details Reads relevant config values
  */
-#ifdef UNIT_TEST
-void __configure(void)
-#else
-static void __attribute__((constructor)) __configure(void)
-#endif
+static void __configure(void)
 {
        struct log_config config;
 
@@ -166,6 +178,26 @@ out:
 }
 
 /**
+ * @brief DLog init
+ * @details Initializes the library
+ */
+static bool is_initialized = false;
+static inline void __dlog_init(void)
+{
+       if (is_initialized)
+               return;
+
+       pthread_mutex_lock(&log_init_lock);
+
+       if (!is_initialized) {
+               __configure();
+               is_initialized = true;
+       }
+
+       pthread_mutex_unlock(&log_init_lock);
+}
+
+/**
  * @brief Fatal assertion
  * @details Conditionally crash the sucka who sent the log
  * @param[in] prio Priority of the log
@@ -240,8 +272,7 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
        if (ret < 0)
                return ret;
 
-       if (!write_to_log)
-               return DLOG_ERROR_NOT_PERMITTED;
+       __dlog_init();
 
        /* if limiter_apply_to_all_buffers config variable is set to 1,
         * check_should_log value does not matter and the entry is always
@@ -321,6 +352,8 @@ void __dlog_fini(void)
 {
        __log_limiter_destroy();
        __dynamic_config_destroy();
+       write_to_log = __write_to_log_null;
+       is_initialized = false;
 }
 #endif
 
index b4f1dbb..c8e42ff 100644 (file)
@@ -125,15 +125,16 @@ void tct_tests()
 
 int main()
 {
+       __dlog_fini();
+       __dlog_fini();
+
        fail_conf_read = true;
-       __configure();
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
        assert(write_to_log != wtl_android);
        assert(write_to_log != wtl_pipe);
        fail_conf_read = false;
        __dlog_fini();
 
-       __configure();
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
        assert(write_to_log != wtl_android);
        assert(write_to_log != wtl_pipe);
@@ -143,16 +144,13 @@ int main()
        __dlog_fini();
 
        log_config_set(&CONFIG, "backend", "Hej, zamawiam Bentoye. Czekam do 11:15.");
-       __configure();
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NOT_PERMITTED);
        assert(write_to_log != wtl_pipe);
        assert(write_to_log != wtl_android);
        __dlog_fini();
 
-
        log_config_set(&CONFIG, "backend", "pipe");
        log_config_set(&CONFIG, "limiter", "0");
-       __configure();
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == 0xDECC16);
        assert(!dynamic_config_called);
        assert(write_to_log == wtl_pipe);
@@ -164,7 +162,6 @@ int main()
        log_config_set(&CONFIG, "debugmode", "0");
        use_dynamic_conf = true;
        limiter_ret = 1;
-       __configure();
        assert(dlog_print(DLOG_ERROR, "tag", "msg") == 45835705);
        assert(__dlog_print(LOG_ID_MAIN, DLOG_ERROR, "tag", "msg") == 45835705);
        assert(write_to_log != wtl_pipe); // ceci n'est pas une pipe
@@ -187,16 +184,13 @@ int main()
        __dlog_fini();
 
        log_config_set(&CONFIG, "enable_system", "0");
-       __configure();
        assert(__dlog_print(LOG_ID_SYSTEM, DLOG_ERROR, "tag", "msg") == DLOG_ERROR_NONE);
        __dlog_fini();
 
        fail_snprintf = true;
-       __configure();
        assert(__dlog_print(LOG_ID_SYSTEM, DLOG_ERROR, "tag", "msg") == 45835705);
        fail_snprintf = false;
        __dlog_fini();
 
-       __configure();
        tct_tests();
 }