change the log macro at all files
[platform/core/uifw/libtbm.git] / src / tbm_log.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2018 Samsung Electronics co., Ltd. All Rights Reserved.
6
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>
9
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:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
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.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "tbm_bufmgr_int.h"
37 #include "tbm_log.h"
38
39 #define TBM_PATH_LEN        1024
40
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"
45
46 #undef LOG_TAG
47 #define LOG_TAG "TBM"
48
49 static unsigned int dlog_enable = 1;
50 static unsigned int color_enable = 1;
51
52 static unsigned int assert_level = TBM_LOG_LEVEL_NONE;
53
54 static unsigned int log_lock_init;
55 static pthread_mutex_t log_lock;
56
57 unsigned int tbm_log_debug_level = TBM_LOG_LEVEL_INFO;
58
59 static int stdout_fd = -1;
60
61 void
62 tbm_log_enable_color(unsigned int enable)
63 {
64         color_enable = enable;
65 }
66
67 void
68 tbm_log_enable_dlog(unsigned int enable)
69 {
70         const char *str = getenv("TBM_DLOG");
71         if (str)
72                 enable = (str[0] == '1') ? 1 : 0;
73         dlog_enable = enable;
74 }
75
76 void
77 tbm_log_enable_debug(unsigned int enable)
78 {
79         if (enable)
80                 tbm_log_debug_level = TBM_LOG_LEVEL_DBG;
81         else
82                 tbm_log_debug_level = TBM_LOG_LEVEL_INFO;
83 }
84
85 void
86 tbm_log_set_debug_level(int level)
87 {
88         tbm_log_debug_level = level;
89 }
90
91 void
92 tbm_log_set_assert_level(int level)
93 {
94         assert_level = level;
95 }
96
97 void
98 tbm_log_set_path(const char *path)
99 {
100         if (!path) {
101                 if (stdout_fd != -1) {
102                         fflush(stdout);
103                         close(STDOUT_FILENO);
104                         dup2(stdout_fd, STDOUT_FILENO);
105                         close(stdout_fd);
106                         stdout_fd = -1;
107                 }
108         } else {
109                 char fd_name[TBM_PATH_LEN];
110                 int  log_fd = -1;
111                 FILE *log_fl;
112
113                 snprintf(fd_name, TBM_PATH_LEN, "%s", path);
114
115                 log_fl = fopen(fd_name, "a");
116                 if (!log_fl) {
117                         TBM_ERR("failed: open file(%s)\n", fd_name);
118                         return;
119                 }
120
121                 if (stdout_fd == -1) {
122                         fflush(stdout);
123                         stdout_fd = dup(STDOUT_FILENO);
124                         if (stdout_fd < 0) {
125                                 TBM_ERR("dup failed: %m\n");
126                                 fclose(log_fl);
127                                 return;
128                         }
129                 }
130
131                 setvbuf(log_fl, NULL, _IOLBF, 512);
132                 log_fd = fileno(log_fl);
133
134                 close(STDOUT_FILENO);
135                 dup2(log_fd, STDOUT_FILENO);
136                 fclose(log_fl);
137         }
138 }
139
140 static void
141 _tbm_log_vprint_stdout(int level, const char *fmt, va_list arg)
142 {
143         char *lvl_str[] = {"TBM_NON", "TBM_ERR", "TBM_WRN", "TBM_INF", "TBM_DBG"};
144         char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
145
146         if (!log_lock_init) {
147                 log_lock_init = 1;
148                 pthread_mutex_init(&log_lock, NULL);
149         }
150
151         if (level > tbm_log_debug_level)
152                 return;
153
154         pthread_mutex_lock(&log_lock);
155
156         if (color_enable)
157                 printf("%s", color[level]);
158         printf("[%s]", lvl_str[level]);
159         if (color_enable)
160                 printf(COLOR_RESET);
161         vprintf(fmt, arg);
162         printf("\n");
163         pthread_mutex_unlock(&log_lock);
164 }
165
166 void
167 tbm_log_print_stdout(int level, const char *fmt, ...)
168 {
169         va_list arg;
170         va_start(arg, fmt);
171         _tbm_log_vprint_stdout(level, fmt, arg);
172         va_end(arg);
173 }
174
175 static void
176 _tbm_log_dlog_print(int level, const char *fmt, va_list arg)
177 {
178         log_priority dlog_prio;
179
180         switch (level) {
181         case TBM_LOG_LEVEL_ERR:
182                 dlog_prio = DLOG_ERROR;
183                 break;
184         case TBM_LOG_LEVEL_WRN:
185                 dlog_prio = DLOG_WARN;
186                 break;
187         case TBM_LOG_LEVEL_INFO:
188                 dlog_prio = DLOG_INFO;
189                 break;
190         case TBM_LOG_LEVEL_DBG:
191                 dlog_prio = DLOG_DEBUG;
192                 break;
193         default:
194                 return;
195         }
196         __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
197 }
198
199 void
200 tbm_log_print(int level, const char *fmt, ...)
201 {
202         va_list arg;
203
204         if (!log_lock_init) {
205                 log_lock_init = 1;
206                 pthread_mutex_init(&log_lock, NULL);
207         }
208
209         if (level > tbm_log_debug_level)
210                 return;
211
212         if (dlog_enable) {
213                 va_start(arg, fmt);
214                 _tbm_log_dlog_print(level, fmt, arg);
215                 va_end(arg);
216         } else {
217                 va_start(arg, fmt);
218                 _tbm_log_vprint_stdout(level, fmt, arg);
219                 va_end(arg);
220         }
221
222         assert(level > assert_level);
223 }
224
225 void
226 tbm_log_reset(void)
227 {
228         pthread_mutex_trylock(&log_lock);
229         pthread_mutex_unlock(&log_lock);
230 }