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;
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);
68 _LogInitModule (unsigned int module, int loglevel)
70 if (module_cnt >= MAX_MODULE_CNT)
73 modules[module_cnt].module = module;
74 modules[module_cnt].name = _LogGetName (module);
75 modules[module_cnt].loglevel = loglevel;
78 return &modules[module_cnt-1];
82 _LogModule (void * handle, int logoption, const char * file, int line, const char * f, va_list args)
84 ModuleInfo *h = (ModuleInfo*)handle;
85 char *ostr[XLOG_LEVEL_MAX] = {"DD", "TT", "II", "WW", "EE"};
87 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
95 if (logoption & XLOG_OPTION_KLOG)
97 snprintf(tmpBuf, BUF_LEN, "[%s]%s", (name)?name:"", f);
98 kLogWrapper (loglevel, logoption, file, line, tmpBuf, args);
102 if (loglevel >= XLOG_LEVEL_INFO)
104 snprintf(tmpBuf, BUF_LEN, "(%s) [%s]%s", ostr[loglevel], (name)?name:"", f);
106 LogVWrite (1, tmpBuf, args);
109 /* write to terminal */
110 if (loglevel >= h->loglevel || logoption & XLOG_OPTION_SLOG)
113 int remain = BUF_LEN;
116 len = snprintf (buf, remain, "(%s) [%10.3f][%s]", ostr[loglevel], GetTimeInMillis()/1000.0, name?name:"");
120 len += vsnprintf (buf, remain, f, args);
122 fwrite(tmpBuf, len, 1, stderr);
127 xDbgLogSetLevel (unsigned int module, int level)
132 if (level < XLOG_LEVEL_0 || level > XLOG_LEVEL_4)
135 for (i = 0; i < module_cnt; i++)
137 if (module == modules[i].module)
139 modules[i].loglevel = level;
144 h = _LogInitModule (module, level);
152 xDbgLog (unsigned int module, int logoption, const char * file, int line, const char * f, ...)
154 int loglevel = logoption & XLOG_MASK_LOGLEVEL;
162 if (loglevel < XLOG_LEVEL_0 || loglevel > XLOG_LEVEL_4)
165 for (i = 0; i < module_cnt; i++)
168 if (module == h->module)
172 h= (ModuleInfo *)_LogInitModule (module, XLOG_LEVEL_DEFAULT);
177 if (logoption & (XLOG_OPTION_KLOG | XLOG_OPTION_SLOG))
180 if (loglevel < XLOG_LEVEL_INFO && loglevel < h->loglevel)
185 _LogModule (h, logoption, file, line, f, args);
192 xDbgLogEnumModules (LOG_ENUM_MODE mode, char *buf, int *remain)
200 for (i = 0; i < module_cnt && *remain > 0; i++)
202 len = snprintf (p, *remain, "%s", modules[i].name);
206 if (i != module_cnt - 1 && *remain > 0)
208 len = snprintf (p, *remain, "/");
214 case MODE_WITH_STATUS:
215 for (i = 0; i < module_cnt && *remain > 0; i++)
217 len = snprintf (p, *remain, " %12s:%d\n", modules[i].name, modules[i].loglevel);
230 _LogGetName (unsigned int module)
232 char *name = malloc (MAX_MODULE_NAME+1);
241 for (i = 0; i < MAX_MODULE_NAME; i++)
243 if (!_C(module, (i<<3)))
246 *p = _C(module, (i<<3));
256 xDbgLogGetModule (char *name)
258 unsigned int module = 0;
265 for (i = 0; i < len; i++)
267 module |= _B(*name, (i<<3));