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