FIoRegistry.h => iniparser.h
[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 <vconf.h>
23 #include <dlog.h>
24 #include <assert.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
30 #include <FAppTypes.h>
31 #include <FBaseLog.h>
32 #include <FBaseSysLog.h>
33 #include "FBase_Log.h"
34 #include "FBase_StringConverter.h"
35 #include "FApp_AppInfo.h"
36
37 extern "C" {
38 #include <iniparser.h>
39 }
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Io;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::App;
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                 char* pAppName = _StringConverter::CopyToCharArrayN(_AppInfo::GetAppExecutableName());
719                 strncpy(appName, pAppName, LOG_MODULE_NAME_LEN_MAX);
720                 delete [] pAppName;
721         }
722         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", appName);
723
724         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
725
726         switch (type)
727         {
728         case static_cast<_LogType>(LOG_INFO):
729                 ALOG_VA(LOG_INFO, logTag, logBody, args);
730                 break;
731
732         case static_cast<_LogType>(LOG_DEBUG):
733                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
734                 break;
735
736         case static_cast<_LogType>(LOG_EXCEPTION):
737                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
738                 break;
739
740         case static_cast<_LogType>(LOG_USER):
741                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
742                 break;
743
744         default:
745                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
746                 break;
747         }
748 }
749
750 void
751 __PrintLogTag(const char* pTag, _LogType type, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
752 {
753         if (unlikely(!iniLoaded))
754         {
755                 __InitializeLogInfo();
756         }
757
758         char logBody[LOG_LEN_MAX];
759         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
760         logBody[LOG_LEN_MAX -1] = '\0';
761
762         char logTag[LOG_TAG_LEN_MAX];
763
764         snprintf(logTag, LOG_TAG_LEN_MAX, "%s", pTag);
765         logTag[LOG_TAG_LEN_MAX - 1] = '\0';
766
767         switch (type)
768         {
769         case static_cast<_LogType>(LOG_INFO):
770                 ALOG_VA(LOG_INFO, logTag, logBody, args);
771                 break;
772
773         case static_cast<_LogType>(LOG_DEBUG):
774                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
775                 break;
776
777         case static_cast<_LogType>(LOG_EXCEPTION):
778                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
779                 break;
780
781         case static_cast<_LogType>(LOG_USER):
782                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
783                 break;
784
785         default:
786                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
787                 break;
788         }
789 }
790
791
792 void
793 __PrintSysLog(_LogType type, LogID id, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
794 {
795         if (unlikely(!iniLoaded))
796         {
797                 __InitializeLogInfo();
798         }
799
800         if (!__GetEnableInfo(static_cast<LogID>(id)))
801         {
802                 return;
803         }
804
805         char logBody[LOG_LEN_MAX];
806         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
807         logBody[LOG_LEN_MAX -1] = '\0';
808
809         char logTag[LOG_MODULE_NAME_LEN_MAX];
810
811         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX, "%s", __GetModuleName(static_cast<LogID>(id)));
812         logTag[LOG_MODULE_NAME_LEN_MAX - 1] = '\0';
813
814         switch (type)
815         {
816         case static_cast<_LogType>(LOG_INFO):
817                 ALOG_VA(LOG_INFO, logTag, logBody, args);
818                 break;
819
820         case static_cast<_LogType>(LOG_DEBUG):
821                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
822                 break;
823
824         case static_cast<_LogType>(LOG_EXCEPTION):
825                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
826                 break;
827
828         case static_cast<_LogType>(LOG_USER):
829                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
830                 break;
831
832         default:
833                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
834                 break;
835         }
836 }
837
838 void
839 __PrintSysLogTag(const char* pTag, _LogType type, LogID id, const char* pFunction, int lineNumber, const char* pFormat, va_list args)
840 {
841         if (unlikely(!iniLoaded))
842         {
843                 __InitializeLogInfo();
844         }
845
846         if (!__GetEnableInfo(static_cast<LogID>(id)))
847         {
848                 return;
849         }
850
851         char logBody[LOG_LEN_MAX];
852         snprintf(logBody, LOG_LEN_MAX, "%s(%d) > %s", pFunction, lineNumber, pFormat);
853         logBody[LOG_LEN_MAX -1] = '\0';
854
855         char logTag[LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX];
856
857         snprintf(logTag, LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX, "%s | %s", __GetModuleName(static_cast<LogID>(id)), pTag);
858         logTag[LOG_MODULE_NAME_LEN_MAX + LOG_TAG_LEN_MAX - 1] = '\0';
859
860         switch (type)
861         {
862         case static_cast<_LogType>(LOG_INFO):
863                 ALOG_VA(LOG_INFO, logTag, logBody, args);
864                 break;
865
866         case static_cast<_LogType>(LOG_DEBUG):
867                 ALOG_VA(LOG_DEBUG, logTag, logBody, args);
868                 break;
869
870         case static_cast<_LogType>(LOG_EXCEPTION):
871                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
872                 break;
873
874         case static_cast<_LogType>(LOG_USER):
875                 ALOG_VA(LOG_ERROR, logTag, logBody, args);
876                 break;
877
878         default:
879                 ALOG_VA(LOG_DEBUG, LOG_TAG_NULL, logBody, args);
880                 break;
881         }
882 }
883
884 const char*
885 __GetModuleName(LogID id)
886 {
887         return logInfo.logTable[id].logModuleName;
888 }
889
890 bool
891 __GetEnableInfo(LogID id)
892 {
893         return logInfo.logTable[id].loggingEnabled;
894 }
895
896 void
897 __InitializeLogInfo()
898 {
899         iniLoaded = true;
900
901         dictionary *pDic;
902         pDic = iniparser_load("/opt/usr/etc/system-log.ini");
903
904         if (pDic == NULL)
905         {
906                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION] opening ini file failed.\n");
907                 return;
908         }
909
910         __InitializeLogLevel(pDic);
911         __InitializePlatformModule(pDic);
912
913         iniparser_freedict(pDic);
914 }
915
916 void
917 __InitializeLogLevel(dictionary *pDic)
918 {
919         char sectApp[]="Application";
920         char sectPlatform[]="Platform";
921
922         char entryInfo[]="INFO";
923         char entryDebug[]="DEBUG";
924         char entryException[]="EXCEPTION";
925
926         char strYes[]="YES";
927         char *pVal;
928
929         char keyString[LOG_MODULE_NAME_LEN_MAX*2];
930         int retVal;
931
932         sprintf(keyString, "%s:%s", sectApp, entryInfo);
933         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
934         pVal = iniparser_getstr(pDic, keyString);
935         if (pVal == NULL)
936         {
937                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/INFO failed.\n");
938                 return;
939         }
940         retVal = strcmp(strYes, pVal);
941         logInfo.applicationInfoEnabled = (retVal == 0);
942
943         sprintf(keyString, "%s:%s", sectApp, entryDebug);
944         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
945         pVal = iniparser_getstr(pDic, keyString);
946         if (pVal == NULL)
947         {
948                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/DEBUG failed.\n");
949                 return;
950         }
951         retVal = strcmp(strYes, pVal);
952         logInfo.applicationDebugEnabled = (retVal == 0);
953
954         sprintf(keyString, "%s:%s", sectApp, entryException);
955         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
956         pVal = iniparser_getstr(pDic, keyString);
957         if (pVal == NULL)
958         {
959                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Application/EXCEPTION failed.\n");
960                 return;
961         }
962         retVal = strcmp(strYes, pVal);
963         logInfo.applicationExceptionEnabled = (retVal == 0);
964
965         sprintf(keyString, "%s:%s", sectPlatform, entryInfo);
966         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
967         pVal = iniparser_getstr(pDic, keyString);
968         if (pVal == NULL)
969         {
970                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Platform/INFO failed.\n");
971                 return;
972         }
973         retVal = strcmp(strYes, pVal);
974         logInfo.platformInfoEnabled = (retVal == 0);
975
976         sprintf(keyString, "%s:%s", sectPlatform, entryException);
977         keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
978         pVal = iniparser_getstr(pDic, keyString);
979         if (pVal == NULL)
980         {
981                 ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION]  finding Platform/EXCEPTION failed.\n");
982                 return;
983         }
984         retVal = strcmp(strYes, pVal);
985         logInfo.platformExceptionEnabled = (retVal == 0);
986 }
987
988 void
989 __InitializePlatformModule(dictionary *pDic)
990 {
991         char sectModule[]="PlatformModules";
992         char strYes[]="YES";
993
994         char *pVal;
995         int retVal;
996
997         char keyString[LOG_MODULE_NAME_LEN_MAX*2];
998
999         for (int i = 0; i < NID_MAX; i++)
1000         {
1001
1002                 sprintf(keyString, "%s:%s", sectModule, logInfo.logTable[i].logIDName);
1003                 keyString[LOG_MODULE_NAME_LEN_MAX*2-1] = '\0';
1004                 pVal = iniparser_getstr(pDic, keyString);
1005                 if (pVal == NULL)
1006                 {
1007                         ALOG(LOG_ERROR, LOG_TAG, "[EXCEPTION] finding PlatformModules/%ls failed.\n", logInfo.logTable[i].logIDName);
1008                 }
1009                 else
1010                 {
1011                         retVal = strcmp(strYes, pVal);
1012                         logInfo.logTable[i].loggingEnabled = (retVal == 0);
1013                 }
1014         }
1015 }
1016