thread: support cb for multi-thread communication
[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 EXTERN void
73 tdm_log_enable_color(unsigned int enable)
74 {
75         color_enable = enable;
76 }
77
78 EXTERN void
79 tdm_log_enable_dlog(unsigned int enable)
80 {
81         dlog_enable = enable;
82 }
83
84 EXTERN void
85 tdm_log_enable_debug(unsigned int enable)
86 {
87         if (enable)
88                 tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
89         else
90                 tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
91 }
92
93 EXTERN void
94 tdm_log_set_debug_level(int level)
95 {
96         tdm_log_debug_level = level;
97 }
98
99 EXTERN void
100 tdm_log_set_assert_level(int level)
101 {
102         assert_level = level;
103 }
104
105 EXTERN void
106 tdm_log_set_path(const char *path)
107 {
108         char fd_name[TDM_PATH_LEN];
109         int  log_fd = -1;
110         FILE *log_fl;
111
112         snprintf(fd_name, TDM_PATH_LEN, "%s", path);
113
114         log_fl = fopen(fd_name, "a");
115         if (!log_fl) {
116                 TDM_ERR("failed: open file(%s)\n", fd_name);
117                 return;
118         }
119
120         fflush(stdout);
121         close(STDOUT_FILENO);
122
123         setvbuf(log_fl, NULL, _IOLBF, 512);
124         log_fd = fileno(log_fl);
125
126         dup2(log_fd, STDOUT_FILENO);
127         fclose(log_fl);
128 }
129
130 EXTERN void
131 tdm_log_print(int level, const char *fmt, ...)
132 {
133         va_list arg;
134
135         if (!log_lock_init) {
136                 log_lock_init = 1;
137                 pthread_mutex_init(&log_lock, NULL);
138
139         }
140
141         if (level > tdm_log_debug_level)
142                 return;
143
144         if (dlog_enable) {
145                 log_priority dlog_prio;
146                 switch (level) {
147                 case TDM_LOG_LEVEL_ERR:
148                         dlog_prio = DLOG_ERROR;
149                         break;
150                 case TDM_LOG_LEVEL_WRN:
151                         dlog_prio = DLOG_WARN;
152                         break;
153                 case TDM_LOG_LEVEL_INFO:
154                         dlog_prio = DLOG_INFO;
155                         break;
156                 case TDM_LOG_LEVEL_DBG:
157                         dlog_prio = DLOG_DEBUG;
158                         break;
159                 default:
160                         return;
161                 }
162                 va_start(arg, fmt);
163                 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
164                 va_end(arg);
165         } else {
166                 struct timespec ts;
167                 char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"};
168                 char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
169
170                 clock_gettime(CLOCK_MONOTONIC, &ts);
171
172                 pthread_mutex_lock(&log_lock);
173
174                 if (color_enable)
175                         printf("%s", color[level]);
176                 printf("[%s]", lvl_str[level]);
177                 if (color_enable)
178                         printf(COLOR_RESET);
179                 printf("[%d.%06d]", (int)ts.tv_sec, (int)ts.tv_nsec / 1000);
180                 va_start(arg, fmt);
181                 vprintf(fmt, arg);
182                 va_end(arg);
183
184                 pthread_mutex_unlock(&log_lock);
185         }
186
187 #ifdef TDM_CONFIG_ASSERT
188         if (level <= assert_level)
189                 assert(0);
190 #endif
191 }
192
193 EXTERN void
194 tdm_log_reset(void)
195 {
196         pthread_mutex_trylock(&log_lock);
197         pthread_mutex_unlock(&log_lock);
198 }