log: check log level
[platform/core/uifw/libtdm.git] / common / tdm_log.c
index 5e1ab2c..2f7152b 100644 (file)
@@ -43,6 +43,7 @@
 #include <assert.h>
 #include <sys/syscall.h>
 #include <dlog.h>
+#include <pthread.h>
 
 #include "tdm.h"
 #include "tdm_log.h"
 #undef LOG_TAG
 #define LOG_TAG "TDM"
 
-static unsigned int dlog_enable;
+static unsigned int dlog_enable = 1;
 static unsigned int color_enable = 1;
-static unsigned int debug_level = TDM_LOG_LEVEL_INFO;
 
 static unsigned int need_check_env = 1;
 
+static unsigned int log_lock_init;
+static pthread_mutex_t log_lock;
+
+unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
+
 static void
 _tdm_log_check_env(void)
 {
@@ -74,15 +79,15 @@ _tdm_log_check_env(void)
 
        str = getenv("TDM_DEBUG_LEVEL");
        if (str)
-               debug_level = strtol(str, &end, 10);
+               tdm_log_debug_level = strtol(str, &end, 10);
 
        str = getenv("TDM_DEBUG");
        if (str && (strstr(str, "1")))
-               debug_level = TDM_LOG_LEVEL_DBG;
+               tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
 
        str = getenv("TDM_DLOG");
-       if (str && (strstr(str, "1")))
-               dlog_enable = 1;
+       if (str && (strstr(str, "0")))
+               dlog_enable = 0;
 }
 
 EXTERN void
@@ -101,15 +106,15 @@ EXTERN void
 tdm_log_enable_debug(unsigned int enable)
 {
        if (enable)
-               debug_level = TDM_LOG_LEVEL_DBG;
+               tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
        else
-               debug_level = TDM_LOG_LEVEL_INFO;
+               tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
 }
 
 EXTERN void
 tdm_log_set_debug_level(int level)
 {
-       debug_level = level;
+       tdm_log_debug_level = level;
 }
 
 EXTERN void
@@ -117,12 +122,18 @@ tdm_log_print(int level, const char *fmt, ...)
 {
        va_list arg;
 
+       if (!log_lock_init) {
+               log_lock_init = 1;
+               pthread_mutex_init(&log_lock, NULL);
+
+       }
+
        if (need_check_env) {
                need_check_env = 0;
                _tdm_log_check_env();
        }
 
-       if (level > debug_level)
+       if (level > tdm_log_debug_level)
                return;
 
        if (dlog_enable) {
@@ -144,7 +155,7 @@ tdm_log_print(int level, const char *fmt, ...)
                        return;
                }
                va_start(arg, fmt);
-               dlog_vprint(dlog_prio, LOG_TAG, fmt, arg);
+               __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
                va_end(arg);
        } else {
                struct timespec ts;
@@ -153,6 +164,8 @@ tdm_log_print(int level, const char *fmt, ...)
 
                clock_gettime(CLOCK_MONOTONIC, &ts);
 
+               pthread_mutex_lock(&log_lock);
+
                if (color_enable)
                        printf("%s", color[level]);
                printf("[%s]", lvl_str[level]);
@@ -162,6 +175,8 @@ tdm_log_print(int level, const char *fmt, ...)
                va_start(arg, fmt);
                vprintf(fmt, arg);
                va_end(arg);
+
+               pthread_mutex_unlock(&log_lock);
        }
 
 #ifdef TDM_CONFIG_ASSERT