xref: /aoo4110/main/sal/inc/rtl/logfile.hxx (revision b1cdbd2c)
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 #ifndef _RTL_LOGFILE_HXX_
24 #define _RTL_LOGFILE_HXX_
25 
26 #include <rtl/logfile.h>
27 #include <rtl/string.hxx>
28 
29 namespace rtl
30 {
31 /**
32 @descr	The intended use for class Logfile is to write time stamp information
33 		for profiling purposes.
34 
35 		Profiling output should only be generated for a special product version of OpenOffice
36 		which is compiled with a defined preprocessor symbol 'TIMELOG'.
37 		Therefore we have provided a set of macros that uses the class Logfile only if
38 		this symbol is defined.  If the macros are not sufficient, i.e. you need more
39 		then three arguments for a printf style message, then you have to insert an
40 		#ifdef TIMELOG/#endif brace yourself.
41 
42 		Additionally the environment variable RTL_LOGFILE has to be defined in order to generate
43 		logging information. If the variable is not empty, it creates a file with the name
44 		$(RTL_LOGFILE)_$(PID).log, where $(PID) is the process id of the running process.
45 		It can be used as a run time switch for enabling or disabling the logging.
46 		Note that this variable is evaluated only once at the first attempt to write a message.
47 
48 		The class LogFile collects runtime data within its constructor and destructor. It can be
49 		used for timing whole functions.
50 		If you want to write timing data without context you can use the RTL_LOGFILE_TRACE-macros
51 		which are defined inside <rtl/logfile.h>.
52 
53 		The class LogFile should not be used directly, instead use the RTL_LOGFILE_CONTEXT/
54 		RTL_LOGFILE_TRACE-macros.
55 
56 		Macro usage:
57 		------------
58 		RTL_LOGFILE_CONTEXT( instance, name );
59 		This macro creates an instance of class LogFile with the name "instance" and writes the current time,
60 		thread id and "name" to the log file.
61 
62 		Example: RTL_LOGFILE_CONTEXT( aLog, "Timing for foo-method" );
63 
64 		RTL_LOGFILE_CONTEXT_TRACE( instance, mesage );
65 		RTL_LOGFILE_CONTEXT_TRACEn( instance, frmt, arg1, .., arg3 );
66 		These macros can be used to log information in a "instance" context. The "instance" object
67 		is used to log message informations. All macros with "frmt" uses printf notation to log timing infos.
68 
69 		Example: RTL_LOGFILE_CONTEXT_TRACE( aLog, "Now we call an expensive function" );
70 				 RTL_LOGFIlE_CONTEXT_TRACE1( aLog, "Config entries read: %u", (unsigned short)i );
71 
72 		RTL_LOGFILE_TRACE( string );
73 		RTL_LOGFILE_TRACEn( frmt, arg1, .., arg3 );
74 		These macros can be used to log information outside a context. The macro directly calls
75 		rtl_logfile_trace to write the info to the log file. All macros with "frmt" uses printf
76 		notation to log timing infos.
77 
78 		Example: RTL_LOGFILE_TRACE( "Timing for loading a file" );
79 				 RTL_LOGFILE_TRACE1( aLog, "Timing for loading file: %s", aFileName );
80 
81 		The lines written to the log file consist of the following space separated elements:
82 		1.	The time relative to the start of the global timer in milliseconds.  The times is
83 			started typically for the first logged line.
84 		2.	Thread id.  It's absolut value is probably of less interest than providing a way to
85 			distinguish different threads.
86 		3.	a.	An opening or closing curly brace indicating the start or end of a scope.
87 				4a. Function name or general scope identifier.
88 			b.	A vertical line indicating an arbitrary message.
89 				4b optional function name or general scope identifier.
90 				5b A colon followed by a space and a free form message terminated by a newline.
91 
92 		There is a second version of creating a context. RTL_LOGFILE_CONTEXT_AUTHOR takes
93 		two more arguments, the name of the project and the author's sign who is responsible
94 		for the code in which the macro is used.
95 */
96 	class Logfile
97 	{
98 	public:
99 		inline Logfile( const sal_Char *name );
100 		/**	@descr	Create a log file context where the message field consists of a project
101 				name, the author's shortcut, and the actual message.  These three strings
102 				are written in a format that is understood by script that later parses the
103 				log file and that so can extract the three strings.
104 			@param	project	Short name of the project, like sw for writer or sc for calc.
105 			@param	author	The sign of the person responsible for the code.
106 			@param	name	The actual message, typically a method name.
107 		*/
108 		inline Logfile( const sal_Char *project, const sal_Char *author, const sal_Char *name );
109 		inline ~Logfile();
110 		inline const sal_Char *getName();
111 	private:
112 		::rtl::OString m_sName;
113 	};
114 
Logfile(const sal_Char * name)115 	inline Logfile::Logfile( const sal_Char *name )
116 		: m_sName( name )
117 	{
118 		rtl_logfile_longTrace( "{ %s\n", name );
119 	}
120 
Logfile(const sal_Char * project,const sal_Char * author,const sal_Char * name)121 	inline Logfile::Logfile( const sal_Char *project, const sal_Char *author, const sal_Char *name )
122 		: m_sName( project)
123 	{
124 		m_sName += " (";
125 		m_sName += author;
126 		m_sName += ") ";
127 		m_sName += name;
128 		rtl_logfile_longTrace( "{ %s\n", m_sName.pData->buffer );
129 	}
130 
~Logfile()131 	inline Logfile::~Logfile()
132 	{
133 		rtl_logfile_longTrace( "} %s\n", m_sName.pData->buffer );
134 	}
135 
getName()136 	inline const sal_Char * Logfile::getName()
137 	{
138 		return m_sName.getStr();
139 	}
140 }
141 
142 #ifdef TIMELOG
143 #define RTL_LOGFILE_CONTEXT( instance, name ) ::rtl::Logfile instance( name )
144 #define RTL_LOGFILE_CONTEXT_AUTHOR( instance, project, author, name ) ::rtl::Logfile instance(project, author, name )
145 #define RTL_LOGFILE_CONTEXT_TRACE( instance, message ) \
146 		rtl_logfile_longTrace( "| %s : %s\n", \
147 						   instance.getName(), \
148 						   message )
149 #define RTL_LOGFILE_CONTEXT_TRACE1( instance , frmt, arg1 ) \
150 		rtl_logfile_longTrace( "| %s : ", \
151 						   instance.getName() ); \
152         rtl_logfile_trace( frmt , arg1 ); \
153         rtl_logfile_trace( "\n" )
154 #define RTL_LOGFILE_CONTEXT_TRACE2( instance , frmt, arg1 , arg2 ) \
155 		rtl_logfile_longTrace( "| %s : ", \
156 						   instance.getName() ); \
157         rtl_logfile_trace( frmt , arg1 , arg2 ); \
158         rtl_logfile_trace( "\n" )
159 #define RTL_LOGFILE_CONTEXT_TRACE3( instance , frmt, arg1 , arg2 , arg3 ) \
160 		rtl_logfile_longTrace( "| %s : ", \
161 						   instance.getName() ); \
162         rtl_logfile_trace( frmt , arg1 , arg2 , arg3 ); \
163         rtl_logfile_trace( "\n" )
164 
165 #else
166 #define RTL_LOGFILE_CONTEXT( instance, name )  ((void)0)
167 #define RTL_LOGFILE_CONTEXT_AUTHOR( instance, project, author, name )  ((void)0)
168 #define RTL_LOGFILE_CONTEXT_TRACE( instance, message )  ((void)0)
169 #define RTL_LOGFILE_CONTEXT_TRACE1( instance, frmt, arg1 ) ((void)0)
170 #define RTL_LOGFILE_CONTEXT_TRACE2( instance, frmt, arg1, arg2 ) ((void)0)
171 #define RTL_LOGFILE_CONTEXT_TRACE3( instance, frmt, arg1, arg2 , arg3 ) ((void)0)
172 #endif
173 
174 // Normal RTL_LOGFILE_* entries will not make it into release versions,
175 // TIMELOG is disabled a few versions prior relase build.
176 //
177 // We need some logs also in these builds, eg. for making performance regression tests.
178 //
179 // POLICY: Don't use RTL_LOGFILE_PRODUCT_* for your personal logging information.
180 //         Be aware that these logs make it into the product shipped to customers.
181 //         If you have good reasons for doing this, please contact product management.
182 
183 #define RTL_LOGFILE_PRODUCT_TRACE( string )  \
184             rtl_logfile_longTrace( "| : %s\n", string )
185 #define RTL_LOGFILE_PRODUCT_TRACE1( frmt, arg1 ) \
186              rtl_logfile_longTrace( "| : " ); \
187              rtl_logfile_trace( frmt, arg1 ); \
188              rtl_logfile_trace( "\n" )
189 #define RTL_LOGFILE_PRODUCT_CONTEXT( instance, name ) \
190             ::rtl::Logfile instance( name )
191 #define RTL_LOGFILE_PRODUCT_CONTEXT_TRACE1( instance, frmt, arg1 ) \
192 		rtl_logfile_longTrace( "| %s : ", \
193 						   instance.getName() ); \
194              rtl_logfile_trace( frmt, arg1 ); \
195              rtl_logfile_trace( "\n" )
196 #define RTL_LOGFILE_HASLOGFILE() \
197 			 rtl_logfile_hasLogFile()
198 
199 
200 #endif
201 
202