Closes app now when you hit the back button. This is a temp fix to keep media workin...
[profile/ivi/ug-camera-efl.git] / src / cam_debug.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://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 #include "cam_debug.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 /* #include <debug-message.h> */
23
24 #define LOG_LEVEL_FLAGS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_DEBUG | \
25                                                 G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | \
26                                                 G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)
27
28 #define LOG_FILTER_CRITICAL             G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR
29 #define LOG_FILTER_WARNING              LOG_FILTER_CRITICAL | G_LOG_LEVEL_WARNING
30 #define LOG_FILTER_MESSAGE              LOG_FILTER_WARNING | G_LOG_LEVEL_MESSAGE
31 #define LOG_FILTER_INFO                 LOG_FILTER_MESSAGE | G_LOG_LEVEL_INFO
32 #define LOG_FILTER_DEBUG                LOG_FILTER_INFO | G_LOG_LEVEL_DEBUG
33 #define LOG_FILTER_ALL                  LOG_FILTER_DEBUG
34
35 #define BG_BLACK        40
36 #define FG_RED          31
37 #define FG_GREEN        32
38 #define FG_YELLOW       33
39 #define FG_PUPPLE       35
40 #define FG_CYAN         36
41 #define FG_BLUE         34
42
43 #define __cam_print_debug(domain, msg) \
44 do { \
45         printf("\x1b[%dm\x1b[%dm[DEBUG.%s] %s", BG_BLACK, FG_GREEN, domain, msg); \
46         printf("\x1b[0m\n"); \
47 } while (0)
48
49 #define __cam_print_info(domain, msg) \
50 do { \
51         printf("\x1b[%dm\x1b[%dm[INFO.%s] %s", BG_BLACK, FG_PUPPLE, domain, msg); \
52         printf("\x1b[0m\n"); \
53 } while (0)
54
55 #define __cam_print_msg(domain, msg) \
56 do { \
57         printf("\x1b[%dm\x1b[%dm[MESSAGE.%s] %s", BG_BLACK, FG_CYAN, domain, msg); \
58         printf("\x1b[0m\n"); \
59 } while (0)
60
61 #define __cam_print_warning(domain, msg) \
62 do { \
63         printf("\x1b[%dm\x1b[%dm[WARNING.%s] %s", BG_BLACK, FG_YELLOW, domain, msg); \
64         printf("\x1b[0m\n"); \
65 } while (0)
66
67 #define __cam_print_critical(domain, msg) \
68 do { \
69         printf("\x1b[%dm\x1b[%dm[CRITICAL.%s] %s", BG_BLACK, FG_RED, domain, msg); \
70         printf("\x1b[0m\n"); \
71 } while (0)
72
73 #define __cam_print_error(domain, msg) \
74 do { \
75         printf("\x1b[%dm\x1b[%dm**ERROR.%s: %s", BG_BLACK, FG_RED, domain, msg); \
76         printf("\x1b[0m\n"); \
77 } while (0)
78
79 #ifdef ENABLE_TIME_MEASURE
80
81 static GTimer *g_timer = NULL;
82 static gdouble previous_elapsed = 0;
83
84 static GTimer *g_measure_timer = NULL;
85 static gdouble previous_time[MEASURE_TYPE_MAX] = { 0 };
86
87 static char debug_type[MEASURE_TYPE_MAX][200] = {
88         "NORMAL_MEASURE_TYPE",
89         "APP_LAUNCHING_TYPE",
90         "CAPTURE_MEASURE_TIME",
91         "REGISTER_FILE_MEASURE_TIME",
92         "REC_START_MEASURE_TIME",
93         "REC_COMMIT_MEASURE_TIME",
94         "REVIEW_MEASURE_TIME",
95         "AUTO_FOCUSE_TIME",
96 };
97
98 #ifdef MEMCHECK
99
100 gboolean memcheck = false;
101
102 EXPORT_API void debug_start_memcheck()
103 {
104         if (!memcheck) {
105                 mwInit();
106                 memcheck = true;
107         }
108 }
109
110 EXPORT_API void debug_stop_memcheck()
111 {
112         if (memcheck) {
113                 mwFlushNow();
114                 mwTerm();
115         }
116 }
117 #endif
118
119 void debug_measure_init()
120 {
121         g_measure_timer = g_timer_new();
122
123         int type = 0;
124
125         for (type = 0; type < MEASURE_TYPE_MAX; type++) {
126                 previous_time[type] = 0;
127         }
128
129         return;
130 }
131
132 void debug_measure_close()
133 {
134         if (g_measure_timer) {
135                 g_timer_destroy(g_measure_timer);
136                 g_measure_timer = NULL;
137         }
138
139         int type = 0;
140
141         for (type = 0; type < MEASURE_TYPE_MAX; type++) {
142                 previous_time[type] = 0;
143         }
144
145         return;
146 }
147
148 void debug_measure_start(cam_time_meature_type meature_type,
149                          const gchar *format, ...)
150 {
151         char buf[512] = { '\0', };
152         va_list ap;
153         va_start(ap, format);
154         vsnprintf(buf, sizeof(buf), format, ap);
155         va_end(ap);
156
157         bool request_reset = true;
158         int type = 0;
159 /*
160         if(meature_type ==APP_LAUNCHING_TYPE)
161         {
162                 if(g_measure_timer == NULL)
163                 {
164                         debug_measure_init();
165                         previous_time[meature_type] = g_timer_elapsed(g_measure_timer, NULL);
166                 }
167         }
168 */
169         if (g_measure_timer == NULL)
170                 debug_measure_init();
171
172         for (type = 0; type < MEASURE_TYPE_MAX; type++) {
173                 if (previous_time[type] != 0)
174                         request_reset = false;
175         }
176
177         if (request_reset)
178                 g_timer_reset(g_measure_timer);
179
180         previous_time[meature_type] = g_timer_elapsed(g_measure_timer, NULL);
181
182         /* cam_debug_time(LOG_TIME, "START %s : %s",debug_type[meature_type] , buf); */
183 }
184
185 void debug_measure_stop(cam_time_meature_type meature_type,
186                         const gchar *format, ...)
187 {
188         char buf[512] = { '\0', };
189         va_list ap;
190         va_start(ap, format);
191         vsnprintf(buf, sizeof(buf), format, ap);
192         va_end(ap);
193
194         gdouble current_elapsed = g_timer_elapsed(g_measure_timer, NULL);
195         gdouble meature_time = 0.0;
196
197         meature_time = (current_elapsed - previous_time[meature_type]);
198
199         cam_debug_time(LOG_TIME, "%s measure time = %.02f (sec) function %s\n",
200                        debug_type[meature_type], (gfloat) (meature_time), buf);
201
202         previous_time[meature_type] = 0;
203 }
204
205 void debug_time_measure_log(const gchar *format, ...)
206 {
207 /*
208         g_return_if_fail(g_timer);
209         gdouble elapsed = g_timer_elapsed(g_timer, NULL);
210
211         char buf[512] = {'\0',};
212         va_list ap;
213         va_start(ap, format);
214         vsnprintf(buf, sizeof(buf), format, ap);
215         va_end(ap);
216
217         // cam_debug_time(LOG_TIME, "%s, measure_time = %.02f (ms)", buf, (gfloat)(elapsed - previous_elapsed));
218
219         previous_elapsed = elapsed;
220 */
221
222 }
223
224 void debug_time_measure_start(const gchar *format, ...)
225 {
226         char buf[512] = { '\0', };
227         va_list ap;
228         va_start(ap, format);
229         vsnprintf(buf, sizeof(buf), format, ap);
230         va_end(ap);
231
232         if (g_timer == NULL) {
233                 g_timer = g_timer_new();
234         } else {
235                 g_timer_start(g_timer);
236         }
237
238         previous_elapsed = g_timer_elapsed(g_timer, NULL);
239
240         /*
241            cam_debug_time(LOG_TIME, "\nNEW MEASUREMENT: %s", buf);
242            cam_debug_time(LOG_TIME, "UNIT NAME, TOTAL ELAPSED, ELAPSED");
243          */
244 }
245
246 void debug_time_measure_stop(void)
247 {
248         if (g_timer) {
249                 g_timer_destroy(g_timer);
250                 g_timer = NULL;
251         }
252 }
253 #endif                          /*ENABLE_TIME_MEASURE */
254
255 #ifdef DEBUG_MESSAGE_ON
256 #ifndef USE_DLOG_MESSAGE
257 void
258 cam_log_func(const gchar *_domain, GLogLevelFlags log_level,
259              const char *file_name, const char *func, int line_number,
260              const char *format, ...)
261 {
262
263         va_list args;
264         char msg_buf[1024] = "\0", message[2048] = "\0";
265         char *p = NULL;
266         GET_FILE_NAME(p, (char *)file_name);
267
268         va_start(args, format);
269         vsnprintf(msg_buf, sizeof(msg_buf), format, args);
270
271         snprintf(message, sizeof(message), "[%s:%s:#%d]%s", p, func,
272                  line_number, msg_buf);
273         va_end(args);
274
275         if (log_level & G_LOG_LEVEL_DEBUG) {
276                 __cam_print_debug(_domain, message);
277         } else if (log_level & G_LOG_LEVEL_INFO) {
278                 __cam_print_info(_domain, message);
279         } else if (log_level & G_LOG_LEVEL_MESSAGE) {
280                 __cam_print_msg(_domain, message);
281         } else if (log_level & G_LOG_LEVEL_WARNING) {
282                 __cam_print_warning(_domain, message);
283         } else if (log_level & G_LOG_LEVEL_CRITICAL) {
284                 __cam_print_critical(_domain, message);
285         } else if (log_level & G_LOG_LEVEL_ERROR) {
286                 __cam_print_error(_domain, message);
287         }
288
289 }
290 #endif
291 #endif