1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
25 #define __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
26 
27 //*************************************************************************************************************
28 //  special macros for time measures
29 //  1) LOGFILE_TIMEMEASURE                  used it to define log file for this operations (default will be set automatically)
30 //  2) START_TIMEMEASURE                    start new measure by using given variable names
31 //  3) START_TIMEMEASURE                    stop current measure by using given variable names and return time
32 //  4) LOG_TIMEMEASURE                      write measured time to logfile
33 //*************************************************************************************************************
34 
35 #ifdef  ENABLE_TIMEMEASURE
36 
37 	//_________________________________________________________________________________________________________________
38 	//	includes
39 	//_________________________________________________________________________________________________________________
40 
41 	#ifndef _RTL_STRBUF_HXX_
42 	#include <rtl/strbuf.hxx>
43 	#endif
44 
45     #ifndef _OSL_TIME_H_
46     #include <osl/time.h>
47     #endif
48 
49 	/*_____________________________________________________________________________________________________________
50         LOGFILE_TIMEMEASURE
51 
52 		For follow macros we need a special log file. If user forget to specify anyone, we must do it for him!
53 	_____________________________________________________________________________________________________________*/
54 
55     #ifndef LOGFILE_TIMEMEASURE
56         #define LOGFILE_TIMEMEASURE "timemeasure.log"
57 	#endif
58 
59     /*_____________________________________________________________________________________________________________
60         class TimeMeasure
61 
62         We need this inline class as global timer to make it possible measure times over different objects!
63         zB. Use it as baseclass to start timer at ctor (must be first called baseclass!!!) and stop it by calling stop method.
64 	_____________________________________________________________________________________________________________*/
65 
66     class DBGTimeMeasureBase
67     {
68         public:
DBGTimeMeasureBase()69             inline DBGTimeMeasureBase()
70             {
71                 m_nEnd   = 0                   ;
72                 m_nStart = osl_getGlobalTimer();
73             }
74 
stopAndGet()75             inline sal_Int32 stopAndGet()
76             {
77                 m_nEnd = osl_getGlobalTimer();
78                 return( m_nEnd-m_nStart );
79             }
80 
81         private:
82             sal_Int32 m_nStart ;
83             sal_Int32 m_nEnd   ;
84     };
85 
86 	/*_____________________________________________________________________________________________________________
87         START_TIMEMEASURE( NSTART, NEND )
88         STOP_TIMEMEASURE( NSTART, NEND, NTIME )
89 
90         If you doesn't need a time measure above different classes ... you can try this macros!
91         They initialize your given members with start end end time ... You can calculate differenz by himself.
92     _____________________________________________________________________________________________________________*/
93 
94     #define START_TIMEMEASURE( NSTART, NEND )                                                                   \
95                 sal_Int32   NSTART  = 0;                                                                        \
96                 sal_Int32   NEND    = 0;                                                                        \
97                 NSTART = osl_getGlobalTimer();
98 
99     #define STOP_TIMEMEASURE( NSTART, NEND, NTIME )                                                             \
100                           NEND  = osl_getGlobalTimer();                                                         \
101                 sal_Int32 NTIME = NEND-NSTART;
102 
103 	/*_____________________________________________________________________________________________________________
104         LOG_TIMEMEASURE( SOPERATION, NSTART )
105 
106         Write measured time to logfile.
107     _____________________________________________________________________________________________________________*/
108 
109     #define LOG_TIMEMEASURE( SOPERATION, NTIME )                                                                \
110                 {                                                                                               \
111                     ::rtl::OStringBuffer _sBuffer( 256 );                                                       \
112                     _sBuffer.append( SOPERATION         );                                                      \
113                     _sBuffer.append( "\t=\t"            );                                                      \
114                     _sBuffer.append( (sal_Int32)(NTIME) );                                                      \
115                     _sBuffer.append( " ms\n"            );                                                      \
116                     WRITE_LOGFILE( LOGFILE_TIMEMEASURE, _sBuffer.makeStringAndClear().getStr() )                \
117                 }
118 
119 #else   // #ifdef ENABLE_TIMEMEASURE
120 
121 	/*_____________________________________________________________________________________________________________
122 		If right testmode is'nt set - implements these macros empty!
123 	_____________________________________________________________________________________________________________*/
124 
125     #undef  LOGFILE_TIMEMEASURE
126     #define START_TIMEMEASURE( NSTART, NEND )
127     #define STOP_TIMEMEASURE( NSTART, NEND, NTIME )
128     #define LOG_TIMEMEASURE( SOPERATION, NTIME )
129 
130 #endif  // #ifdef ENABLE_TIMEMEASURE
131 
132 //*****************************************************************************************************************
133 //	end of file
134 //*****************************************************************************************************************
135 
136 #endif  // #ifndef __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
137