1 /**************************************************************************
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
7 Contact: Boram Park <boram1288.park@samsung.com>
8 Sangjin LEE <lsj119@samsung.com>
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:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
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.
30 **************************************************************************/
42 #define API __attribute__ ((visibility("default")))
46 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
49 #define MAX_MODULE_NAME 4
50 #define MAX_MODULE_CNT 256
60 static ModuleInfo modules[MAX_MODULE_CNT];
61 static int module_cnt = 0;
62 static int default_level = XLOG_LEVEL_DEFAULT;
64 static Bool dlog_enable;
66 extern void dLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
67 extern void kLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
68 static char* _LogGetName (unsigned int module);
71 _LogInitModule (unsigned int module, int loglevel)
73 if (module_cnt >= MAX_MODULE_CNT)
76 modules[module_cnt].module = module;
77 modules[module_cnt].name = _LogGetName (module);
78 modules[module_cnt].loglevel = loglevel;
81 return &modules[module_cnt-1];
85 _LogModule (void * handle, int logoption, const char * file, int line, const char * f, va_list args)
87 ModuleInfo *h = (ModuleInfo*)handle;
88 char *ostr[XLOG_LEVEL_MAX] = {"DD", "TT", "II", "WW", "EE"};
90 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
98 if (logoption & XLOG_OPTION_KLOG)
100 snprintf(tmpBuf, BUF_LEN, "[%s]%s", (name)?name:"", f);
101 kLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
105 if (loglevel >= XLOG_LEVEL_INFO)
107 if (logoption & XLOG_OPTION_SECURE)
108 snprintf(tmpBuf, BUF_LEN, "(%s) > [SECURE_LOG] [%s]%s", ostr[loglevel], (name)?name:"", f);
110 snprintf(tmpBuf, BUF_LEN, "(%s) [%s]%s", ostr[loglevel], (name)?name:"", f);
113 LogVWrite (1, tmpBuf, args);
116 dLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
118 /* write to Xorg.0.log too */
119 if (loglevel >= XLOG_LEVEL_WARNING)
120 LogVWrite (1, tmpBuf, args);
124 /* write to console */
125 if (loglevel >= h->loglevel)
128 int remain = BUF_LEN;
131 len = snprintf (buf, remain, "(%s) [%10.3f][%s]", ostr[loglevel], GetTimeInMillis()/1000.0, name?name:"");
135 len += vsnprintf (buf, remain, f, args);
137 fwrite(tmpBuf, len, 1, stderr);
142 xDbgLogSetLevel (unsigned int module, int level)
147 if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
150 if (module == XDBG_ALL_MODULE)
152 default_level = level;
153 for (i = 0; i < module_cnt; i++)
154 modules[i].loglevel = level;
159 for (i = 0; i < module_cnt; i++)
161 if (module == modules[i].module)
163 modules[i].loglevel = level;
168 h = _LogInitModule (module, level);
176 xDbgLogEnableDlog (Bool enable)
178 dlog_enable = (enable > 0) ? TRUE:FALSE;
182 xDbgGetLogEnableDlog ()
188 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
190 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
198 if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
201 for (i = 0; i < module_cnt; i++)
204 if (module == h->module)
208 h= (ModuleInfo *)_LogInitModule (module, default_level);
213 if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
217 _LogModule (h, logoption, file, line, f, args);
224 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
232 for (i = 0; i < module_cnt && *remain > 0; i++)
234 len = snprintf (p, *remain, "%s", modules[i].name);
238 if (i != module_cnt - 1 && *remain > 0)
240 len = snprintf (p, *remain, "/");
246 case MODE_WITH_STATUS:
247 for (i = 0; i < module_cnt && *remain > 0; i++)
249 len = snprintf (p, *remain, " %12s:%d\n", modules[i].name, modules[i].loglevel);
262 _LogGetName (unsigned int module)
264 char *name = malloc (MAX_MODULE_NAME+1);
273 for (i = 0; i < MAX_MODULE_NAME; i++)
275 if (!_C(module, (i<<3)))
278 *p = _C(module, (i<<3));
288 xDbgLogGetModule (char *name)
290 unsigned int module = 0;
296 if (!strcasecmp (name, "all"))
297 return XDBG_ALL_MODULE;
300 for (i = 0; i < len; i++)
302 module |= _B(*name, (i<<3));