sync with tizen_2.0
[platform/framework/native/appfw.git] / inc / FBaseLog.h
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                FBaseLog.h
20  * @brief               This is the header file for the diagnostic types.
21  *
22  * This header file defines the diagnostic types.
23  *
24  */
25 #ifndef _FBASE_LOG_H_
26 #define _FBASE_LOG_H_
27
28 #include <new>
29 #include <FOspConfig.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36  * @defgroup GroupMacros Debugging Macros
37  *
38  * This page describes Tizen debugging macros.
39  *
40  * @since 2.0
41  */
42
43 #if defined(_APP_LOG) || defined(_OSP_DEBUG_) || defined(_DEBUG)
44
45 // AppLog Macro
46
47 #define AppLog(...)                     AppLogInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
48 #define AppLogDebug(...)                AppLogDebugInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
49 #define AppLogException(...)            AppLogExceptionInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
50
51 #define AppLogTag(tag, ...)                     AppLogTagInternal(tag, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
52 #define AppLogDebugTag(tag, ...)                AppLogDebugTagInternal(tag, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
53 #define AppLogExceptionTag(tag, ...)            AppLogExceptionTagInternal(tag, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
54
55 #define AppLogIf(expression, ...) \
56         if (expression) { \
57                 AppLogInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__);     \
58         } \
59         else {;}
60
61 #define AppLogDebugIf(expression, ...) \
62         if (expression) { \
63                 AppLogDebugInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__); \
64         } \
65         else {;}
66
67 #define AppLogExceptionIf(expression, ...) \
68         if (expression) { \
69                 AppLogExceptionInternal(__PRETTY_FUNCTION__, __LINE__, __VA_ARGS__); \
70         } \
71         else {;}
72
73 #define AppAssert(condition) \
74         if (!(condition)) { \
75                 AppassertInternal(__PRETTY_FUNCTION__, __LINE__); \
76         } \
77         else {;}
78
79 #define AppAssertf(condition, ...)      \
80         if (!(condition)) { \
81                 AppassertfInternal(# condition, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__); \
82         } \
83         else {;}
84 #else // defined(_APP_LOG)
85
86 /**
87  * @addtogroup  GroupMacros
88  *
89  * @{
90  */
91 /**
92  * This macro allows display of arbitrary messages for future examination.
93  *
94  * @since 2.0
95  *
96  * @param[in]   ...                     The message to display
97  *
98  * The following example demonstrates how to use the AppLog macro.
99  *
100  * @code
101  *      Bool
102  *      MyEngine::Init(int value)
103  *      {
104  *         AppLog("Initialization successful.");
105  *
106  *         return true;
107  *      }
108  * @endcode
109  * @hideinitializer
110  */
111 #define AppLog(...)
112
113 /**
114  * This macro must be added in your program if you want the debug messages to be displayed in the output.
115  *
116  * @since 2.0
117  *
118  * @param[in]   ...                     The message to display
119  *
120  *  @image html debugging_applog_output.png
121  *
122  * The following example demonstrates how to use the AppLogDebug macro.
123  *
124  * @code
125  *      Bool
126  *      MyEngine::Init(int value)
127  *      {
128  *         AppLogDebug("Invoked with value: %d", value);
129  *
130  *         //...
131  *
132  *         AppLogDebug("Exit.");
133  *
134  *         return true;
135  *      }
136  * @endcode
137  * @hideinitializer
138  */
139 #define AppLogDebug(...)
140
141 /**
142  * This macro must be added in your program if you want the exception messages to be displayed in the output.
143  *
144  * @since 2.0
145  *
146  * @param[in]   ...                     The message to display
147  *
148  * The following example demonstrates how to use the AppLogException macro.
149  *
150  * @code
151  *      Bool
152  *      MyEngine::Init(int value)
153  *      {
154  *         AppLogDebug("Invoked with value: %d", value);
155  *         //...
156  *         if (something_wrong)   // The Try family macros can be used instead.
157  *         {
158  *            AppLogException("An unexpected error occurred.");
159  *
160  *            return false;
161  *         }
162  *
163  *         AppLog("Initialization successful.");
164  *         AppLogDebug("Exit.");
165  *
166  *         return true;
167  *      }
168  * @endcode
169  * @hideinitializer
170  */
171 #define AppLogException(...)
172
173 /**
174  * This macro is used to check logical errors in a program.
175  * If the assertion fails, the current process is killed.
176  *
177  * @since 2.0
178  *
179  * @param[in]   condition  The condition that is expected to be true
180  * @hideinitializer
181  */
182 #define AppAssert(condition)
183
184 /**
185  * This macro is used to check logical errors in a program.
186  * If the assertion fails, a message is printed on the console and the current process is killed.
187  *
188  * @since 2.0
189  *
190  * @param[in]   condition               The condition that is expected to be true
191  * @param[in]   ...                     The message to print, if the assertion fails
192  *
193  * @image html debugging_appassert.png
194  *
195  * The following example demonstrates how to use the Assert macro.
196  *
197  * @code
198  *      result
199  *      MyClass::DoSomething(void)
200  *      {
201  *         result r = E_SUCCESS;
202  *         r = mutex.Acquire();
203  *         // Do something
204  *         r = mutex.Release();
205  *
206  *         // If false, console prints "Mutex Release Failed."
207  *         // and the process is killed.
208  *         AppAssertf(r == E_SUCCESS, "Mutex Release Failed.");
209  *
210  *         return r;
211  *      }
212  * @endcode
213  * @hideinitializer
214  */
215 #define AppAssertf(condition, ...)
216
217 /**
218  * This macro is added in a program to display a message when an expression is @c true.
219  *
220  * @since 2.0
221  *
222  * @param[in]   expression              The expression to evaluate
223  * @param[in]   ...                     The message to display
224  *
225  * The following example demonstrates how to use the AppLogIf macro.
226  *
227  * @code
228  *      Bool
229  *      MyEngine::Init(int value)
230  *      {
231  *         AppLogIf(value !=0, "Invoked with value: %d", value);
232  *         //...
233  *
234  *         return true;
235  *      }
236  * @endcode
237  * @hideinitializer
238  */
239 #define AppLogIf(expression, ...)
240
241 /**
242  * This macro is added in a program to display a debug message when an expression is @c true.
243  *
244  * @since 2.0
245  *
246  * @param[in]   expression              The expression to evaluate
247  * @param[in]   ...                     The message to display
248  *
249  * The following example demonstrates how to use the AppLogDebugIf macro.
250  *
251  * @code
252  *      Bool
253  *      MyEngine::Init(int value)
254  *      {
255  *         AppLogDebugIf(value !=0, "Invoked with value: %d", value);
256  *         //...
257  *
258  *         return true;
259  *      }
260  * @endcode
261  * @hideinitializer
262  */
263 #define AppLogDebugIf(expression, ...)
264
265 /**
266  * This macro is added in a program to display an exception message when an expression is @c true.
267  *
268  * @since 2.0
269  *
270  * @param[in]   expression              The expression to evaluate
271  * @param[in]   ...                     The message to display
272  *
273  * The following example demonstrates how to use the AppLogExceptionIf macro.
274  *
275  * @code
276  *      Bool
277  *      MyEngine::Init(int value)
278  *      {
279  *         int status;
280  *
281  *         AppLogExceptionIf(status != 0, "status : %d.", status);
282  *         //...
283  *
284  *         return true;
285  *      }
286  * @endcode
287  * @hideinitializer
288  */
289 #define AppLogExceptionIf(expression, ...)
290
291 /**
292  * This macro is added in a program to display an info message with a tag.
293  *
294  * @since 2.0
295  *
296  * @param[in]   tag                     Used to identify the source of a log message
297  * @param[in]   ...                     The message to display
298  *
299  * The following example demonstrates how to use the AppLogTag macro.
300  *
301  * @code
302  *      Bool
303  *      MyEngine::Init(int value)
304  *      {
305  *         int status;
306  *
307  *         AppLogTag("MyTag", "Initialization successful.");
308  *         //...
309  *
310  *         return true;
311  *      }
312  * @endcode
313  * @hideinitializer
314  */
315 #define AppLogTag(tag, ...)
316
317 /**
318  * This macro is added in a program to display a debug message with a tag.
319  *
320  * @since 2.0
321  *
322  * @param[in]   tag                     Used to identify the source of a log message
323  * @param[in]   ...                     The message to display
324  *
325  * The following example demonstrates how to use the AppLogDebugTag macro.
326  *
327   * @code
328  *      Bool
329  *      MyEngine::Init(int value)
330  *      {
331  *         AppLogDebugTag("MyTag", "Invoked with value: %d", value);
332  *
333  *         //...
334  *
335  *         AppLogDebugTag("MyTag", "Exit.");
336  *
337  *         return true;
338  *      }
339  * @endcode
340  * @hideinitializer
341  */
342 #define AppLogDebugTag(tag, ...)
343
344 /**
345  * This macro is added in a program to display an exception message with a tag.
346  *
347  * @since 2.0
348  *
349  * @param[in]   tag                     Used to identify the source of a log message
350  * @param[in]   ...                     The message to display
351  *
352  * The following example demonstrates how to use the AppLogExceptionTag macro.
353  *
354  * @code
355  *      Bool
356  *      MyEngine::Init(int value)
357  *      {
358  *         AppLogDebug("Invoked with value: %d", value);
359  *         //...
360  *         if (something_wrong)   // The Try family macros can be used instead.
361  *         {
362  *            AppLogExceptionTag("MyTag", "An unexpected error occurred.");
363  *
364  *            return false;
365  *         }
366  *
367  *         AppLog("Initialization successful.");
368  *         AppLogDebug("Exit.");
369  *
370  *         return true;
371  *      }
372  * @endcode
373  * @hideinitializer
374  */
375 #define AppLogExceptionTag(tag, ...)
376 /** @} */
377 #endif // defined(_APP_LOG)
378
379 // Try Macros
380 /**
381  * @addtogroup  GroupMacros
382  *
383  * @{
384  */
385 /**
386  * If the condition is @c false, it prints a message, evaluates a cleanup expression,
387  * and goes to <tt>CATCH</tt>.
388  *
389  * @since 2.0
390  *
391  * @param[in]   condition               The condition that is expected to be true
392  * @param[in]   expr                    Expressions that are evaluated before going to CATCH label
393  * @param[in]   ...                     The message to display
394  *
395  * The following example demonstrates how to use the Try macro.
396  *
397  * @code
398  *      const  A*
399  *      MyClass::DoSomething(const wchar_t* pValue)
400  *      {
401  *         result r = E_SUCCESS;
402  *
403  *         // Do something...
404  *
405  *         // If pValue is null, print "pValue == null" to the
406  *         // console and return E_INVALID_ARG.
407  *         TryCatch(pValue != null, r = E_INVALID_ARG, "pValue == null");
408  *
409  *         SetLastResult(E_SUCCESS);
410  *
411  *         return _pValue;
412  *
413  *         CATCH:
414  *            SetLastResult(r);
415  *
416  *            return null;
417  *      }
418  * @endcode
419  * @hideinitializer
420  */
421 #define TryCatch(condition, expr, ...) \
422         if (!(condition)) { \
423                 AppLogException(__VA_ARGS__); \
424                 expr; \
425                 goto CATCH;     \
426         } \
427         else {;}
428
429 /**
430  * If the condition is @c false, it prints a message, sets the last result, evaluates a cleanup expression
431  * and goes to <tt>CATCH</tt>.
432  *
433  * @since 2.0
434  *
435  * @param[in]   condition               The condition that is expected to be true
436  * @param[in]   expr                    Expressions that are evaluated before going to CATCH label
437  * @param[in]   r                       The last result to set
438  * @param[in]   ...                     The message to display
439  * @hideinitializer
440  */
441 #define TryCatchResult(condition, expr, r, ...) \
442         if (!(condition)) { \
443                 SetLastResult(r); \
444                 AppLogException(__VA_ARGS__); \
445                 expr; \
446                 goto CATCH;     \
447         } \
448         else {;}
449
450 /**
451  * If the condition is @c false, the message is printed and a value is returned.
452  *
453  * @since 2.0
454  *
455  * @param[in]   condition               The condition that is expected to be true
456  * @param[in]   returnValue             The value to return when the condition is @c false
457  * @param[in]   ...                     The message to display
458  * @hideinitializer
459  */
460 #define TryReturn(condition, returnValue, ...)  \
461         if (!(condition)) { \
462                 AppLogException(__VA_ARGS__); \
463                 return returnValue;     \
464         } \
465         else {;}
466
467 /**
468  * If the condition is @c false, the message is printed, sets the last result and a value is returned.
469  *
470  * @since 2.0
471  *
472  * @param[in]   condition               The condition that is expected to be true
473  * @param[in]   returnValue             The value to return when the condition is @c false
474  * @param[in]   r                       The last result to set
475  * @param[in]   ...                     The message to display
476  * @hideinitializer
477  */
478 #define TryReturnResult(condition, returnValue, r, ...) \
479         if (!(condition)) { \
480                 SetLastResult(r); \
481                 AppLogException(__VA_ARGS__); \
482                 return returnValue;     \
483         } \
484         else {;}
485
486 /**
487  * If the condition is @c false, the message is printed, sets the last result and no value is returned.
488  *
489  * @since 2.0
490  *
491  * @param[in]   condition               The condition that is expected to be true
492  * @param[in]   r                       The last result to set
493  * @param[in]   ...                     The message to display
494  * @hideinitializer
495  */
496 #define TryReturnVoidResult(condition, r, ...)  \
497         if (!(condition)) { \
498                 SetLastResult(r); \
499                 AppLogException(__VA_ARGS__); \
500                 return; \
501         } \
502         else {;}
503
504 /**
505  * If the condition is @c false, the message is printed and no value is returned.
506  *
507  * @since 2.0
508  *
509  * @param[in]   condition               The condition that is expected to be true
510  * @param[in]   ...                     The message to display
511  * @hideinitializer
512  */
513 #define TryReturnVoid(condition, ...) \
514         if (!(condition)) { \
515                 AppLogException(__VA_ARGS__); \
516                 return; \
517         } \
518         else {;}
519
520 /**
521  * If the condition is @c false, the message is printed.
522  *
523  * @since 2.0
524  *
525  * @param[in]   condition               The condition that is expected to be true
526  * @param[in]   ...                     The message to display
527  * @hideinitializer
528  */
529 #define TryLog(condition, ...)  \
530         if (!(condition)) { \
531                 AppLog(__VA_ARGS__); \
532         } \
533         else {;}
534
535
536 // TryTag Macros
537
538 /**
539  * If the condition is @c false, it prints a message with a tag, evaluates a cleanup expression
540  * and goes to <tt>CATCH</tt>.
541  *
542  * @since 2.0
543  *
544  * @param[in]   tag                     Used to identify the source of a log message
545  * @param[in]   condition               The condition that is expected to be true
546  * @param[in]   expr                    Expressions that are evaluated before going to CATCH label
547  * @param[in]   ...                     The message to display
548  * @hideinitializer
549  */
550 #define TryCatchTag(tag, condition, expr, ...) \
551         if (!(condition)) { \
552                 AppLogExceptionTag(tag, __VA_ARGS__); \
553                 expr; \
554                 goto CATCH;     \
555         } \
556         else {;}
557
558 /**
559  * If the condition is @c false, it prints a message with a tag, sets the last result, evaluates a cleanup expression,
560  * and goes to <tt>CATCH</tt>.
561  *
562  * @since 2.0
563  *
564  * @param[in]   tag                     Used to identify the source of a log message
565  * @param[in]   condition               The condition that is expected to be true
566  * @param[in]   expr                    Expressions that are evaluated before going to CATCH label
567  * @param[in]   r                       The last result to set
568  * @param[in]   ...                     The message to display
569  * @hideinitializer
570  */
571 #define TryCatchResultTag(tag, condition, expr, r, ...) \
572         if (!(condition)) { \
573                 SetLastResult(r); \
574                 AppLogExceptionTag(tag, __VA_ARGS__); \
575                 expr; \
576                 goto CATCH;     \
577         } \
578         else {;}
579
580 /**
581  * If the condition is @c false, the message is printed with a tag and a value is returned.
582  *
583  * @since 2.0
584  *
585  * @param[in]   tag                     Used to identify the source of a log message
586  * @param[in]   condition               The condition that is expected to be true
587  * @param[in]   returnValue             The value to return when the condition is @c false
588  * @param[in]   ...                     The message to display
589  * @hideinitializer
590  */
591 #define TryReturnTag(tag, condition, returnValue, ...)  \
592         if (!(condition)) { \
593                 AppLogExceptionTag(tag, __VA_ARGS__); \
594                 return returnValue;     \
595         } \
596         else {;}
597
598 /**
599  * If the condition is @c false, the message is printed with a tag, sets the last result and a value is returned.
600  *
601  * @since 2.0
602  *
603  * @param[in]   tag                     Used to identify the source of a log message
604  * @param[in]   condition               The condition that is expected to be true
605  * @param[in]   returnValue             The value to return when the condition is @c false
606  * @param[in]   r                       The last result to set
607  * @param[in]   ...                     The message to display
608  * @hideinitializer
609  */
610 #define TryReturnResultTag(tag, condition, returnValue, r, ...) \
611         if (!(condition)) { \
612                 SetLastResult(r); \
613                 AppLogExceptionTag(tag, __VA_ARGS__); \
614                 return returnValue;     \
615         } \
616         else {;}
617
618 /**
619  * If the condition is @c false, the message is printed with a tag, sets the last result and no value is returned.
620  *
621  * @since 2.0
622  *
623  * @param[in]   tag                     Used to identify the source of a log message
624  * @param[in]   condition               The condition that is expected to be true
625  * @param[in]   r                       The last result to set
626  * @param[in]   ...                     The message to display
627  * @hideinitializer
628  */
629 #define TryReturnVoidResultTag(tag, condition, r, ...)  \
630         if (!(condition)) { \
631                 SetLastResult(r); \
632                 AppLogExceptionTag(tag, __VA_ARGS__); \
633                 return; \
634         } \
635         else {;}
636
637 /**
638  * If the condition is @c false, the message is printed with a tag and no value is returned.
639  *
640  * @since 2.0
641  *
642  * @param[in]   tag                     Used to identify the source of a log message
643  * @param[in]   condition               The condition that is expected to be true
644  * @param[in]   ...                     The message to display
645  * @hideinitializer
646  */
647 #define TryReturnVoidTag(tag, condition, ...) \
648         if (!(condition)) { \
649                 AppLogExceptionTag(tag, __VA_ARGS__); \
650                 return; \
651         } \
652         else {;}
653
654 /**
655  * If the condition is @c false, the message is printed with a tag.
656  *
657  * @since 2.0
658  *
659  * @param[in]   tag                     Used to identify the source of a log message
660  * @param[in]   condition               The condition that is expected to be true
661  * @param[in]   ...                     The message to display
662  * @hideinitializer
663  */
664 #define TryLogTag(tag, condition, ...)  \
665         if (!(condition)) { \
666                 AppLogTag(tag, __VA_ARGS__); \
667         } \
668         else {;}
669
670 /** @} */
671
672 _OSP_EXPORT_ void AppLogInternal(const char* pFunction, int lineNumber, const char* pFormat, ...);
673 _OSP_EXPORT_ void AppLogDebugInternal(const char* pFunction, int lineNumber, const char* pFormat, ...);
674 _OSP_EXPORT_ void AppLogExceptionInternal(const char* pFunction, int lineNumber, const char* pFormat, ...);
675 _OSP_EXPORT_ void AppassertInternal(const char* pFunction, int lineNumber);
676 _OSP_EXPORT_ void AppassertfInternal(const char* expr, const char* pFunction, int lineNumber, const char* pFormat, ...);
677
678 _OSP_EXPORT_ void AppLogTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...);
679 _OSP_EXPORT_ void AppLogDebugTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...);
680 _OSP_EXPORT_ void AppLogExceptionTagInternal(const char* pTag, const char* pFunction, int lineNumber, const char* pFormat, ...);
681
682
683 #ifdef __cplusplus
684 }
685 #endif
686
687 #endif // _FBASE_LOG_H_