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