1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
29 #define __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
30 
31 //*************************************************************************************************************
32 //  special macros for time measures
33 //  1) LOGFILE_TIMEMEASURE                  used it to define log file for this operations (default will be set automaticly)
34 //  2) START_TIMEMEASURE                    start new measure by using given variable names
35 //  3) START_TIMEMEASURE                    stop current measure by using given variable names and return time
36 //  4) LOG_TIMEMEASURE                      write measured time to logfile
37 //*************************************************************************************************************
38 
39 #ifdef  ENABLE_TIMEMEASURE
40 
41 	//_________________________________________________________________________________________________________________
42 	//	includes
43 	//_________________________________________________________________________________________________________________
44 
45 	#ifndef _RTL_STRBUF_HXX_
46 	#include <rtl/strbuf.hxx>
47 	#endif
48 
49     #ifndef _OSL_TIME_H_
50     #include <osl/time.h>
51     #endif
52 
53 	/*_____________________________________________________________________________________________________________
54         LOGFILE_TIMEMEASURE
55 
56 		For follow macros we need a special log file. If user forget to specify anyone, we must do it for him!
57 	_____________________________________________________________________________________________________________*/
58 
59     #ifndef LOGFILE_TIMEMEASURE
60         #define LOGFILE_TIMEMEASURE "timemeasure.log"
61 	#endif
62 
63     /*_____________________________________________________________________________________________________________
64         class TimeMeasure
65 
66         We need this inline class as global timer to make it possible measure times over different objects!
67         zB. Use it as baseclass to start timer at ctor (must be first called baseclass!!!) and stop it by calling stop method.
68 	_____________________________________________________________________________________________________________*/
69 
70     class DBGTimeMeasureBase
71     {
72         public:
73             inline DBGTimeMeasureBase()
74             {
75                 m_nEnd   = 0                   ;
76                 m_nStart = osl_getGlobalTimer();
77             }
78 
79             inline sal_Int32 stopAndGet()
80             {
81                 m_nEnd = osl_getGlobalTimer();
82                 return( m_nEnd-m_nStart );
83             }
84 
85         private:
86             sal_Int32 m_nStart ;
87             sal_Int32 m_nEnd   ;
88     };
89 
90 	/*_____________________________________________________________________________________________________________
91         START_TIMEMEASURE( NSTART, NEND )
92         STOP_TIMEMEASURE( NSTART, NEND, NTIME )
93 
94         If you doesn't need a time measure above different classes ... you can try this macros!
95         They initialize your given members with start end end time ... You can calculate differenz by himself.
96     _____________________________________________________________________________________________________________*/
97 
98     #define START_TIMEMEASURE( NSTART, NEND )                                                                   \
99                 sal_Int32   NSTART  = 0;                                                                        \
100                 sal_Int32   NEND    = 0;                                                                        \
101                 NSTART = osl_getGlobalTimer();
102 
103     #define STOP_TIMEMEASURE( NSTART, NEND, NTIME )                                                             \
104                           NEND  = osl_getGlobalTimer();                                                         \
105                 sal_Int32 NTIME = NEND-NSTART;
106 
107 	/*_____________________________________________________________________________________________________________
108         LOG_TIMEMEASURE( SOPERATION, NSTART )
109 
110         Write measured time to logfile.
111     _____________________________________________________________________________________________________________*/
112 
113     #define LOG_TIMEMEASURE( SOPERATION, NTIME )                                                                \
114                 {                                                                                               \
115                     ::rtl::OStringBuffer _sBuffer( 256 );                                                       \
116                     _sBuffer.append( SOPERATION         );                                                      \
117                     _sBuffer.append( "\t=\t"            );                                                      \
118                     _sBuffer.append( (sal_Int32)(NTIME) );                                                      \
119                     _sBuffer.append( " ms\n"            );                                                      \
120                     WRITE_LOGFILE( LOGFILE_TIMEMEASURE, _sBuffer.makeStringAndClear().getStr() )                \
121                 }
122 
123 #else   // #ifdef ENABLE_TIMEMEASURE
124 
125 	/*_____________________________________________________________________________________________________________
126 		If right testmode is'nt set - implements these macros empty!
127 	_____________________________________________________________________________________________________________*/
128 
129     #undef  LOGFILE_TIMEMEASURE
130     #define START_TIMEMEASURE( NSTART, NEND )
131     #define STOP_TIMEMEASURE( NSTART, NEND, NTIME )
132     #define LOG_TIMEMEASURE( SOPERATION, NTIME )
133 
134 #endif  // #ifdef ENABLE_TIMEMEASURE
135 
136 //*****************************************************************************************************************
137 //	end of file
138 //*****************************************************************************************************************
139 
140 #endif  // #ifndef __FRAMEWORK_MACROS_DEBUG_TIMEMEASURE_HXX_
141