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);
121 if (logoption & XLOG_OPTION_SECURE)
122 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);
124 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);
127 snprintf(tmpBuf, BUF_LEN, "[failed to get local time] (%s) [%s]%s", ostr[loglevel], (name)?name:"", f);
129 LogVWrite (1, tmpBuf, args);
133 /* dlog already supports localtime stamp. So we just add clock time! */
135 if (logoption & XLOG_OPTION_SECURE)
136 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) > [SECURE_LOG] [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
138 snprintf(tmpBuf, BUF_LEN, "[%10.3f] (%s) [%s]%s", GetTimeInMillis()/1000.0, ostr[loglevel], (name)?name:"", f);
140 dLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
144 /* write to console */
145 if (loglevel >= h->loglevel)
148 int remain = BUF_LEN;
154 gettimeofday(&tv, NULL);
155 tm = localtime(&tv.tv_sec);
158 tv_ms = tv.tv_usec/1000;
160 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:"");
162 len = snprintf (buf, remain, "[%10.3f] [Failed to get current time:%03d] (%s) [%s]", GetTimeInMillis()/1000.0, tv_ms, ostr[loglevel], name?name:"");
166 len += vsnprintf (buf, remain, f, args);
168 fwrite(tmpBuf, len, 1, stderr);
173 xDbgLogSetLevel (unsigned int module, int level)
178 if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
181 if (module == XDBG_ALL_MODULE)
183 default_level = level;
184 for (i = 0; i < module_cnt; i++)
185 modules[i].loglevel = level;
190 for (i = 0; i < module_cnt; i++)
192 if (module == modules[i].module)
194 modules[i].loglevel = level;
199 h = _LogInitModule (module, level);
207 xDbgLogEnableDlog (Bool enable)
209 dlog_enable = (enable > 0) ? TRUE:FALSE;
213 xDbgGetLogEnableDlog ()
219 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
221 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
229 if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
232 for (i = 0; i < module_cnt; i++)
235 if (module == h->module)
239 h= (ModuleInfo *)_LogInitModule (module, default_level);
244 if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
248 _LogModule (h, logoption, file, line, f, args);
255 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
263 for (i = 0; i < module_cnt && *remain > 0; i++)
265 len = snprintf (p, *remain, "%s", modules[i].name);
269 if (i != module_cnt - 1 && *remain > 0)
271 len = snprintf (p, *remain, "/");
277 case MODE_WITH_STATUS:
278 for (i = 0; i < module_cnt && *remain > 0; i++)
280 len = snprintf (p, *remain, " %12s:%d\n", modules[i].name, modules[i].loglevel);
293 _LogGetName (unsigned int module)
295 char *name = malloc (MAX_MODULE_NAME+1);
304 for (i = 0; i < MAX_MODULE_NAME; i++)
306 if (!_C(module, (i<<3)))
309 *p = _C(module, (i<<3));
319 xDbgLogGetModule (char *name)
321 unsigned int module = 0;
327 if (!strcasecmp (name, "all"))
328 return XDBG_ALL_MODULE;
331 for (i = 0; i < len; i++)
333 module |= _B(*name, (i<<3));