Flora license update
[apps/core/preloaded/ug-camera-efl.git] / include / cam_debug.h
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #ifndef _CAM_DEBUG_H_
19 #define _CAM_DEBUG_H_
20
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <stdlib.h>
24
25 G_BEGIN_DECLS
26 /*
27 #define LOG_TO_FILE
28 #define  ENABLE_TIME_MEASURE
29 #define TRACE_OBJECT
30 #define ENABLE_TIME_STAMP
31 */
32 #define DEBUG_MESSAGE_ON
33 #define USE_DLOG_MESSAGE        /* for dlog */
34 #define  TRACE_FENTER_LEAVE
35 #define ENABLE_CHECK_MEMORY_OCCUPIED_SIZE
36 /* Domain */
37 #define LOG_UI  "UI"            /* about UI, EFL, */
38 #define LOG_MM  "MM"            /* about MSL, */
39 #define LOG_FILE "FILE"         /* about file */
40 #define LOG_SND "SOUND"
41 #define LOG_SYS "SYSTEM"
42 #define LOG_FWK "FRAMEWROK"
43 #define LOG_CONFIG "CONFIG"
44 #define LOG_CAM "CAMERA"        /* #define LOG_CAMERA    0x00000001 */
45 #ifdef DEBUG_MESSAGE_ON
46 #define GET_FILE_NAME(p, f)     \
47                 do { \
48                         p = strrchr(f, '/');    \
49                         if (p)\
50                                 p++;\
51                         else \
52                                 p = f;\
53                 } while (0)
54 /*
55 #ifdef ENABLE_CHECK_MEMORY_STATUS
56 #define CHECK_MEMORY_STATS(s) fprintf(stderr, "[%s]: %s\n", s , __FUNCTION__); \
57                                                               malloc_stats(); \
58                                                               fprintf(stderr, "------------------\n")
59 #else
60 #define CHECK_MEMORY_STATS(s)                   G_STMT_START{ (void)0; }G_STMT_END
61 #endif
62 */
63 #ifdef ENABLE_CHECK_MEMORY_OCCUPIED_SIZE
64 #include <malloc.h>
65 struct mallinfo g_mem_info;
66 #endif
67
68 #ifdef USE_DLOG_MESSAGE
69
70 #include <dlog.h>
71 #ifdef LOG_TAG
72 #undef LOG_TAG
73 #endif
74
75 #define LOG_TAG "cam-app"
76
77 #define DEBUG_TRACE(fmt, arg...) do { if (TRUE) { \
78                         LOGD("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg);     \
79                 } } while (0)
80
81 #define debug_msg(domain, fmt, arg...)           LOGD("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
82 #define cam_debug(domain, fmt, arg...)          LOGD("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
83 #define cam_info(domain, fmt, arg...)            LOGI("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
84 #define cam_message(domain, fmt, arg...)        LOGI("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
85 #define cam_warning(domain, fmt, arg...)        LOGW("[%s : %s : %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
86 #define cam_critical(domain, fmt, arg...)        LOGE("[%s : %s: %d] "fmt"\n", __FILE__, __FUNCTION__, __LINE__, ##arg)
87
88 #ifdef ENABLE_CHECK_MEMORY_OCCUPIED_SIZE
89 #define CHECK_MEMORY_STATS(s) g_mem_info = mallinfo(); \
90                                                                 LOGD("\n\n[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.hblkhd + (g_mem_info).uordblks)
91 #endif
92 /*
93                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "\n\n[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.arena); \
94                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.ordblks); \
95                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.hblks); \
96                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.hblkhd); \
97                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.uordblks); \
98                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.fordblks); \
99                                                                 dlog(MID_CAMERA, DLOG_VERBOSE, "[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.keepcost); \
100 */
101
102 #else                           /*  #ifdef USE_DLOG_MESSAGE */
103
104 void
105 cam_log_func(const gchar *_domain, GLogLevelFlags log_level,
106              const char *file_name, const char *func, int line_number,
107              const char *format, ...);
108
109 #define debug_msg(category, fmt, arg...)                cam_log_func(category, G_LOG_LEVEL_DEBUG, __FILE__, __func__, __LINE__, fmt, ##arg)
110 #define cam_debug(category, fmt, arg...)                cam_log_func(category, G_LOG_LEVEL_DEBUG, __FILE__, __func__, __LINE__, fmt,  ##arg)
111 #define cam_info(category, fmt, arg...)         cam_log_func(category, G_LOG_LEVEL_INFO, __FILE__, __func__, __LINE__, fmt, ##arg)
112 #define cam_message(category, fmt, arg...)        cam_log_func(category, G_LOG_LEVEL_MESSAGE, __FILE__,  __func__, __LINE__, fmt, ##arg)
113 #define cam_warning(category, fmt, arg...)      cam_log_func(category, G_LOG_LEVEL_WARNING, __FILE__,  __func__, __LINE__, fmt, ##arg)
114 #define cam_critical(category, fmt, arg...)     cam_log_func(category, G_LOG_LEVEL_CRITICAL, __FILE__, __func__, __LINE__, fmt, ##arg)
115
116 #ifdef ENABLE_CHECK_MEMORY_OCCUPIED_SIZE
117 #define CHECK_MEMORY_STATS(s) g_mem_info = mallinfo(); \
118                                                                 cam_debug(LOG_SYS, "\n\n[[[[[[[[[[ %s : mem size is %d ]]]]]]]]]]\n", s, g_mem_info.hblkhd + (g_mem_info).uordblks)
119 #endif
120
121 #endif                          /* DEBUG MESSAGE ON */
122
123 #else
124 #define debug_msg(domain, fmt, arg...)          G_STMT_START{ (void)0; } G_STMT_END
125 #define cam_debug(domain, fmt, arg...)          G_STMT_START{ (void)0; } G_STMT_END
126 #define cam_info(domain, fmt, arg...)           G_STMT_START{ (void)0; } G_STMT_END
127 #define cam_message(domain, fmt, arg...) G_STMT_START{ (void)0; } G_STMT_END
128 #define cam_warning(domain, fmt, arg...)        G_STMT_START{ (void)0; } G_STMT_END
129 #define cam_critical(fmt, arg...)                       G_STMT_START{ (void)0; } G_STMT_END
130 #ifdef ENABLE_CHECK_MEMORY_OCCUPIED_SIZE
131 #define CHECK_MEMORY_STATS(s)                   G_STMT_START{ (void)0; } G_STMT_END
132 #endif
133 #endif                          /* #ifdef DEBUG_MESSAGE_ON */
134 #define warn_msg cam_warning
135 #define critical_msg cam_critical
136 /* Trace function enter/leave
137  */
138 #ifdef TRACE_FENTER_LEAVE
139 #define debug_fenter(domain)                            cam_debug(domain, " started");
140 #define debug_fleave(domain)                            cam_debug(domain, " leaved");
141 #else
142 #define debug_fenter(domain)                            G_STMT_START{ (void)0; } G_STMT_END
143 #define debug_fleave(domain)                            G_STMT_START{ (void)0; } G_STMT_END
144 #endif
145 #define cam_ret_if(expr) do { \
146         if (expr) { \
147                 return; \
148         } \
149 } while (0)
150 #define cam_retv_if(expr, val) do { \
151         if (expr) { \
152                 return (val); \
153         } \
154 } while (0)
155 #define cam_retm_if(expr, fmt, arg...) do { \
156         if (expr) { \
157                 cam_warning(LOG_UI, fmt, ##arg); \
158                 return; \
159         } \
160 } while (0)
161 #define cam_retvm_if(expr, val, fmt, arg...) do { \
162         if (expr) { \
163                 cam_warning(LOG_UI, fmt, ##arg); \
164                 return (val); \
165         } \
166 } while (0)
167 /* Trace module elapsed time
168  */
169 #ifdef ENABLE_TIME_MEASURE
170     typedef enum {
171         NORMAL_MEASURE_TYPE = 0,
172         APP_LAUNCHING_TYPE,
173         CAPTURE_MEASURE_TYPE,
174         REGISTER_MEASURE_TYPE,
175         REC_START_MEASURE_TYPE,
176         REC_COMMIT_MEASURE_TYPE,
177         REVIEW_MEASURE_TYPE,
178         AUTO_FOCUSE_TIME,
179         MEASURE_TYPE_MAX,
180 } cam_time_meature_type;
181
182 void debug_measure_start(cam_time_meature_type meature_type,
183                          const gchar *format, ...);
184 void debug_measure_stop(cam_time_meature_type meature_type,
185                         const gchar *format, ...);
186 void debug_measure_init();
187 void debug_measure_close();
188
189 void debug_time_measure_start(const gchar *format, ...);
190 void debug_time_measure_stop(void);
191 void debug_time_measure_log(const gchar *format, ...);
192
193 #define debug_time_measure_fenter(fmt, arg...)  debug_time_measure_log("%s(). LINE :%d ", __FUNCTION__, __LINE__, ##arg)
194 #define debug_time_measure_fleave(fmt, arg...)  debug_time_measure_log("%s(). LINE :%d", __FUNCTION__, __LINE__, ##arg)
195
196 #else
197 #define debug_time_measure_start(fmt, arg...)   G_STMT_START{ (void)0; } G_STMT_END
198 #define debug_time_measure_stop()                               G_STMT_START{ (void)0; } G_STMT_END
199 #define debug_time_measure_log(fmt, arg...)             G_STMT_START{ (void)0; } G_STMT_END
200 #define debug_time_measure_fenter(fmt, arg...)  G_STMT_START{ (void)0; } G_STMT_END
201 #define debug_time_measure_fleave(fmt, arg...)  G_STMT_START{ (void)0; } G_STMT_END
202 #define debug_measure_start(fmt, arg...)        G_STMT_START{ (void)0; } G_STMT_END
203 #define debug_measure_stop(fmt, arg...) G_STMT_START{ (void)0; } G_STMT_END
204 #define debug_measure_init()            G_STMT_START{ (void)0; } G_STMT_END
205 #define debug_measure_close()   G_STMT_START{ (void)0; } G_STMT_END
206 #endif                          /*ENABLE_TIME_MEASURE */
207 #ifdef ENABLE_TIME_STAMP
208 #include <sys/time.h>
209 #include <linux/unistd.h>
210     enum {
211         CAM_MSG_COLOR_DEFAULT = 0,
212         CAM_MSG_COLOR_BLACK = 30,
213         CAM_MSG_COLOR_RED = 31,
214         CAM_MSG_COLOR_GREEN = 32,
215         CAM_MSG_COLOR_YELLOW = 33,
216         CAM_MSG_COLOR_BLUE = 34,
217         CAM_MSG_COLOR_MAGENTA = 35,
218         CAM_MSG_COLOR_CYAN = 36,
219         CAM_MSG_COLOR_WHITE = 37,
220 };
221
222 static long s_time_sec = 0L;
223 static long s_time_usec = 0L;
224 static struct timeval s_cur_time;
225
226 static pid_t gettid(void)
227 {
228         return syscall(__NR_gettid);
229 }
230
231 #define debug_time_reset(fmt, arg...)   \
232         do {    \
233                 struct timeval time;    \
234                 gettimeofday(&time, NULL);      \
235                 s_cur_time = time; \
236                 s_time_sec = time.tv_sec;       \
237                 s_time_usec = time.tv_usec;     \
238                 debug_color(CAM_MSG_COLOR_RED, "[TIMER],<%03ld.%03ld>, \t000.000 sec (reset timer)\t000.000\t[%-20.20s:%4d]" fmt "\n", s_time_sec, s_time_usec, __FUNCTION__, __LINE__, ##arg); \
239         } while (0)
240
241 #define debug_time_stamp(fmt, arg...)   \
242         do {    \
243                 struct timeval time;    \
244                 long sec, usec; \
245                 long a_sec, a_usec; \
246                 gettimeofday(&time, NULL);      \
247                 a_sec = time.tv_sec - s_cur_time.tv_sec; \
248                 if (s_cur_time.tv_usec > time.tv_usec) {        \
249                         a_usec = 1000000L - s_cur_time.tv_usec + time.tv_usec;  \
250                         a_sec--;        \
251                 } else {        \
252                         a_usec = time.tv_usec - s_cur_time.tv_usec;     \
253                 }       \
254                 s_cur_time = time; \
255                 sec = time.tv_sec - s_time_sec; \
256                 if (s_time_usec > time.tv_usec) {       \
257                         usec = 1000000L - s_time_usec + time.tv_usec;   \
258                         sec--;  \
259                 } else {        \
260                         usec = time.tv_usec - s_time_usec;      \
261                 }       \
262                 debug_color(CAM_MSG_COLOR_RED, "[TIMER],<%03ld.%03ld>, %03ld.%03ld sec (time stamp) %03ld.%03ld\t[%-20.20s:%4d]" fmt "\n", time.tv_sec, time.tv_usec, sec, usec / 1000, a_sec, a_usec/1000, __FUNCTION__, __LINE__, ##arg); \
263         } while (0)
264
265 #define debug_time_stamp_simple(fmt, arg...)    \
266         do {    \
267                 struct timeval time;    \
268                 gettimeofday(&time, NULL);      \
269                 debug_color(CAM_MSG_COLOR_RED, "[TIMER],<%03ld.%03ld> (reset timer), [%-20.20s:%4d], TID=%d" fmt "\n", time.tv_sec, time.tv_usec, __FUNCTION__, __LINE__, gettid(), ##arg);     \
270         } while (0)
271
272 #define debug_color(color, fmt, arg...) \
273         do {    \
274                 fprintf(stderr, "\033[%dm", color);     \
275                 fprintf(stderr, fmt, ##arg);    \
276                 fprintf(stderr, "\033[%dm", CAM_MSG_COLOR_DEFAULT);     \
277                 \
278         } while (0)
279 #else
280 /*
281  *      NO PROFILING MESSAGE
282  */
283 #define debug_time_reset(fmt, arg...) G_STMT_START{ (void)0; } G_STMT_END
284 #define debug_time_stamp(fmt, arg...) G_STMT_START{ (void)0; } G_STMT_END
285 #define debug_time_stamp_simple(fmt, arg...) G_STMT_START{ (void)0; } G_STMT_END
286 #endif
287 /* Trace GObject deletion
288  */
289 #ifdef TRACE_OBJECT
290 void cam_debug_trace_object(gpointer obj, const gchar *format, ...);
291 #else
292 #define cam_debug_trace_object(obj, fmt, arg...)        G_STMT_START{ (void)0; } G_STMT_END
293 #endif
294 #define cam_debug_trace_alloc cam_debug_trace_object
295     G_END_DECLS
296 #endif /*_CAM_DEBUG_H_*/