X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=common%2Ftdm_log.c;h=975eb19b6bd980709f3e42f0e19eab24af1ac92a;hb=0a37ccf8b2a60d62b7286e8f0debcfccb927e4e1;hp=593dc5ee75da63f9a9fc6d88307c6b036a9c82c3;hpb=5bd6c8bcbb65615ce395ae444d3e93daa1217f5a;p=platform%2Fcore%2Fuifw%2Flibtdm.git diff --git a/common/tdm_log.c b/common/tdm_log.c index 593dc5e..975eb19 100644 --- a/common/tdm_log.c +++ b/common/tdm_log.c @@ -9,7 +9,7 @@ * Taeheon Kim , * YoungJun Cho , * SooChan Lim , - * Boram Park + * Boram Park * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the @@ -49,8 +49,6 @@ #include "tdm_log.h" #include "tdm_macro.h" -//#define TDM_CONFIG_ASSERT - #define LOG_MAX_LEN 4076 #define COLOR_RED "\x1b[31m" /* for error */ @@ -63,76 +61,132 @@ 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 assert_level = TDM_LOG_LEVEL_NONE; static unsigned int log_lock_init; static pthread_mutex_t log_lock; -static void -_tdm_log_check_env(void) -{ - const char *str; - char *end; - - str = getenv("TDM_DEBUG_LEVEL"); - if (str) - debug_level = strtol(str, &end, 10); +unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO; - str = getenv("TDM_DEBUG"); - if (str && (strstr(str, "1"))) - debug_level = TDM_LOG_LEVEL_DBG; - - str = getenv("TDM_DLOG"); - if (str && (strstr(str, "0"))) - dlog_enable = 0; -} +static int stdout_fd = -1; EXTERN void tdm_log_enable_color(unsigned int enable) { color_enable = enable; + TDM_INFO("color_enable: %d", color_enable); } EXTERN void tdm_log_enable_dlog(unsigned int enable) { dlog_enable = enable; + TDM_INFO("dlog_enable: %d", dlog_enable); } EXTERN void -tdm_log_enable_debug(unsigned int enable) +tdm_log_set_debug_level(int level) { - if (enable) - debug_level = TDM_LOG_LEVEL_DBG; - else - debug_level = TDM_LOG_LEVEL_INFO; + tdm_log_debug_level = level; + TDM_INFO("debug_level: %d", tdm_log_debug_level); } EXTERN void -tdm_log_set_debug_level(int level) +tdm_log_set_assert_level(int level) { - debug_level = level; + assert_level = level; + TDM_INFO("assert_level: %d", assert_level); } EXTERN void -tdm_log_print(int level, const char *fmt, ...) +tdm_log_set_path(const char *path) { - va_list arg; + TDM_INFO("log_path: %s", path); + + if (!path) { + if (stdout_fd != -1) { + fflush(stdout); + close(STDOUT_FILENO); + dup2(stdout_fd, STDOUT_FILENO); + close(stdout_fd); + stdout_fd = -1; + } + } else { + char fd_name[TDM_PATH_LEN]; + int log_fd = -1; + FILE *log_fl; + + snprintf(fd_name, TDM_PATH_LEN, "%s", path); + log_fl = fopen(fd_name, "a"); + if (!log_fl) { + TDM_ERR("failed: open file(%s)\n", fd_name); + return; + } + + if (stdout_fd == -1) { + fflush(stdout); + stdout_fd = dup(STDOUT_FILENO); + if (stdout_fd < 0) { + TDM_ERR("dup failed: %m\n"); + fclose(log_fl); + return; + } + } + + setvbuf(log_fl, NULL, _IOLBF, 512); + log_fd = fileno(log_fl); + + close(STDOUT_FILENO); + dup2(log_fd, STDOUT_FILENO); + fclose(log_fl); + TDM_INFO("log_path: %s done", path); + } +} + +static void +_tdm_log_vprint_stdout(int level, const char *fmt, va_list ap) +{ 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 > tdm_log_debug_level) + return; + + char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"}; + char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET}; - if (level > debug_level) + pthread_mutex_lock(&log_lock); + + if (color_enable) + printf("%s", color[level]); + printf("[%s]", lvl_str[level]); + if (color_enable) + printf(COLOR_RESET); + vprintf(fmt, ap); + printf("\n"); + pthread_mutex_unlock(&log_lock); +} + + +EXTERN void +tdm_log_printf(int level, const char *fmt, ...) +{ + va_list arg; + va_start(arg, fmt); + _tdm_log_vprint_stdout(level, fmt, arg); + va_end(arg); +} + +EXTERN void +tdm_log_print(int level, const char *fmt, ...) +{ + va_list arg; + + if (level > tdm_log_debug_level) return; if (dlog_enable) { @@ -154,32 +208,19 @@ 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; - char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"}; - char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET}; - - clock_gettime(CLOCK_MONOTONIC, &ts); - - pthread_mutex_lock(&log_lock); - - if (color_enable) - printf("%s", color[level]); - printf("[%s]", lvl_str[level]); - if (color_enable) - printf(COLOR_RESET); - printf("[%d.%06d]", (int)ts.tv_sec, (int)ts.tv_nsec / 1000); va_start(arg, fmt); - vprintf(fmt, arg); + _tdm_log_vprint_stdout(level, fmt, arg); va_end(arg); - - pthread_mutex_unlock(&log_lock); } -#ifdef TDM_CONFIG_ASSERT - if (level < 3) - assert(0); -#endif + assert(level > assert_level); +} + +EXTERN void +tdm_log_reset(void) +{ + pthread_mutex_unlock(&log_lock); }