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