fix stringio truncation warning
[platform/core/uifw/libtdm.git] / src / tdm_config.c
index 6686769..95f5119 100644 (file)
@@ -9,7 +9,7 @@
  *          Taeheon Kim <th908.kim@samsung.com>,
  *          YoungJun Cho <yj44.cho@samsung.com>,
  *          SooChan Lim <sc1.lim@samsung.com>,
- *          Boram Park <sc1.lim@samsung.com>
+ *          Boram Park <boram1288.park@samsung.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
@@ -49,8 +49,9 @@
 #define TDM_CONFIG_FILENAME "tdm.ini"
 #define TDM_CONFIG_GENERAL_SECTION "general"
 
+static pthread_mutex_t g_dic_lock = PTHREAD_MUTEX_INITIALIZER;
 static dictionary *g_dic = NULL;
-static pthread_mutex_t g_dic_lock;
+static int init_dic = 0;
 
 static int
 _tdm_config_check_file_owner(const char *filepath)
@@ -95,67 +96,86 @@ _tdm_config_load_file(const char *dir, const char *filename)
        return dic;
 }
 
-static dictionary *
-_tdm_config_load(void)
+static void
+_tdm_config_check_logs(void)
 {
-       dictionary *dic = NULL;
+       const char *path;
+       int level;
 
-#if 0
-       /* not allowed: try to read from RW directory */
-       dic = _tdm_config_load_file(TDM_SYSCONF_PATH, TDM_CONFIG_FILENAME);
-#endif
+       pthread_mutex_unlock(&g_dic_lock);
 
-       if (!dic) {
-               /* try to read from RO directory */
-               dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
+       level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_LOG_LEVEL, -1);
+       if (level == -1) {
+               const char *str = getenv("TDM_DEBUG_LEVEL");
+               if (str)
+                       level = str[0] - '0';
+               else
+                       level = 3;
+       }
+       tdm_log_set_debug_level(level);
+
+       level = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_ASSERT_LEVEL, -1);
+       if (level == -1) {
+               const char *str = getenv("TDM_ASSERT_LEVEL");
+               if (str)
+                       level = str[0] - '0';
+               else
+                       level = 0;
+       }
+       tdm_log_set_assert_level(level);
+
+       /* if TDM_CONFIG_KEY_DEBUG_LOG_PATH is setted, TDM_CONFIG_KEY_DEBUG_DLOG will be ignored. */
+       path = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_LOG_PATH, NULL);
+       if (path) {
+               tdm_log_enable_dlog(0);
+               tdm_log_set_path(path);
+       } else {
+               int dlog = tdm_config_get_int(TDM_CONFIG_KEY_DEBUG_DLOG, -1);
+               if (dlog == -1) {
+                       const char *str = getenv("TDM_DLOG");
+                       if (str)
+                               dlog = (str[0] == '1') ? 1 : 0;
+                       else
+                               dlog = 1;
+               }
+               tdm_log_enable_dlog(dlog);
        }
 
-       return dic;
+       pthread_mutex_lock(&g_dic_lock);
 }
 
-INTERN tdm_error
-tdm_config_init(void)
+static void
+_tdm_config_check_init(void)
 {
-       if (g_dic) {
-               TDM_ERR("init failed: twice");
-               return TDM_ERROR_OPERATION_FAILED;
-       }
+       if (init_dic)
+               return;
 
-       if (pthread_mutex_init(&g_dic_lock, NULL)) {
-               TDM_ERR("mutex init failed: %m");
-               return TDM_ERROR_OUT_OF_MEMORY;
-       }
+       init_dic = 1;
 
-       g_dic = _tdm_config_load();
-       if (!g_dic) {
-               if (!getenv("TDM_NO_CONFIG")) {
-                       TDM_ERR("Loading config file failed!!");
-                       pthread_mutex_destroy(&g_dic_lock);
-                       return TDM_ERROR_NONE;
-               }
-       }
+       g_dic = _tdm_config_load_file(TDM_DATA_PATH, TDM_CONFIG_FILENAME);
 
-       TDM_INFO("tdm config init done (%p)", g_dic);
+       _tdm_config_check_logs();
 
-       return TDM_ERROR_NONE;
+       TDM_INFO("tdm config init %s (%p)", (g_dic) ? "successed" : "failed", g_dic);
 }
 
 INTERN void
 tdm_config_deinit(void)
 {
+       pthread_mutex_lock(&g_dic_lock);
+
        if (!g_dic) {
+               pthread_mutex_unlock(&g_dic_lock);
                return;
        }
 
        iniparser_freedict(g_dic);
        g_dic = NULL;
-
-       /* we don't need to lock/unlock here because we come here
-        * after tdm_thread has been destroyed
-        */
-       pthread_mutex_destroy(&g_dic_lock);
+       init_dic = 0;
 
        TDM_INFO("tdm config deinit done");
+
+       pthread_mutex_unlock(&g_dic_lock);
 }
 
 static const char*
@@ -183,7 +203,7 @@ _tdm_config_get_string_internal(dictionary *dic, const char *key, const char *de
        return result;
 }
 
-INTERN int
+EXTERN int
 tdm_config_get_int(const char *key, int default_value)
 {
        const char *result;
@@ -191,12 +211,15 @@ tdm_config_get_int(const char *key, int default_value)
 
        TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
 
+       pthread_mutex_lock(&g_dic_lock);
+
+       _tdm_config_check_init();
        if (!g_dic) {
                TDM_INFO("%s = %d: default", key, default_value);
+               pthread_mutex_unlock(&g_dic_lock);
                return default_value;
        }
 
-       pthread_mutex_lock(&g_dic_lock);
        result = _tdm_config_get_string_internal(g_dic, key, NULL);
        pthread_mutex_unlock(&g_dic_lock);
 
@@ -212,19 +235,22 @@ tdm_config_get_int(const char *key, int default_value)
        return value;
 }
 
-INTERN const char*
+EXTERN const char*
 tdm_config_get_string(const char *key, const char *default_value)
 {
        const char *result;
 
        TDM_RETURN_VAL_IF_FAIL(key != NULL, default_value);
 
+       pthread_mutex_lock(&g_dic_lock);
+
+       _tdm_config_check_init();
        if (!g_dic) {
                TDM_INFO("%s = %s: default", key, default_value);
+               pthread_mutex_unlock(&g_dic_lock);
                return default_value;
        }
 
-       pthread_mutex_lock(&g_dic_lock);
        result = _tdm_config_get_string_internal(g_dic, key, default_value);
        pthread_mutex_unlock(&g_dic_lock);
 
@@ -233,7 +259,7 @@ tdm_config_get_string(const char *key, const char *default_value)
        return result;
 }
 
-INTERN tdm_error
+EXTERN tdm_error
 tdm_config_set_int(const char *key, int value)
 {
        char temp[TDM_NAME_LEN];
@@ -241,15 +267,19 @@ tdm_config_set_int(const char *key, int value)
 
        TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
 
+       snprintf(temp, sizeof temp, "%d", value);
+
+       pthread_mutex_lock(&g_dic_lock);
+
+       _tdm_config_check_init();
        if (!g_dic) {
                TDM_INFO("%s = %d set failed", key, value);
+               pthread_mutex_unlock(&g_dic_lock);
                return TDM_ERROR_BAD_REQUEST;
        }
 
-       snprintf(temp, sizeof temp, "%d", value);
-
-       pthread_mutex_lock(&g_dic_lock);
        ret = iniparser_set(g_dic, key, (const char*)temp);
+
        pthread_mutex_unlock(&g_dic_lock);
 
        TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);
@@ -259,20 +289,24 @@ tdm_config_set_int(const char *key, int value)
        return TDM_ERROR_NONE;
 }
 
-INTERN tdm_error
+EXTERN tdm_error
 tdm_config_set_string(const char *key, const char *value)
 {
        int ret;
 
        TDM_RETURN_VAL_IF_FAIL(key != NULL, TDM_ERROR_INVALID_PARAMETER);
 
+       pthread_mutex_lock(&g_dic_lock);
+
+       _tdm_config_check_init();
        if (!g_dic) {
                TDM_INFO("%s = %s set failed", key, value);
+               pthread_mutex_unlock(&g_dic_lock);
                return TDM_ERROR_BAD_REQUEST;
        }
 
-       pthread_mutex_lock(&g_dic_lock);
        ret = iniparser_set(g_dic, key, value);
+
        pthread_mutex_unlock(&g_dic_lock);
 
        TDM_RETURN_VAL_IF_FAIL(ret == 0, TDM_ERROR_OPERATION_FAILED);