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