Fix invalid assignment of sizeof
[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 <boram1288.park@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 <pthread.h>
46
47 #include "tdm.h"
48 #include "tdm_log.h"
49 #include "tdm_macro.h"
50
51 #define LOG_MAX_LEN    4076
52
53 #define COLOR_RED "\x1b[31m"      /* for error */
54 #define COLOR_YELLOW "\x1b[33m"   /* for warning */
55 #define COLOR_GREEN "\x1b[32m"    /* for info */
56 #define COLOR_RESET "\x1b[0m"
57
58 #undef LOG_TAG
59 #define LOG_TAG "TDM"
60
61 #if ENABLE_DLOG
62 #include <dlog.h>
63 static unsigned int dlog_enable = 1;
64 #else
65 #include <stdarg.h>
66 static unsigned int dlog_enable = 0;
67 #endif
68 static unsigned int color_enable = 1;
69
70 static unsigned int assert_level = TDM_LOG_LEVEL_NONE;
71
72 static unsigned int log_lock_init;
73 static pthread_mutex_t log_lock;
74
75 unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
76
77 static int stdout_fd = -1;
78
79 EXTERN void
80 tdm_log_enable_color(unsigned int enable)
81 {
82         color_enable = enable;
83         TDM_INFO("color_enable: %d", color_enable);
84 }
85
86 EXTERN void
87 tdm_log_enable_dlog(unsigned int enable)
88 {
89         dlog_enable = enable;
90         TDM_INFO("dlog_enable: %d", dlog_enable);
91 }
92
93 EXTERN void
94 tdm_log_set_debug_level(int level)
95 {
96         tdm_log_debug_level = level;
97         TDM_INFO("debug_level: %d", tdm_log_debug_level);
98 }
99
100 EXTERN void
101 tdm_log_set_assert_level(int level)
102 {
103         assert_level = level;
104         TDM_INFO("assert_level: %d", assert_level);
105 }
106
107 EXTERN void
108 tdm_log_set_path(const char *path)
109 {
110         TDM_INFO("log_path: %s", path);
111
112         if (!path) {
113                 if (stdout_fd != -1) {
114                         fflush(stdout);
115                         close(STDOUT_FILENO);
116                         dup2(stdout_fd, STDOUT_FILENO);
117                         close(stdout_fd);
118                         stdout_fd = -1;
119                 }
120         } else {
121                 char fd_name[TDM_PATH_LEN];
122                 int  log_fd = -1;
123                 FILE *log_fl;
124
125                 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
126
127                 log_fl = fopen(fd_name, "a");
128                 if (!log_fl) {
129                         TDM_ERR("failed: open file(%s)\n", fd_name);
130                         return;
131                 }
132
133                 if (stdout_fd == -1) {
134                         fflush(stdout);
135                         stdout_fd = dup(STDOUT_FILENO);
136                         if (stdout_fd < 0) {
137                                 TDM_ERR("dup failed: %m\n");
138                                 fclose(log_fl);
139                                 return;
140                         }
141                 }
142
143                 setvbuf(log_fl, NULL, _IOLBF, 512);
144                 log_fd = fileno(log_fl);
145
146                 close(STDOUT_FILENO);
147                 dup2(log_fd, STDOUT_FILENO);
148                 fclose(log_fl);
149                 TDM_INFO("log_path: %s done", path);
150         }
151 }
152
153 static void
154 _tdm_log_vprint_stdout(int level, const char *fmt, va_list ap)
155 {
156         if (!log_lock_init) {
157                 log_lock_init = 1;
158                 pthread_mutex_init(&log_lock, NULL);
159         }
160
161         if (level > tdm_log_debug_level)
162                 return;
163
164         char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"};
165         char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
166
167         pthread_mutex_lock(&log_lock);
168
169         if (color_enable)
170                 printf("%s", color[level]);
171         printf("[%s]", lvl_str[level]);
172         if (color_enable)
173                 printf(COLOR_RESET);
174         vprintf(fmt, ap);
175         printf("\n");
176         pthread_mutex_unlock(&log_lock);
177 }
178
179
180 EXTERN void
181 tdm_log_printf(int level, const char *fmt, ...)
182 {
183         va_list arg;
184         va_start(arg, fmt);
185         _tdm_log_vprint_stdout(level, fmt, arg);
186         va_end(arg);
187 }
188
189 EXTERN void
190 tdm_log_print(int level, const char *fmt, ...)
191 {
192         va_list arg;
193
194         if (level > tdm_log_debug_level)
195                 return;
196
197 #if ENABLE_DLOG
198         if (dlog_enable) {
199                 log_priority dlog_prio;
200                 switch (level) {
201                 case TDM_LOG_LEVEL_ERR:
202                         dlog_prio = DLOG_ERROR;
203                         break;
204                 case TDM_LOG_LEVEL_WRN:
205                         dlog_prio = DLOG_WARN;
206                         break;
207                 case TDM_LOG_LEVEL_INFO:
208                         dlog_prio = DLOG_INFO;
209                         break;
210                 case TDM_LOG_LEVEL_DBG:
211                         dlog_prio = DLOG_DEBUG;
212                         break;
213                 default:
214                         return;
215                 }
216                 va_start(arg, fmt);
217                 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
218                 va_end(arg);
219         }
220         else
221 #endif
222         {
223                 va_start(arg, fmt);
224                 _tdm_log_vprint_stdout(level, fmt, arg);
225                 va_end(arg);
226         }
227
228
229         assert(level > assert_level);
230 }
231
232 EXTERN void
233 tdm_log_reset(void)
234 {
235         pthread_mutex_unlock(&log_lock);
236 }