log: check log level
[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 TDM_CONFIG_ASSERT
53
54 #define LOG_MAX_LEN    4076
55
56 #define COLOR_RED "\x1b[31m"      /* for error */
57 #define COLOR_YELLOW "\x1b[33m"   /* for warning */
58 #define COLOR_GREEN "\x1b[32m"    /* for info */
59 #define COLOR_RESET "\x1b[0m"
60
61 #undef LOG_TAG
62 #define LOG_TAG "TDM"
63
64 static unsigned int dlog_enable = 1;
65 static unsigned int color_enable = 1;
66
67 static unsigned int need_check_env = 1;
68
69 static unsigned int log_lock_init;
70 static pthread_mutex_t log_lock;
71
72 unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
73
74 static void
75 _tdm_log_check_env(void)
76 {
77         const char *str;
78         char *end;
79
80         str = getenv("TDM_DEBUG_LEVEL");
81         if (str)
82                 tdm_log_debug_level = strtol(str, &end, 10);
83
84         str = getenv("TDM_DEBUG");
85         if (str && (strstr(str, "1")))
86                 tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
87
88         str = getenv("TDM_DLOG");
89         if (str && (strstr(str, "0")))
90                 dlog_enable = 0;
91 }
92
93 EXTERN void
94 tdm_log_enable_color(unsigned int enable)
95 {
96         color_enable = enable;
97 }
98
99 EXTERN void
100 tdm_log_enable_dlog(unsigned int enable)
101 {
102         dlog_enable = enable;
103 }
104
105 EXTERN void
106 tdm_log_enable_debug(unsigned int enable)
107 {
108         if (enable)
109                 tdm_log_debug_level = TDM_LOG_LEVEL_DBG;
110         else
111                 tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
112 }
113
114 EXTERN void
115 tdm_log_set_debug_level(int level)
116 {
117         tdm_log_debug_level = level;
118 }
119
120 EXTERN void
121 tdm_log_print(int level, const char *fmt, ...)
122 {
123         va_list arg;
124
125         if (!log_lock_init) {
126                 log_lock_init = 1;
127                 pthread_mutex_init(&log_lock, NULL);
128
129         }
130
131         if (need_check_env) {
132                 need_check_env = 0;
133                 _tdm_log_check_env();
134         }
135
136         if (level > tdm_log_debug_level)
137                 return;
138
139         if (dlog_enable) {
140                 log_priority dlog_prio;
141                 switch (level) {
142                 case TDM_LOG_LEVEL_ERR:
143                         dlog_prio = DLOG_ERROR;
144                         break;
145                 case TDM_LOG_LEVEL_WRN:
146                         dlog_prio = DLOG_WARN;
147                         break;
148                 case TDM_LOG_LEVEL_INFO:
149                         dlog_prio = DLOG_INFO;
150                         break;
151                 case TDM_LOG_LEVEL_DBG:
152                         dlog_prio = DLOG_DEBUG;
153                         break;
154                 default:
155                         return;
156                 }
157                 va_start(arg, fmt);
158                 __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg);
159                 va_end(arg);
160         } else {
161                 struct timespec ts;
162                 char *lvl_str[] = {"TDM_NON", "TDM_ERR", "TDM_WRN", "TDM_INF", "TDM_DBG"};
163                 char *color[] = {COLOR_RESET, COLOR_RED, COLOR_YELLOW, COLOR_GREEN, COLOR_RESET};
164
165                 clock_gettime(CLOCK_MONOTONIC, &ts);
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                 printf("[%d.%06d]", (int)ts.tv_sec, (int)ts.tv_nsec / 1000);
175                 va_start(arg, fmt);
176                 vprintf(fmt, arg);
177                 va_end(arg);
178
179                 pthread_mutex_unlock(&log_lock);
180         }
181
182 #ifdef TDM_CONFIG_ASSERT
183         if (level < 3)
184                 assert(0);
185 #endif
186 }