Revert "Revert "libdlog: initialise through attribute constructor"" 40/200240/2
authorHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 20 Feb 2019 11:18:28 +0000 (11:18 +0000)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 21 Feb 2019 17:33:36 +0000 (17:33 +0000)
This reverts commit b6fc587b29893517472bb84ad7e98a8ed212e2e8.

Change-Id: I4c2dc6b27dbeb486490df4b98d116a310137662f

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

index 0dcf656..6e62899 100644 (file)
@@ -19,4 +19,5 @@ void __update_plog(const struct log_config *conf);
 
 #ifdef UNIT_TEST
 void __dlog_fini(void);
+void __configure(void);
 #endif
index 22509f9..08aaebf 100644 (file)
@@ -38,8 +38,6 @@
 #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
@@ -50,7 +48,7 @@ static int __write_to_log_null(log_id_t, log_priority, const char *, const char
  * @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) = __write_to_log_null;
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = 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);
@@ -64,20 +62,6 @@ 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);
@@ -156,7 +140,11 @@ void __update_plog(const struct log_config *conf)
  * @brief Configure the library
  * @details Reads relevant config values
  */
-static void __configure(void)
+#ifdef UNIT_TEST
+void __configure(void)
+#else
+static void __attribute__((constructor)) __configure(void)
+#endif
 {
        struct log_config config;
 
@@ -178,26 +166,6 @@ 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
@@ -272,7 +240,8 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
        if (ret < 0)
                return ret;
 
-       __dlog_init();
+       if (!write_to_log)
+               return DLOG_ERROR_NOT_PERMITTED;
 
        /* if limiter_apply_to_all_buffers config variable is set to 1,
         * check_should_log value does not matter and the entry is always
@@ -352,8 +321,6 @@ void __dlog_fini(void)
 {
        __log_limiter_destroy();
        __dynamic_config_destroy();
-       write_to_log = __write_to_log_null;
-       is_initialized = false;
 }
 #endif
 
index c8e42ff..b4f1dbb 100644 (file)
@@ -125,16 +125,15 @@ 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);
@@ -144,13 +143,16 @@ 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);
@@ -162,6 +164,7 @@ 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
@@ -184,13 +187,16 @@ 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();
 }