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