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 **************************************************************************/
45 #define API __attribute__ ((visibility("default")))
49 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
52 #define MAX_MODULE_NAME 4
53 #define MAX_MODULE_CNT 256
64 static ModuleInfo modules[MAX_MODULE_CNT];
65 static int module_cnt = 0;
66 static int default_level = XLOG_LEVEL_DEFAULT;
67 static char *st_month[MAX_MONTH] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
69 static Bool dlog_enable;
71 extern void dLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
72 extern void kLogWrapper (int loglevel, int is_module, const char * file, int line, const char * f, va_list args);
73 static char* _LogGetName (unsigned int module);
76 _LogInitModule (unsigned int module, int loglevel)
78 if (module_cnt >= MAX_MODULE_CNT)
81 modules[module_cnt].module = module;
82 modules[module_cnt].name = _LogGetName (module);
83 modules[module_cnt].loglevel = loglevel;
86 return &modules[module_cnt-1];
90 _LogModule (void * handle, int logoption, const char * file, int line, const char * f, va_list args)
92 ModuleInfo *h = (ModuleInfo*)handle;
93 char *ostr[XLOG_LEVEL_MAX] = {"DD", "TT", "II", "WW", "EE"};
95 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
105 if (logoption & XLOG_OPTION_KLOG)
107 snprintf(tmpBuf, BUF_LEN, "[%s]%s", (name)?name:"", f);
108 kLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
110 else if (loglevel >= XLOG_LEVEL_INFO)
114 /* get local time from tv */
115 gettimeofday(&tv, NULL);
116 tm = localtime(&tv.tv_sec);
119 if (logoption & XLOG_OPTION_SECURE)
120 snprintf(tmpBuf, BUF_LEN, "[%s %d %02d:%02d:%02d:%03ld] (%s) > [SECURE_LOG] [%s]%s", st_month[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000, ostr[loglevel], (name)?name:"", f);
122 snprintf(tmpBuf, BUF_LEN, "[%s %d %02d:%02d:%02d:%03ld] (%s) [%s]%s", st_month[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000, ostr[loglevel], (name)?name:"", f);
124 LogVWrite (1, tmpBuf, args);
128 /* dlog already supports localtime stamp. So we just add clock time! */
130 if (logoption & XLOG_OPTION_SECURE)
131 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) > [SECURE_LOG] [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
133 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
135 dLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
139 /* write to console */
140 if (loglevel >= h->loglevel)
143 int remain = BUF_LEN;
149 gettimeofday(&tv, NULL);
150 tm = localtime(&tv.tv_sec);
153 tv_ms = tv.tv_usec/1000;
154 len = snprintf (buf, remain, "[%10.3f] [%s %d %02d:%02d:%02d:%03d] (%s) [%s]", GetTimeInMillis()/1000.0, st_month[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv_ms, ostr[loglevel], name?name:"");
158 len += vsnprintf (buf, remain, f, args);
160 fwrite(tmpBuf, len, 1, stderr);
165 xDbgLogSetLevel (unsigned int module, int level)
170 if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
173 if (module == XDBG_ALL_MODULE)
175 default_level = level;
176 for (i = 0; i < module_cnt; i++)
177 modules[i].loglevel = level;
182 for (i = 0; i < module_cnt; i++)
184 if (module == modules[i].module)
186 modules[i].loglevel = level;
191 h = _LogInitModule (module, level);
199 xDbgLogEnableDlog (Bool enable)
201 dlog_enable = (enable > 0) ? TRUE:FALSE;
205 xDbgGetLogEnableDlog ()
211 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
213 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
221 if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
224 for (i = 0; i < module_cnt; i++)
227 if (module == h->module)
231 h= (ModuleInfo *)_LogInitModule (module, default_level);
236 if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
240 _LogModule (h, logoption, file, line, f, args);
247 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
255 for (i = 0; i < module_cnt && *remain > 0; i++)
257 len = snprintf (p, *remain, "%s", modules[i].name);
261 if (i != module_cnt - 1 && *remain > 0)
263 len = snprintf (p, *remain, "/");
269 case MODE_WITH_STATUS:
270 for (i = 0; i < module_cnt && *remain > 0; i++)
272 len = snprintf (p, *remain, " %12s:%d\n", modules[i].name, modules[i].loglevel);
285 _LogGetName (unsigned int module)
287 char *name = malloc (MAX_MODULE_NAME+1);
296 for (i = 0; i < MAX_MODULE_NAME; i++)
298 if (!_C(module, (i<<3)))
301 *p = _C(module, (i<<3));
311 xDbgLogGetModule (char *name)
313 unsigned int module = 0;
319 if (!strcasecmp (name, "all"))
320 return XDBG_ALL_MODULE;
323 for (i = 0; i < len; i++)
325 module |= _B(*name, (i<<3));