print mesage type(ex, (WW), (EE))
[adaptation/xorg/driver/xserver-xorg-module-xdbg.git] / lib / xdbg_log.c
1 /**************************************************************************
2
3 xdbg
4
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Boram Park <boram1288.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <string.h>
37 #include <stdarg.h>
38
39 #include "xdbg_log.h"
40
41 #ifndef API
42 #define API __attribute__ ((visibility("default")))
43 #endif
44
45 #ifndef MIN
46 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
47 #endif
48
49 #define MAX_MODULE_NAME 4
50 #define MAX_MODULE_CNT  256
51 #define BUF_LEN         1024
52
53 typedef struct
54 {
55     unsigned int module;
56     char *name;
57     int   loglevel;
58 } ModuleInfo;
59
60 static ModuleInfo modules[MAX_MODULE_CNT];
61 static int module_cnt = 0;
62
63 extern void dLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
64 extern void kLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
65 static char* _LogGetName (unsigned int module);
66
67 static void*
68 _LogInitModule (unsigned int module, int loglevel)
69 {
70     if (module_cnt >= MAX_MODULE_CNT)
71         return NULL;
72
73     modules[module_cnt].module = module;
74     modules[module_cnt].name = _LogGetName (module);
75     modules[module_cnt].loglevel = loglevel;
76     module_cnt++;
77
78     return &modules[module_cnt-1];
79 }
80
81 static void
82 _LogModule (void * handle, int logoption, const char * file, int line, const char * f, va_list args)
83 {
84     ModuleInfo *h = (ModuleInfo*)handle;
85     char *ostr[XLOG_LEVEL_MAX] = {"DD", "TT", "II", "WW", "EE"};
86     char tmpBuf[BUF_LEN];
87     int loglevel = logoption & XLOG_MASK_LOGLEVEL;
88     const char *name;
89
90     if (!h)
91         return;
92
93     name = h->name;
94
95     if (logoption & XLOG_OPTION_KLOG)
96     {
97         snprintf(tmpBuf, BUF_LEN, "[%s]%s", (name)?name:"", f);
98         kLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
99     }
100
101     /* write to file */
102     if (loglevel >= XLOG_LEVEL_INFO)
103     {
104         snprintf(tmpBuf, BUF_LEN, "(%s) [%s]%s", ostr[loglevel], (name)?name:"", f);
105
106         LogVWrite (1, tmpBuf, args);
107     }
108
109     /* write to terminal */
110     if (loglevel >= h->loglevel || logoption & XLOG_OPTION_SLOG)
111     {
112         char *buf = tmpBuf;
113         int   remain = BUF_LEN;
114         int   len = 0;
115
116         len = snprintf (buf, remain, "(%s) [%10.3f][%s]", ostr[loglevel], GetTimeInMillis()/1000.0, name?name:"");
117         buf += len;
118         remain -= len;
119
120         len += vsnprintf (buf, remain, f, args);
121
122         fwrite(tmpBuf, len, 1, stderr);
123     }
124 }
125
126 API int
127 xDbgLogSetLevel (unsigned int module, int level)
128 {
129     int i;
130     ModuleInfo * h;
131
132     if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
133         return FALSE;
134
135     for (i = 0; i < module_cnt; i++)
136     {
137         if (module == modules[i].module)
138         {
139             modules[i].loglevel = level;
140             return TRUE;
141         }
142     }
143
144     h = _LogInitModule (module, level);
145     if (h == NULL)
146         return FALSE;
147
148     return TRUE;
149 }
150
151 API void*
152 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
153 {
154     int loglevel = logoption & XLOG_MASK_LOGLEVEL;
155     ModuleInfo *h;
156     va_list args;
157     int i;
158
159     if (module == 0)
160         return NULL;
161
162     if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
163         return NULL;
164
165     for (i = 0; i < module_cnt; i++)
166     {
167         h = &modules[i];
168         if (module == h->module)
169             goto check_level;
170     }
171
172     h= (ModuleInfo *)_LogInitModule (module, XLOG_LEVEL_DEFAULT);
173     if(h == NULL)
174         return NULL;
175
176 check_level:
177     if (logoption & (XLOG_OPTION_KLOG | XLOG_OPTION_SLOG))
178         goto write_log;
179
180     if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
181         return h;
182
183 write_log:
184     va_start (args, f);
185     _LogModule (h, logoption, file, line, f, args);
186     va_end (args);
187
188     return h;
189 }
190
191 API int
192 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
193 {
194     int i, len;
195     char *p = buf;
196
197     switch (mode)
198     {
199     case MODE_NAME_ONLY:
200         for (i = 0; i < module_cnt && *remain > 0; i++)
201         {
202             len = snprintf (p, *remain, "%s", modules[i].name);
203             p += len;
204             *remain -= len;
205
206             if (i != module_cnt - 1 && *remain > 0)
207             {
208                 len = snprintf (p, *remain, "/");
209                 p += len;
210                 *remain -= len;
211             }
212         }
213         break;
214     case MODE_WITH_STATUS:
215         for (i = 0; i < module_cnt && *remain > 0; i++)
216         {
217             len = snprintf (p, *remain, "   %12s:%d\n", modules[i].name, modules[i].loglevel);
218             p += len;
219             *remain -= len;
220         }
221         break;
222     default:
223         return 0;
224     }
225
226     return p - buf;
227 }
228
229 static char*
230 _LogGetName (unsigned int module)
231 {
232     char *name = malloc (MAX_MODULE_NAME+1);
233     char *p = name;
234     int i;
235
236     if (!name)
237         return NULL;
238
239     name[0] = '\0';
240
241     for (i = 0; i < MAX_MODULE_NAME; i++)
242     {
243         if (!_C(module, (i<<3)))
244             break;
245
246         *p = _C(module, (i<<3));
247         p++;
248     }
249
250     *p = '\0';
251
252     return name;
253 }
254
255 API unsigned int
256 xDbgLogGetModule (char *name)
257 {
258     unsigned int module = 0;
259     int i, len;
260
261     if (!name)
262         return 0;
263
264     len = strlen (name);
265     for (i = 0; i < len; i++)
266     {
267         module |= _B(*name, (i<<3));
268         name++;
269     }
270
271     return module;
272 }