Modify the value of privilege level enum
[platform/framework/native/appfw.git] / src / base / FBaseSys.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseSys.cpp
19  * @brief               This file defines the diagnostics types.
20  */
21
22 #include <cassert>
23 #include <cstdio>
24 #include <cstring>
25 #include <unistd.h>
26 #include <sys/syscall.h>
27 #include <sys/types.h>
28
29 #include <vconf.h>
30 #include <dlog.h>
31 #include <appinfo.h>
32
33 #include <FAppTypes.h>
34 #include <FBaseLog.h>
35 #include <FBaseSysLog.h>
36
37 #include "FBase_Log.h"
38 #include "FBase_StringConverter.h"
39
40 extern "C" {
41 #include <iniparser.h>
42 }
43
44 using namespace Tizen::Base;
45
46 #ifdef LOG_TAG
47 #undef LOG_TAG
48 #define LOG_TAG "OSP_SLP"
49 #define LOG_TAG_NULL ""
50 #endif
51
52 void __InitializeLogLevel(dictionary *pDic);
53 void __InitializePlatformModule(dictionary *pDic);
54
55 static const char LOG_TAG_ASSERT[] = "Assert";
56 static const char FILE_DEBUGMODE[] = "/opt/etc/.debugmode";
57
58 bool iniLoaded = false;
59
60 static bool appNameLoaded = false;
61 static char appName[LOG_MODULE_NAME_LEN_MAX];
62
63 static bool envLoaded = false;
64 static bool envPlatformInfoEnabled = false;
65
66 #define APP_RESERVED(X) NID_APP+X
67 #define BASE_RESERVED(X) NID_BASE+X
68 #define CNT_RESERVED(X) NID_CNT+X
69 #define CTXT_RESERVED(X) NID_CTXT+X
70 #define GRP_RESERVED(X) NID_GRP+X
71
72 #define IO_RESERVED(X) NID_IO+X
73 #define LCL_RESERVED(X) NID_LCL+X
74 #define LOC_RESERVED(X) NID_LOC+X
75 #define MEDIA_RESERVED(X) NID_MEDIA+X
76 #define MSG_RESERVED(X) NID_MSG+X
77
78 #define NET_RESERVED(X) NID_NET+X
79 #define SEC_RESERVED(X) NID_SEC+X
80 #define SCL_RESERVED(X) NID_SCL+X
81 #define SYS_RESERVED(X) NID_SYS+X
82 #define TEL_RESERVED(X) NID_TEL+X
83
84 #define TEXT_RESERVED(X) NID_TEXT+X
85 #define UI_RESERVED(X) NID_UI+X
86 #define UIX_RESERVED(X) NID_UIX+X
87 #define WEB_RESERVED(X) NID_WEB+X
88 #define SHELL_RESERVED(X) NID_SHELL+X
89
90 static LogInfo logInfo =
91 {
92         {
93                 //OSP Namespace ID ===============================================================
94                 {NID_APP, "NID_APP", "Tizen::App", true},
95                 {APP_RESERVED(1) , "RESERVED", "", false},
96                 {APP_RESERVED(2) , "RESERVED", "", false},
97                 {APP_RESERVED(3) , "RESERVED", "", false},
98                 {APP_RESERVED(4) , "RESERVED", "", false},
99                 {APP_RESERVED(5) , "RESERVED", "", false},
100                 {APP_RESERVED(6) , "RESERVED", "", false},
101                 {APP_RESERVED(7) , "RESERVED", "", false},
102                 {APP_RESERVED(8) , "RESERVED", "", false},
103                 {APP_RESERVED(9) , "RESERVED", "", false},
104
105                 {NID_BASE, "NID_BASE", "Tizen::Base", true},
106                 {NID_BASE_COL, "NID_BASE_COL", "Tizen::Base::Collection", true},
107                 {NID_BASE_RT, "NID_BASE_RT", "Tizen::Base::Runtime", true},
108                 {NID_BASE_UTIL, "NID_BASE_UTIL", "Tizen::Base::Utility", true},
109                 {BASE_RESERVED(4) , "RESERVED", "", false},
110                 {BASE_RESERVED(5) , "RESERVED", "", false},
111                 {BASE_RESERVED(6) , "RESERVED", "", false},
112                 {BASE_RESERVED(7) , "RESERVED", "", false},
113                 {BASE_RESERVED(8) , "RESERVED", "", false},
114                 {BASE_RESERVED(9) , "RESERVED", "", false},
115
116                 {NID_CNT, "NID_CNT", "Tizen::Content", true},
117                 {CNT_RESERVED(1) , "RESERVED", "", false},
118                 {CNT_RESERVED(2) , "RESERVED", "", false},
119                 {CNT_RESERVED(3) , "RESERVED", "", false},
120                 {CNT_RESERVED(4) , "RESERVED", "", false},
121                 {CNT_RESERVED(5) , "RESERVED", "", false},
122                 {CNT_RESERVED(6) , "RESERVED", "", false},
123                 {CNT_RESERVED(7) , "RESERVED", "", false},
124                 {CNT_RESERVED(8) , "RESERVED", "", false},
125                 {CNT_RESERVED(9) , "RESERVED", "", false},
126
127                 {NID_CTXT, "NID_CTXT", "Tizen::Context", true},
128                 {CTXT_RESERVED(1) , "RESERVED", "", false},
129                 {CTXT_RESERVED(2) , "RESERVED", "", false},
130                 {CTXT_RESERVED(3) , "RESERVED", "", false},
131                 {CTXT_RESERVED(4) , "RESERVED", "", false},
132                 {CTXT_RESERVED(5) , "RESERVED", "", false},
133                 {CTXT_RESERVED(6) , "RESERVED", "", false},
134                 {CTXT_RESERVED(7) , "RESERVED", "", false},
135                 {CTXT_RESERVED(8) , "RESERVED", "", false},
136                 {CTXT_RESERVED(9) , "RESERVED", "", false},
137
138                 {NID_GRP, "NID_GRP", "Tizen::Graphics", true},
139                 {GRP_RESERVED(1) , "RESERVED", "", false},
140                 {GRP_RESERVED(2) , "RESERVED", "", false},
141                 {GRP_RESERVED(3) , "RESERVED", "", false},
142                 {GRP_RESERVED(4) , "RESERVED", "", false},
143                 {GRP_RESERVED(5) , "RESERVED", "", false},
144                 {GRP_RESERVED(6) , "RESERVED", "", false},
145                 {GRP_RESERVED(7) , "RESERVED", "", false},
146                 {GRP_RESERVED(8) , "RESERVED", "", false},
147                 {GRP_RESERVED(9) , "RESERVED", "", false},
148
149                 {NID_IO, "NID_IO", "Tizen::Io", true},
150                 {IO_RESERVED(1) , "RESERVED", "", false},
151                 {IO_RESERVED(2) , "RESERVED", "", false},
152                 {IO_RESERVED(3) , "RESERVED", "", false},
153                 {IO_RESERVED(4) , "RESERVED", "", false},
154                 {IO_RESERVED(5) , "RESERVED", "", false},
155                 {IO_RESERVED(6) , "RESERVED", "", false},
156                 {IO_RESERVED(7) , "RESERVED", "", false},
157                 {IO_RESERVED(8) , "RESERVED", "", false},
158                 {IO_RESERVED(9) , "RESERVED", "", false},
159
160                 {NID_LCL, "NID_LCL", "Tizen::Locales", true},
161                 {LCL_RESERVED(1) , "RESERVED", "", false},
162                 {LCL_RESERVED(2) , "RESERVED", "", false},
163                 {LCL_RESERVED(3) , "RESERVED", "", false},
164                 {LCL_RESERVED(4) , "RESERVED", "", false},
165                 {LCL_RESERVED(5) , "RESERVED", "", false},
166                 {LCL_RESERVED(6) , "RESERVED", "", false},
167                 {LCL_RESERVED(7) , "RESERVED", "", false},
168                 {LCL_RESERVED(8) , "RESERVED", "", false},
169                 {LCL_RESERVED(9) , "RESERVED", "", false},
170
171                 {NID_LOC, "NID_LOC", "Tizen::Locations", true},
172                 {NID_LOC_CTRL, "NID_LOC_CTRL", "Tizen::Locations::Controls", true},
173                 {NID_LOC_SVC, "NID_LOC_SVC", "Tizen::Locations::Services", true},
174                 {LOC_RESERVED(3) , "RESERVED", "", false},
175                 {LOC_RESERVED(4) , "RESERVED", "", false},
176                 {LOC_RESERVED(5) , "RESERVED", "", false},
177                 {LOC_RESERVED(6) , "RESERVED", "", false},
178                 {LOC_RESERVED(7) , "RESERVED", "", false},
179                 {LOC_RESERVED(8) , "RESERVED", "", false},
180                 {LOC_RESERVED(9) , "RESERVED", "", false},
181
182                 {NID_MEDIA, "NID_MEDIA", "Tizen::Media", true},
183                 {MEDIA_RESERVED(1) , "RESERVED", "", false},
184                 {MEDIA_RESERVED(2) , "RESERVED", "", false},
185                 {MEDIA_RESERVED(3) , "RESERVED", "", false},
186                 {MEDIA_RESERVED(4) , "RESERVED", "", false},
187                 {MEDIA_RESERVED(5) , "RESERVED", "", false},
188                 {MEDIA_RESERVED(6) , "RESERVED", "", false},
189                 {MEDIA_RESERVED(7) , "RESERVED", "", false},
190                 {MEDIA_RESERVED(8) , "RESERVED", "", false},
191                 {MEDIA_RESERVED(9) , "RESERVED", "", false},
192
193                 {NID_MSG, "NID_MSG", "Tizen::Messaging", true},
194                 {MSG_RESERVED(1) , "RESERVED", "", false},
195                 {MSG_RESERVED(2) , "RESERVED", "", false},
196                 {MSG_RESERVED(3) , "RESERVED", "", false},
197                 {MSG_RESERVED(4) , "RESERVED", "", false},
198                 {MSG_RESERVED(5) , "RESERVED", "", false},
199                 {MSG_RESERVED(6) , "RESERVED", "", false},
200                 {MSG_RESERVED(7) , "RESERVED", "", false},
201                 {MSG_RESERVED(8) , "RESERVED", "", false},
202                 {MSG_RESERVED(9) , "RESERVED", "", false},
203
204                 {NID_NET, "NID_NET", "Tizen::Net", true},
205                 {NID_NET_BT, "NID_NET_BT", "Tizen::Net::Bluetooth", true},
206                 {NID_NET_HTTP, "NID_NET_HTTP", "Tizen::Net::Http", true},
207                 {NID_NET_NFC, "NID_NET_NFC", "Tizen::Net::Nfc", true},
208                 {NID_NET_SOCK, "NID_NET_SOCK", "Tizen::Net::Sockets", true},
209                 {NID_NET_WIFI, "NID_NET_WIFI", "Tizen::Net::Wifi", true},
210                 {NET_RESERVED(6) , "RESERVED", "", false},
211                 {NET_RESERVED(7) , "RESERVED", "", false},
212                 {NET_RESERVED(8) , "RESERVED", "", false},
213                 {NET_RESERVED(9) , "RESERVED", "", false},
214
215                 {NID_SEC, "NID_SEC", "Tizen::Security", true},
216                 {NID_SEC_CERT, "NID_SEC_CERT", "Tizen::Security::Cert", true},
217                 {NID_SEC_CRYPTO, "NID_SEC_CRYPTO", "Tizen::Security::Crypto", true},
218                 {SEC_RESERVED(3) , "RESERVED", "", false},
219                 {SEC_RESERVED(4) , "RESERVED", "", false},
220                 {SEC_RESERVED(5) , "RESERVED", "", false},
221                 {SEC_RESERVED(6) , "RESERVED", "", false},
222                 {SEC_RESERVED(7) , "RESERVED", "", false},
223                 {SEC_RESERVED(8) , "RESERVED", "", false},
224                 {SEC_RESERVED(9) , "RESERVED", "", false},
225
226                 {NID_SCL, "NID_SCL", "Tizen::Social", true},
227                 {SCL_RESERVED(1) , "RESERVED", "", false},
228                 {SCL_RESERVED(2) , "RESERVED", "", false},
229                 {SCL_RESERVED(3) , "RESERVED", "", false},
230                 {SCL_RESERVED(4) , "RESERVED", "", false},
231                 {SCL_RESERVED(5) , "RESERVED", "", false},
232                 {SCL_RESERVED(6) , "RESERVED", "", false},
233                 {SCL_RESERVED(7) , "RESERVED", "", false},
234                 {SCL_RESERVED(8) , "RESERVED", "", false},
235                 {SCL_RESERVED(9) , "RESERVED", "", false},
236
237                 {NID_SYS, "NID_SYS", "Tizen::System", true},
238                 {SYS_RESERVED(1) , "RESERVED", "", false},
239                 {SYS_RESERVED(2) , "RESERVED", "", false},
240                 {SYS_RESERVED(3) , "RESERVED", "", false},
241                 {SYS_RESERVED(4) , "RESERVED", "", false},
242                 {SYS_RESERVED(5) , "RESERVED", "", false},
243                 {SYS_RESERVED(6) , "RESERVED", "", false},
244                 {SYS_RESERVED(7) , "RESERVED", "", false},
245                 {SYS_RESERVED(8) , "RESERVED", "", false},
246                 {SYS_RESERVED(9) , "RESERVED", "", false},
247
248                 {NID_TEL, "NID_TEL", "Tizen::Telephony", true},
249                 {TEL_RESERVED(1) , "RESERVED", "", false},
250                 {TEL_RESERVED(2) , "RESERVED", "", false},
251                 {TEL_RESERVED(3) , "RESERVED", "", false},
252                 {TEL_RESERVED(4) , "RESERVED", "", false},
253                 {TEL_RESERVED(5) , "RESERVED", "", false},
254                 {TEL_RESERVED(6) , "RESERVED", "", false},
255                 {TEL_RESERVED(7) , "RESERVED", "", false},
256                 {TEL_RESERVED(8) , "RESERVED", "", false},
257                 {TEL_RESERVED(9) , "RESERVED", "", false},
258
259                 {NID_TEXT, "NID_TEXT", "Tizen::Text", true},
260                 {TEXT_RESERVED(1) , "RESERVED", "", false},
261                 {TEXT_RESERVED(2) , "RESERVED", "", false},
262                 {TEXT_RESERVED(3) , "RESERVED", "", false},
263                 {TEXT_RESERVED(4) , "RESERVED", "", false},
264                 {TEXT_RESERVED(5) , "RESERVED", "", false},
265                 {TEXT_RESERVED(6) , "RESERVED", "", false},
266                 {TEXT_RESERVED(7) , "RESERVED", "", false},
267                 {TEXT_RESERVED(8) , "RESERVED", "", false},
268                 {TEXT_RESERVED(9) , "RESERVED", "", false},
269
270                 {NID_UI, "NID_UI", "Tizen::Ui", true},
271                 {NID_UI_ANIM, "NID_UI_ANIM", "Tizen::Ui::Animations", true},
272                 {NID_UI_CTRL, "NID_UI_CTRL", "Tizen::Ui::Controls", true},
273                 {NID_UI_EFFECT, "NID_UI_EFFECT", "Tizen::Ui::Effects", true},
274                 {NID_UI_IME, "NID_UI_IME", "Tizen::Ui::Ime", true},
275                 {NID_UI_SCENES, "NID_UI_SCENES", "Tizen::Ui::Scenes", true},
276                 {UI_RESERVED(6) , "RESERVED", "", false},
277                 {UI_RESERVED(7) , "RESERVED", "", false},
278                 {UI_RESERVED(8) , "RESERVED", "", false},
279                 {UI_RESERVED(9) , "RESERVED", "", false},
280
281                 {NID_UIX, "NID_UIX", "Tizen::Uix", true},
282                 {NID_UIX_SPEECH, "NID_UIX_SPEECH", "Tizen::Uix::Speech", true},
283                 {UIX_RESERVED(2) , "RESERVED", "", false},
284                 {UIX_RESERVED(3) , "RESERVED", "", false},
285                 {UIX_RESERVED(4) , "RESERVED", "", false},
286                 {UIX_RESERVED(5) , "RESERVED", "", false},
287                 {UIX_RESERVED(6) , "RESERVED", "", false},
288                 {UIX_RESERVED(7) , "RESERVED", "", false},
289                 {UIX_RESERVED(8) , "RESERVED", "", false},
290                 {UIX_RESERVED(9) , "RESERVED", "", false},
291
292                 {NID_WEB, "NID_WEB", "Tizen::Web", true},
293                 {NID_WEB_CTRL, "NID_WEB_CTRL", "Tizen::Web::Controls", true},
294                 {NID_WEB_JSON, "NID_WEB_JSON", "Tizen::Web::Json", true},
295                 {WEB_RESERVED(3) , "RESERVED", "", false},
296                 {WEB_RESERVED(4) , "RESERVED", "", false},
297                 {WEB_RESERVED(5) , "RESERVED", "", false},
298                 {WEB_RESERVED(6) , "RESERVED", "", false},
299                 {WEB_RESERVED(7) , "RESERVED", "", false},
300                 {WEB_RESERVED(8) , "RESERVED", "", false},
301                 {WEB_RESERVED(9) , "RESERVED", "", false},
302
303                 {NID_SHELL, "NID_SHELL", "Tizen::Shell", true},
304                 {SHELL_RESERVED(1) , "RESERVED", "", false},
305                 {SHELL_RESERVED(2) , "RESERVED", "", false},
306                 {SHELL_RESERVED(3) , "RESERVED", "", false},
307                 {SHELL_RESERVED(4) , "RESERVED", "", false},
308                 {SHELL_RESERVED(5) , "RESERVED", "", false},
309                 {SHELL_RESERVED(6) , "RESERVED", "", false},
310                 {SHELL_RESERVED(7) , "RESERVED", "", false},
311                 {SHELL_RESERVED(8) , "RESERVED", "", false},
312                 {SHELL_RESERVED(9) , "RESERVED", "", false}
313
314         },
315
316         // Other properties ===========================================================
317         false,          // consoleOut
318         false,          // fileOut
319
320         true,           //platformInfoEnabled;
321         true,           //platformExceptionEnabled;
322
323         true,           // applicationInfoEnabled
324         true,           // applicationDebugEnabled
325         true,           // applicationExceptionEnabled
326
327 };
328
329 void
330 AppLogInternal(const char* pFunction, int lineNumber, const char* pFormat, ...)
331 {
332         va_list args;
333
334         if (!logInfo.applicationInfoEnabled)
335         {
336                 return;
337         }
338
339         va_start(args, pFormat);
340
341         __PrintLog(static_cast<_LogType>(LOG_INFO), pFunction, lineNumber, pFormat, args);
342
343         va_end(args);
344 }
345
346 void
347 AppLogTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...)
348 {
349         va_list args;
350
351         if (!logInfo.applicationInfoEnabled)
352         {
353                 return;
354         }
355
356         va_start(args, pFormat);
357
358         __PrintLogTag(pTag, static_cast<_LogType>(LOG_INFO), pFunction, lineNumber, pFormat, args);
359
360         va_end(args);
361 }
362
363 void
364 AppLogDebugInternal(const char* pFunction, int lineNumber, const char* pFormat, ...)
365 {
366         va_list args;
367
368         if (!logInfo.applicationDebugEnabled)
369         {
370                 return;
371         }
372
373         va_start(args, pFormat);
374
375         __PrintLog(static_cast<_LogType>(LOG_DEBUG), pFunction, lineNumber, pFormat, args);
376
377         va_end(args);
378 }
379
380 void
381 AppLogDebugTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...)
382 {
383         va_list args;
384
385         if (!logInfo.applicationDebugEnabled)
386         {
387                 return;
388         }
389
390         va_start(args, pFormat);
391
392         __PrintLogTag(pTag, static_cast<_LogType>(LOG_DEBUG), pFunction, lineNumber, pFormat, args);
393
394         va_end(args);
395 }
396
397 void
398 AppLogExceptionInternal(const char* pFunction, int lineNumber, const char* pFormat, ...)
399 {
400         va_list args;
401
402         if (!logInfo.applicationExceptionEnabled)
403         {
404                 return;
405         }
406
407         va_start(args, pFormat);
408
409         __PrintLog(static_cast<_LogType>(LOG_EXCEPTION), pFunction, lineNumber, pFormat, args);
410
411         va_end(args);
412 }
413
414 void
415 AppLogExceptionTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...)
416 {
417         va_list args;
418
419         if (!logInfo.applicationExceptionEnabled)
420         {
421                 return;
422         }
423
424         va_start(args, pFormat);
425
426         __PrintLogTag(pTag, static_cast<_LogType>(LOG_EXCEPTION), pFunction, lineNumber, pFormat, args);
427
428         va_end(args);
429 }
430
431 void
432 AppassertInternal(const char* pFunction, int lineNumber)
433 {
434         char logBody[LOG_LEN_MAX];
435         bool platformDebug;
436         int appDebug;
437         char logTag[LOG_MODULE_NAME_LEN_MAX];
438
439         vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &appDebug);
440         platformDebug = (0 == access(FILE_DEBUGMODE, F_OK));
441
442         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", appName);
443         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
444
445         if( (platformDebug == false) && (appDebug == 0) )
446         {
447                 snprintf(logBody, LOG_LEN_MAX, "%s(%d) > App assertion passed due to debug mode off!", pFunction, lineNumber);
448                 ALOG(LOG_ERROR, logTag, "%s", logBody);
449                 return;
450         }
451
452         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > AppAssert!", pFunction, lineNumber);
453         ALOG(LOG_ERROR, logTag, "%s", logBody);
454
455         assert(false);
456 }
457
458 void
459 AppassertfInternal(const char* expr, const char* pFunction, int lineNumber, const char* pFormat, ...)
460 {
461         char logBody[LOG_LEN_MAX];
462         bool platformDebug;
463         int appDebug;
464         va_list args;
465         va_start(args, pFormat);
466
467         vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &appDebug);
468         platformDebug = (0 == access(FILE_DEBUGMODE, F_OK));
469
470         if( (platformDebug == false) && (appDebug == 0) )
471         {
472                 snprintf(logBody, LOG_LEN_MAX, "App assertion passed due to debug mode off! : %s", pFormat);
473                 __PrintLog(static_cast<_LogType>(LOG_USER), pFunction, lineNumber, logBody, args);
474                 va_end(args);
475                 return;
476         }
477
478         __PrintLog(static_cast<_LogType>(LOG_USER), pFunction, lineNumber, pFormat, args);
479
480         va_end(args);
481
482         assert(false);
483 }
484
485
486 void
487 SysLogInternal(unsigned long nid, const char* pFunction, int lineNumber, const char* pFormat, ...)
488 {
489         va_list args;
490
491         if (unlikely(!envLoaded))
492         {
493                 int ret = access("/home/developer/.platforminfologgingmode", F_OK);
494                 if (ret == 0)
495                 {
496                         envPlatformInfoEnabled = true;
497                 }
498                 else
499                 {
500                         envPlatformInfoEnabled = false;
501                 }
502
503                 envLoaded = true;
504         }
505
506         if (!envPlatformInfoEnabled)
507         {
508                 return;
509         }
510
511         if (!logInfo.platformInfoEnabled)
512         {
513                 return;
514         }
515
516         va_start(args, pFormat);
517
518         __PrintSysLog(static_cast<_LogType>(LOG_INFO), static_cast<LogID>(nid), pFunction, lineNumber, pFormat, args);
519
520         va_end(args);
521 }
522
523 void
524 SysLogExceptionInternal(unsigned long nid, result r, const char* pFunction, int lineNumber, const char* pFormat, ...)
525 {
526         va_list args;
527
528         SetLastResult(r);
529
530         if (!logInfo.platformExceptionEnabled)
531         {
532                 return;
533         }
534
535         va_start(args, pFormat);
536
537         __PrintSysLog(static_cast<_LogType>(LOG_EXCEPTION), static_cast<LogID>(nid), pFunction, lineNumber, pFormat, args);
538
539         va_end(args);
540 }
541
542 void
543 SysLogTagInternal(unsigned long nid, const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...)
544 {
545         va_list args;
546
547         if (unlikely(!envLoaded))
548         {
549                 int ret = access("/home/developer/.platforminfologgingmode", F_OK);
550                 if (ret == 0)
551                 {
552                         envPlatformInfoEnabled = true;
553                 }
554                 else
555                 {
556                         envPlatformInfoEnabled = false;
557                 }
558
559                 envLoaded = true;
560         }
561
562         if (!envPlatformInfoEnabled)
563         {
564                 return;
565         }
566
567         if (!logInfo.platformInfoEnabled)
568         {
569                 return;
570         }
571
572         va_start(args, pFormat);
573
574         __PrintSysLogTag(pTag, static_cast<_LogType>(LOG_INFO), static_cast<LogID>(nid), pFunction, lineNumber, pFormat, args);
575
576         va_end(args);
577 }
578
579 void
580 SysLogExceptionTagInternal(unsigned long nid, const char* pTag, result r, const char* pFunction, int lineNumber,
581                                                                  const char* pFormat,
582                                                                  ...)
583 {
584         va_list args;
585
586         SetLastResult(r);
587
588         if (!logInfo.platformExceptionEnabled)
589         {
590                 return;
591         }
592
593         va_start(args, pFormat);
594
595         __PrintSysLogTag(pTag, static_cast<_LogType>(LOG_EXCEPTION), static_cast<LogID>(nid), pFunction, lineNumber, pFormat, args);
596
597         va_end(args);
598 }
599
600
601 void
602 SysAssertInternal(const char* pFileName, int lineNumber, const char* pFunction)
603 {
604
605         bool platformDebug;
606         char logBody[LOG_LEN_MAX];
607
608         platformDebug = (0 == access(FILE_DEBUGMODE, F_OK));
609
610         if(!platformDebug)
611         {
612                 return;
613         }
614
615         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > SysAssert!", pFunction, lineNumber);
616
617         ALOG(LOG_ERROR, LOG_TAG_ASSERT, "%s", logBody);
618
619         assert(false);
620 }
621
622 void
623 SysAssertfInternal(const char* expr, const char* pFunction, int lineNumber, const char* pFormat, ...)
624 {
625         char logBody[LOG_LEN_MAX];
626         bool platformDebug;
627         va_list args;
628
629         platformDebug = (0 == access(FILE_DEBUGMODE, F_OK));
630
631         if(!platformDebug)
632         {
633                 return;
634         }
635
636         va_start(args, pFormat);
637
638         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
639         logBody[LOG_LEN_MAX - 1] = '\0';
640
641         ALOG_VA(LOG_ERROR, LOG_TAG_ASSERT, logBody, args);
642
643         va_end(args);
644
645         assert(false);
646 }
647
648 void
649 SysPropagateInternal(const char* pFunction, int lineNumber, unsigned long nid, result r)
650 {
651         char logBody[PROPAGATION_LEN_MAX];
652         snprintf(logBody, PROPAGATION_LEN_MAX, "%s(%d) > [%s] Propagating to caller...", pFunction, lineNumber, GetErrorMessage(r));
653
654         char logTag[LOG_MODULE_NAME_LEN_MAX];
655         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", __GetModuleName(static_cast<LogID>(nid)));
656
657         ALOG(LOG_ERROR, logTag, "%s", logBody);
658 }
659
660 void
661 SysTryReturnResultInternal(unsigned long id, result r, const char* pFunction, int lineNumber, const char* pFormat, ...)
662 {
663
664         va_list args;
665
666         SetLastResult(r);
667
668         if (!logInfo.platformExceptionEnabled)
669         {
670                 return;
671         }
672
673         if (!iniLoaded)
674         {
675                 __InitializeLogInfo();
676         }
677
678         if (!__GetEnableInfo(static_cast<LogID>(id)))
679         {
680                 return;
681         }
682
683         va_start(args, pFormat);
684
685         char logBody[LOG_LEN_MAX];
686         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > [%s] %s", pFunction, lineNumber, GetErrorMessage(r), pFormat);
687         logBody[LOG_LEN_MAX -1] = '\0';
688
689         char logTag[LOG_MODULE_NAME_LEN_MAX];
690
691         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", __GetModuleName(static_cast<LogID>(id)));
692         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
693
694         ALOG_VA(LOG_ERROR, logTag, logBody, args);
695
696         va_end(args);
697 }
698
699
700
701 void
702 __PrintLog(_LogType type, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
703 {
704         if (unlikely(!iniLoaded))
705         {
706                 __InitializeLogInfo();
707         }
708
709         char logBody[LOG_LEN_MAX];
710         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
711         logBody[LOG_LEN_MAX - 1] = '\0';
712
713         char logTag[LOG_MODULE_NAME_LEN_MAX];
714
715         if (unlikely(!appNameLoaded))
716         {
717                 appNameLoaded = true;
718                 strncpy(appName, appinfo_get_execname(), LOG_MODULE_NAME_LEN_MAX);
719         }
720         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", appName);
721
722         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
723
724         switch (type)
725         {
726         case static_cast<_LogType>(LOG_INFO):
727                 ALOG_VA(LOG_INFO, logTag, logBody, args);
728                 break;
729
730         case static_cast<_LogType>(LOG_DEBUG):
731                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
732                 break;
733
734         case static_cast<_LogType>(LOG_EXCEPTION):
735                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
736                 break;
737
738         case static_cast<_LogType>(LOG_USER):
739                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
740                 break;
741
742         default:
743                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
744                 break;
745         }
746 }
747
748 void
749 __PrintLogTag(const char* pTag, _LogType type, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
750 {
751         if (unlikely(!iniLoaded))
752         {
753                 __InitializeLogInfo();
754         }
755
756         char logBody[LOG_LEN_MAX];
757         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
758         logBody[LOG_LEN_MAX -1] = '\0';
759
760         char logTag[LOG_TAG_LEN_MAX];
761
762         snprintf(logTag, LOG_TAG_LEN_MAX, "%s", pTag);
763         logTag[LOG_TAG_LEN_MAX - 1] = '\0';
764
765         switch (type)
766         {
767         case static_cast<_LogType>(LOG_INFO):
768                 ALOG_VA(LOG_INFO, logTag, logBody, args);
769                 break;
770
771         case static_cast<_LogType>(LOG_DEBUG):
772                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
773                 break;
774
775         case static_cast<_LogType>(LOG_EXCEPTION):
776                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
777                 break;
778
779         case static_cast<_LogType>(LOG_USER):
780                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
781                 break;
782
783         default:
784                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
785                 break;
786         }
787 }
788
789
790 void
791 __PrintSysLog(_LogType type, LogID id, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
792 {
793         if (unlikely(!iniLoaded))
794         {
795                 __InitializeLogInfo();
796         }
797
798         if (!__GetEnableInfo(static_cast<LogID>(id)))
799         {
800                 return;
801         }
802
803         char logBody[LOG_LEN_MAX];
804         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
805         logBody[LOG_LEN_MAX -1] = '\0';
806
807         char logTag[LOG_MODULE_NAME_LEN_MAX];
808
809         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", __GetModuleName(static_cast<LogID>(id)));
810         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
811
812         switch (type)
813         {
814         case static_cast<_LogType>(LOG_INFO):
815                 ALOG_VA(LOG_INFO, logTag, logBody, args);
816                 break;
817
818         case static_cast<_LogType>(LOG_DEBUG):
819                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
820                 break;
821
822         case static_cast<_LogType>(LOG_EXCEPTION):
823                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
824                 break;
825
826         case static_cast<_LogType>(LOG_USER):
827                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
828                 break;
829
830         default:
831                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
832                 break;
833         }
834 }
835
836 void
837 __PrintSysLogTag(const char* pTag, _LogType type, LogID id, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
838 {
839         if (unlikely(!iniLoaded))
840         {
841                 __InitializeLogInfo();
842         }
843
844         if (!__GetEnableInfo(static_cast<LogID>(id)))
845         {
846                 return;
847         }
848
849         char logBody[LOG_LEN_MAX];
850         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
851         logBody[LOG_LEN_MAX -1] = '\0';
852
853         char logTag[LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX];
854
855         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX, "%s | %s", __GetModuleName(static_cast<LogID>(id)), pTag);
856         logTag[LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX - 1] = '\0';
857
858         switch (type)
859         {
860         case static_cast<_LogType>(LOG_INFO):
861                 ALOG_VA(LOG_INFO, logTag, logBody, args);
862                 break;
863
864         case static_cast<_LogType>(LOG_DEBUG):
865                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
866                 break;
867
868         case static_cast<_LogType>(LOG_EXCEPTION):
869                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
870                 break;
871
872         case static_cast<_LogType>(LOG_USER):
873                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
874                 break;
875
876         default:
877                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
878                 break;
879         }
880 }
881
882 const char*
883 __GetModuleName(LogID id)
884 {
885         return logInfo.logTable[id].logModuleName;
886 }
887
888 bool
889 __GetEnableInfo(LogID id)
890 {
891         return logInfo.logTable[id].loggingEnabled;
892 }
893
894 void
895 __InitializeLogInfo()
896 {
897         iniLoaded = true;
898
899         dictionary *pDic;
900         pDic = iniparser_load("/opt/usr/etc/system-log.ini");
901
902         if (pDic == NULL)
903         {
904                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION] opening ini file failed.\n");
905                 return;
906         }
907
908         __InitializeLogLevel(pDic);
909         __InitializePlatformModule(pDic);
910
911         iniparser_freedict(pDic);
912 }
913
914 void
915 __InitializeLogLevel(dictionary *pDic)
916 {
917         char sectApp[]="Application";
918         char sectPlatform[]="Platform";
919
920         char entryInfo[]="INFO";
921         char entryDebug[]="DEBUG";
922         char entryException[]="EXCEPTION";
923
924         char strYes[]="YES";
925         char *pVal;
926
927         char keyString[LOG_MODULE_NAME_LEN_MAX*2];
928         int retVal;
929
930         sprintf(keyString, "%s:%s", sectApp, entryInfo);
931         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
932         pVal = iniparser_getstr(pDic, keyString);
933         if (pVal == NULL)
934         {
935                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/INFO failed.\n");
936                 return;
937         }
938         retVal = strcmp(strYes, pVal);
939         logInfo.applicationInfoEnabled = (retVal == 0);
940
941         sprintf(keyString, "%s:%s", sectApp, entryDebug);
942         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
943         pVal = iniparser_getstr(pDic, keyString);
944         if (pVal == NULL)
945         {
946                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/DEBUG failed.\n");
947                 return;
948         }
949         retVal = strcmp(strYes, pVal);
950         logInfo.applicationDebugEnabled = (retVal == 0);
951
952         sprintf(keyString, "%s:%s", sectApp, entryException);
953         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
954         pVal = iniparser_getstr(pDic, keyString);
955         if (pVal == NULL)
956         {
957                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/EXCEPTION failed.\n");
958                 return;
959         }
960         retVal = strcmp(strYes, pVal);
961         logInfo.applicationExceptionEnabled = (retVal == 0);
962
963         sprintf(keyString, "%s:%s", sectPlatform, entryInfo);
964         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
965         pVal = iniparser_getstr(pDic, keyString);
966         if (pVal == NULL)
967         {
968                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Platform/INFO failed.\n");
969                 return;
970         }
971         retVal = strcmp(strYes, pVal);
972         logInfo.platformInfoEnabled = (retVal == 0);
973
974         sprintf(keyString, "%s:%s", sectPlatform, entryException);
975         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
976         pVal = iniparser_getstr(pDic, keyString);
977         if (pVal == NULL)
978         {
979                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Platform/EXCEPTION failed.\n");
980                 return;
981         }
982         retVal = strcmp(strYes, pVal);
983         logInfo.platformExceptionEnabled = (retVal == 0);
984 }
985
986 void
987 __InitializePlatformModule(dictionary *pDic)
988 {
989         char sectModule[]="PlatformModules";
990         char strYes[]="YES";
991
992         char *pVal;
993         int retVal;
994
995         char keyString[LOG_MODULE_NAME_LEN_MAX*2];
996
997         for (int i = 0; i < NID_MAX; i++)
998         {
999
1000                 sprintf(keyString, "%s:%s", sectModule, logInfo.logTable[i].logIDName);
1001                 keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
1002                 pVal = iniparser_getstr(pDic, keyString);
1003                 if (pVal == NULL)
1004                 {
1005                         ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION] finding PlatformModules/%ls failed.\n", logInfo.logTable[i].logIDName);
1006                 }
1007                 else
1008                 {
1009                         retVal = strcmp(strYes, pVal);
1010                         logInfo.logTable[i].loggingEnabled = (retVal == 0);
1011                 }
1012         }
1013 }
1014