Upload initial version
[platform/core/uifw/libscl-ui-nui.git] / scl / scldebug.cpp
1 /*
2  * Copyright (c) 2012 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "scldebug.h"
19 #include <sys/time.h>
20 #include <string.h>
21
22
23 #ifdef SCL_DEBUG_ON
24
25 /* Measure elapased time */
26 static float
27 _SCL_DEBUG_ELAPSED_TIME(const char* str, struct timeval t0, struct timeval t1);
28
29 /* Measure elapased time */
30 float SCL_DEBUG_TIME(const char* fileStr, int line, const char* str)
31 {
32     float etime = 0.0;
33     static struct timeval t0 = {
34         0,
35     };
36     static struct timeval t1;
37     char *pFileStr = const_cast<char*>(fileStr);
38     if (pFileStr) {
39         if (strlen(fileStr) > 20) {
40             pFileStr += (strlen(fileStr) - 20);
41         }
42         gettimeofday(&t1, NULL);
43         if (t0.tv_usec != 0) {
44             etime = ((t1.tv_sec * 1000000 + t1.tv_usec) - (t0.tv_sec * 1000000 + t0.tv_usec))/1000000.0;
45         }
46         if (strncmp(str, "get_", 4) == 0) {
47             //printf("[%-20.20s]%04d [T:%u][E:" mc_red "%f" mc_normal "]" mc_normal " %s" mc_normal "\n", pFileStr, line, (t1.tv_sec * 1000000 + t1.tv_usec), etime, str);
48         } else {
49             printf("[%-20.20s]%04d [T:%lu][E:" mc_red "%f" mc_normal "]" mc_blue " %s" mc_normal "\n", pFileStr, line, (t1.tv_sec * 1000000 + t1.tv_usec), etime, str);
50         }
51         t0 = t1;
52     }
53     return etime;
54 }
55
56 /* Measure elapsed time */
57 static float _SCL_DEBUG_ELAPSED_TIME(const char* str, struct timeval t0, struct timeval t1)
58 {
59     float etime;
60     etime = ((t1.tv_sec * 1000000 + t1.tv_usec) - (t0.tv_sec * 1000000 + t0.tv_usec))/1000000.0;
61
62     printf("[%s] elap-time = " mc_green "%f ms" mc_normal " (%lu~%lu) \n", str, etime*1000, (t0.tv_sec * 1000000 + t0.tv_usec), (t1.tv_sec * 1000000 + t1.tv_usec));
63     return etime;
64 }
65
66 /* Measure elapsed time */
67 float SCL_DEBUG_ELAPSED_TIME(const char* fileStr, int line, const char* str, int type)
68 {
69     static struct timeval s_tv1;
70     static struct timeval s_tv2;
71     static int s_start_line = 0;
72     static int s_end_line = 0;
73     float etime = 0.0;
74     if (type == MEASURE_START) {
75         gettimeofday(&s_tv1, NULL);
76         s_start_line = line;
77         printf("[%-20.20s]%04d [T:%lu]" mc_blue " %s" mc_normal "\n", fileStr, line, (s_tv1.tv_sec * 1000000 + s_tv1.tv_usec), str);
78     } else if (type == MEASURE_END) {
79         gettimeofday(&s_tv2, NULL);
80         s_end_line = line;
81         char printStr[100];
82         snprintf(printStr, 100, "%s(Line:%d~%d)", str, s_start_line, s_end_line);
83         etime = _SCL_DEBUG_ELAPSED_TIME(printStr, s_tv1, s_tv2);
84     }
85     return etime;
86 }
87 #endif