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