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;
106 if (logoption & XLOG_OPTION_KLOG)
108 snprintf(tmpBuf, BUF_LEN, "[%s]%s", (name)?name:"", f);
109 kLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
111 else if (loglevel >= XLOG_LEVEL_INFO)
115 /* get local time from tv */
116 gettimeofday(&tv, NULL);
117 tm = localtime_r(&tv.tv_sec, &result);
122 if (logoption & XLOG_OPTION_SECURE)
123 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);
125 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);
128 snprintf(tmpBuf, BUF_LEN, "[failed to get local time] (%s) [%s]%s", ostr[loglevel], (name)?name:"", f);
130 LogVWrite (1, tmpBuf, args);
134 /* dlog already supports localtime stamp. So we just add clock time! */
136 if (logoption & XLOG_OPTION_SECURE)
137 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) > [SECURE_LOG] [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
139 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
141 dLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
145 /* write to console */
146 if (loglevel >= h->loglevel)
149 int remain = BUF_LEN;
155 gettimeofday(&tv, NULL);
156 tm = localtime_r(&tv.tv_sec, &result);
159 tv_ms = tv.tv_usec/1000;
161 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:"");
163 len = snprintf (buf, remain, "[%10.3f] [Failed to get current time:%03d] (%s) [%s]", GetTimeInMillis()/1000.0, tv_ms, ostr[loglevel], name?name:"");
167 len += vsnprintf (buf, remain, f, args);
169 fwrite(tmpBuf, len, 1, stderr);
174 xDbgLogSetLevel (unsigned int module, int level)
179 if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
182 if (module == XDBG_ALL_MODULE)
184 default_level = level;
185 for (i = 0; i < module_cnt; i++)
186 modules[i].loglevel = level;
191 for (i = 0; i < module_cnt; i++)
193 if (module == modules[i].module)
195 modules[i].loglevel = level;
200 h = _LogInitModule (module, level);
208 xDbgLogEnableDlog (Bool enable)
210 dlog_enable = (enable > 0) ? TRUE:FALSE;
214 xDbgGetLogEnableDlog ()
220 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
222 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
230 if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
233 for (i = 0; i < module_cnt; i++)
236 if (module == h->module)
240 h= (ModuleInfo *)_LogInitModule (module, default_level);
245 if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
249 _LogModule (h, logoption, file, line, f, args);
256 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
264 for (i = 0; i < module_cnt && *remain > 0; i++)
266 len = snprintf (p, *remain, "%s", modules[i].name);
270 if (i != module_cnt - 1 && *remain > 0)
272 len = snprintf (p, *remain, "/");
278 case MODE_WITH_STATUS:
279 for (i = 0; i < module_cnt && *remain > 0; i++)
281 len = snprintf (p, *remain, " %12s:%d\n", modules[i].name, modules[i].loglevel);
294 _LogGetName (unsigned int module)
296 char *name = malloc (MAX_MODULE_NAME+1);
305 for (i = 0; i < MAX_MODULE_NAME; i++)
307 if (!_C(module, (i<<3)))
310 *p = _C(module, (i<<3));
320 xDbgLogGetModule (char *name)
322 unsigned int module = 0;
328 if (!strcasecmp (name, "all"))
329 return XDBG_ALL_MODULE;
332 for (i = 0; i < len; i++)
334 module |= _B(*name, (i<<3));