40a846ab964b20d4d03cf68de3bf7a778222a0a9
[platform/core/uifw/libtdm.git] / common / tdm_log.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <time.h>
43 #include <assert.h>
44 #include <sys/syscall.h>
45 #include <dlog.h>
46 #include <pthread.h>
47
48 #include "tdm.h"
49 #include "tdm_log.h"
50 #include "tdm_macro.h"
51
52 #define LOG_MAX_LEN    4076
53
54 #define COLOR_RED "\x1b[31m"      /* for error */
55 #define COLOR_YELLOW "\x1b[33m"   /* for warning */
56 #define COLOR_GREEN "\x1b[32m"    /* for info */
57 #define COLOR_RESET "\x1b[0m"
58
59 #undef LOG_TAG
60 #define LOG_TAG "TDM"
61
62 static unsigned int dlog_enable = 1;
63 static unsigned int color_enable = 1;
64
65 static unsigned int assert_level = TDM_LOG_LEVEL_NONE;
66
67 static unsigned int log_lock_init;
68 static pthread_mutex_t log_lock;
69
70 unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
71
72 static int stdout_fd = -1;
73
74 EXTERN void
75 tdm_log_enable_color(unsigned int enable)
76 {
77         color_enable = enable;
78         TDM_INFO("color_enable: %d", color_enable);
79 }
80
81 EXTERN void
82 tdm_log_enable_dlog(unsigned int enable)
83 {
84         const char *str = getenv("TDM_DLOG");
85         if (str)
86                 enable = (str[0] == '1') ? 1 : 0;
87         dlog_enable = enable;
88         TDM_INFO("dlog_enable: %d", dlog_enable);
89 }
90
91 EXTERN void
92 tdm_log_set_debug_level(int level)
93 {
94         const char *str = getenv("TDM_DEBUG_LEVEL");
95         if (str)
96                 level = str[0] - '0';
97         tdm_log_debug_level = level;
98         TDM_INFO("debug_level: %d", tdm_log_debug_level);
99 }
100
101 EXTERN void
102 tdm_log_set_assert_level(int level)
103 {
104         const char *str = getenv("TDM_ASSERT_LEVEL");
105         if (str)
106                 level = str[0] - '0';
107         assert_level = level;
108         TDM_INFO("assert_level: %d", assert_level);
109 }
110
111 EXTERN void
112 tdm_log_set_path(const char *path)
113 {
114         TDM_INFO("log_path: %s", path);
115
116         if (!path) {
117                 if (stdout_fd != -1) {
118                         fflush(stdout);
119                         close(STDOUT_FILENO);
120                         dup2(stdout_fd, STDOUT_FILENO);
121                         close(stdout_fd);
122                         stdout_fd = -1;
123                 }
124         } else {
125                 char fd_name[TDM_PATH_LEN];
126                 int  log_fd = -1;
127                 FILE *log_fl;
128
129                 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
130
131                 log_fl = fopen(fd_name, "a");
132                 if (!log_fl) {
133                         TDM_ERR("failed: open file(%s)\n", fd_name);
134                         return;
135                 }
136
137                 if (stdout_fd == -1) {
138                         fflush(stdout);
139                         stdout_fd = dup(STDOUT_FILENO);
140                         if (stdout_fd < 0) {
141                                 TDM_ERR("dup failed: %m\n");
142                                 fclose(log_fl);
143                                 return;
144                         }
145                 }
146
147                 setvbuf(log_fl, NULL, _IOLBF, 512);
148                 log_fd = fileno(log_fl);
149
150                 close(STDOUT_FILENO);
151                 dup2(log_fd, STDOUT_FILENO);
152                 fclose(log_fl);
153                 TDM_INFO("log_path: %s done", path);
154         }
155 }
156
157 static void
158 _tdm_log_vprint_stdout(int level, const char *fmt, va_list ap)
159 {
160         if (!log_lock_init) {
161                 log_lock_init = 1;
162                 pthread_mutex_init(&log_lock, NULL);
163         }
164
165         if (level > tdm_log_debug_level)
166                 return;
167
168         char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"};
169         char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
170
171         pthread_mutex_lock(&log_lock);
172
173         if (color_enable)
174                 printf("%s", color[level]);
175         printf("[%s]", lvl_str[level]);
176         if (color_enable)
177                 printf(COLOR_RESET);
178         vprintf(fmt, ap);
179         printf("\n");
180         pthread_mutex_unlock(&log_lock);
181 }
182
183
184 EXTERN void
185 tdm_log_printf(int level, const char *fmt, ...)
186 {
187         va_list arg;
188         va_start(arg, fmt);
189         _tdm_log_vprint_stdout(level, fmt, arg);
190         va_end(arg);
191 }
192
193 EXTERN void
194 tdm_log_print(int level, const char *fmt, ...)
195 {
196         va_list arg;
197
198         if (level > tdm_log_debug_level)
199                 return;
200
201         if (dlog_enable) {
202                 log_priority dlog_prio;
203                 switch (level) {
204                 case TDM_LOG_LEVEL_ERR:
205                         dlog_prio = DLOG_ERROR;
206                         break;
207                 case TDM_LOG_LEVEL_WRN:
208                         dlog_prio = DLOG_WARN;
209                         break;
210                 case TDM_LOG_LEVEL_INFO:
211                         dlog_prio = DLOG_INFO;
212                         break;
213                 case TDM_LOG_LEVEL_DBG:
214                         dlog_prio = DLOG_DEBUG;
215                         break;
216                 default:
217                         return;
218                 }
219                 va_start(arg, fmt);
220                 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
221                 va_end(arg);
222         } else {
223                 va_start(arg, fmt);
224                 _tdm_log_vprint_stdout(level, fmt, arg);
225                 va_end(arg);
226         }
227
228         assert(level > assert_level);
229 }
230
231 EXTERN void
232 tdm_log_reset(void)
233 {
234         pthread_mutex_unlock(&log_lock);
235 }