1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <sc1.lim@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
44 #include <sys/syscall.h>
50 #include "tdm_macro.h"
52 #define LOG_MAX_LEN 4076
54 #define COLOR_RED "\x1b[31m" /* for error */
55 #define COLOR_YELLOW "\x1b[33m" /* for warning */
56 #define COLOR_GREEN "\x1b[32m" /* for info */
57 #define COLOR_RESET "\x1b[0m"
62 static unsigned int dlog_enable = 0;
63 static unsigned int color_enable = 1;
65 static unsigned int assert_level = TDM_LOG_LEVEL_NONE;
67 static unsigned int log_lock_init;
68 static pthread_mutex_t log_lock;
70 unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
72 static int stdout_fd = -1;
75 tdm_log_enable_color(unsigned int enable)
77 color_enable = enable;
81 tdm_log_enable_dlog(unsigned int enable)
83 const char *debug = getenv("TDM_DLOG");
84 if (debug && strstr(debug, "1"))
91 tdm_log_enable_debug(unsigned int enable)
94 tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
96 tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
100 tdm_log_set_debug_level(int level)
102 tdm_log_debug_level = level;
106 tdm_log_set_assert_level(int level)
108 assert_level = level;
112 tdm_log_set_path(const char *path)
115 if (stdout_fd != -1) {
117 close(STDOUT_FILENO);
118 dup2(stdout_fd, STDOUT_FILENO);
123 char fd_name[TDM_PATH_LEN];
127 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
129 log_fl = fopen(fd_name, "a");
131 TDM_ERR("failed: open file(%s)\n", fd_name);
135 if (stdout_fd == -1) {
137 stdout_fd = dup(STDOUT_FILENO);
139 TDM_ERR("dup failed: %m\n");
145 setvbuf(log_fl, NULL, _IOLBF, 512);
146 log_fd = fileno(log_fl);
148 close(STDOUT_FILENO);
149 dup2(log_fd, STDOUT_FILENO);
155 tdm_log_print(int level, const char *fmt, ...)
159 if (!log_lock_init) {
161 pthread_mutex_init(&log_lock, NULL);
165 if (level > tdm_log_debug_level)
169 log_priority dlog_prio;
171 case TDM_LOG_LEVEL_ERR:
172 dlog_prio = DLOG_ERROR;
174 case TDM_LOG_LEVEL_WRN:
175 dlog_prio = DLOG_WARN;
177 case TDM_LOG_LEVEL_INFO:
178 dlog_prio = DLOG_INFO;
180 case TDM_LOG_LEVEL_DBG:
181 dlog_prio = DLOG_DEBUG;
187 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
190 char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"};
191 char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
193 pthread_mutex_lock(&log_lock);
196 printf("%s", color[level]);
197 printf("[%s]", lvl_str[level]);
204 pthread_mutex_unlock(&log_lock);
207 assert(level > assert_level);
213 pthread_mutex_trylock(&log_lock);
214 pthread_mutex_unlock(&log_lock);