1 /**************************************************************************
5 Copyright 2018 Samsung Electronics co., Ltd. All Rights Reserved.
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
36 #include "tbm_bufmgr_int.h"
39 #define TBM_PATH_LEN 1024
41 #define COLOR_RED "\x1b[31m" /* for error */
42 #define COLOR_YELLOW "\x1b[33m" /* for warning */
43 #define COLOR_GREEN "\x1b[32m" /* for info */
44 #define COLOR_RESET "\x1b[0m"
51 static unsigned int dlog_enable = 1;
54 static unsigned int dlog_enable = 0;
56 static unsigned int color_enable = 1;
58 static unsigned int assert_level = TBM_LOG_LEVEL_NONE;
60 static unsigned int log_lock_init;
61 static pthread_mutex_t log_lock;
63 unsigned int tbm_log_debug_level = TBM_LOG_LEVEL_INFO;
65 static int stdout_fd = -1;
68 tbm_log_enable_color(unsigned int enable)
70 color_enable = enable;
74 tbm_log_enable_dlog(unsigned int enable)
80 tbm_log_set_debug_level(int level)
82 tbm_log_debug_level = level;
86 tbm_log_set_assert_level(int level)
92 tbm_log_set_path(const char *path)
95 if (stdout_fd != -1) {
98 dup2(stdout_fd, STDOUT_FILENO);
103 char fd_name[TBM_PATH_LEN];
107 snprintf(fd_name, TBM_PATH_LEN, "%s", path);
109 log_fl = fopen(fd_name, "a");
111 TBM_ERR("failed: open file(%s)\n", fd_name);
115 if (stdout_fd == -1) {
117 stdout_fd = dup(STDOUT_FILENO);
119 TBM_ERR("dup failed: %m\n");
125 setvbuf(log_fl, NULL, _IOLBF, 512);
126 log_fd = fileno(log_fl);
128 close(STDOUT_FILENO);
129 dup2(log_fd, STDOUT_FILENO);
135 _tbm_log_vprint_stdout(int level, const char *fmt, va_list arg)
137 char *lvl_str[] = {"TBM_NON", "TBM_ERR", "TBM_WRN", "TBM_INF", "TBM_DBG"};
138 char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
140 if (!log_lock_init) {
142 pthread_mutex_init(&log_lock, NULL);
145 if (level > tbm_log_debug_level)
148 pthread_mutex_lock(&log_lock);
151 printf("%s", color[level]);
152 printf("[%s]", lvl_str[level]);
157 pthread_mutex_unlock(&log_lock);
161 tbm_log_print_stdout(int level, const char *fmt, ...)
165 _tbm_log_vprint_stdout(level, fmt, arg);
171 _tbm_log_dlog_print(int level, const char *fmt, va_list arg)
173 log_priority dlog_prio;
176 case TBM_LOG_LEVEL_ERR:
177 dlog_prio = DLOG_ERROR;
179 case TBM_LOG_LEVEL_WRN:
180 dlog_prio = DLOG_WARN;
182 case TBM_LOG_LEVEL_INFO:
183 dlog_prio = DLOG_INFO;
185 case TBM_LOG_LEVEL_DBG:
186 dlog_prio = DLOG_DEBUG;
191 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
196 tbm_log_print(int level, const char *fmt, ...)
200 if (!log_lock_init) {
202 pthread_mutex_init(&log_lock, NULL);
205 if (level > tbm_log_debug_level)
211 _tbm_log_dlog_print(level, fmt, arg);
218 _tbm_log_vprint_stdout(level, fmt, arg);
222 assert(level > assert_level);