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