update dev300-m58
[ooovba.git] / sal / inc / rtl / logfile.hxx
blobdb146c0b76a9367cafe39b3e67aa411dd7cff25f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: logfile.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _RTL_LOGFILE_HXX_
31 #define _RTL_LOGFILE_HXX_
33 #include <rtl/logfile.h>
34 #include <rtl/string.hxx>
36 namespace rtl
38 /**
39 @descr The intended use for class Logfile is to write time stamp information
40 for profiling purposes.
42 Profiling output should only be generated for a special product version of OpenOffice
43 which is compiled with a defined preprocessor symbol 'TIMELOG'.
44 Therefore we have provided a set of macros that uses the class Logfile only if
45 this symbol is defined. If the macros are not sufficient, i.e. you need more
46 then three arguments for a printf style message, then you have to insert an
47 #ifdef TIMELOG/#endif brace yourself.
49 Additionally the environment variable RTL_LOGFILE has to be defined in order to generate
50 logging information. If the variable is not empty, it creates a file with the name
51 $(RTL_LOGFILE)_$(PID).log, where $(PID) is the process id of the running process.
52 It can be used as a run time switch for enabling or disabling the logging.
53 Note that this variable is evaluated only once at the first attempt to write a message.
55 The class LogFile collects runtime data within its constructor and destructor. It can be
56 used for timing whole functions.
57 If you want to write timing data without context you can use the RTL_LOGFILE_TRACE-macros
58 which are defined inside <rtl/logfile.h>.
60 The class LogFile should not be used directly, instead use the RTL_LOGFILE_CONTEXT/
61 RTL_LOGFILE_TRACE-macros.
63 Macro usage:
64 ------------
65 RTL_LOGFILE_CONTEXT( instance, name );
66 This macro creates an instance of class LogFile with the name "instance" and writes the current time,
67 thread id and "name" to the log file.
69 Example: RTL_LOGFILE_CONTEXT( aLog, "Timing for foo-method" );
71 RTL_LOGFILE_CONTEXT_TRACE( instance, mesage );
72 RTL_LOGFILE_CONTEXT_TRACEn( instance, frmt, arg1, .., arg3 );
73 These macros can be used to log information in a "instance" context. The "instance" object
74 is used to log message informations. All macros with "frmt" uses printf notation to log timing infos.
76 Example: RTL_LOGFILE_CONTEXT_TRACE( aLog, "Now we call an expensive function" );
77 RTL_LOGFIlE_CONTEXT_TRACE1( aLog, "Config entries read: %u", (unsigned short)i );
79 RTL_LOGFILE_TRACE( string );
80 RTL_LOGFILE_TRACEn( frmt, arg1, .., arg3 );
81 These macros can be used to log information outside a context. The macro directly calls
82 rtl_logfile_trace to write the info to the log file. All macros with "frmt" uses printf
83 notation to log timing infos.
85 Example: RTL_LOGFILE_TRACE( "Timing for loading a file" );
86 RTL_LOGFILE_TRACE1( aLog, "Timing for loading file: %s", aFileName );
88 The lines written to the log file consist of the following space separated elements:
89 1. The time relative to the start of the global timer in milliseconds. The times is
90 started typically for the first logged line.
91 2. Thread id. It's absolut value is probably of less interest than providing a way to
92 distinguish different threads.
93 3. a. An opening or closing curly brace indicating the start or end of a scope.
94 4a. Function name or general scope identifier.
95 b. A vertical line indicating an arbitrary message.
96 4b optional function name or general scope identifier.
97 5b A colon followed by a space and a free form message terminated by a newline.
99 There is a second version of creating a context. RTL_LOGFILE_CONTEXT_AUTHOR takes
100 two more arguments, the name of the project and the author's sign who is responsible
101 for the code in which the macro is used.
103 class Logfile
105 public:
106 inline Logfile( const sal_Char *name );
107 /** @descr Create a log file context where the message field consists of a project
108 name, the author's shortcut, and the actual message. These three strings
109 are written in a format that is understood by script that later parses the
110 log file and that so can extract the three strings.
111 @param project Short name of the project, like sw for writer or sc for calc.
112 @param author The sign of the person responsible for the code.
113 @param name The actual message, typically a method name.
115 inline Logfile( const sal_Char *project, const sal_Char *author, const sal_Char *name );
116 inline ~Logfile();
117 inline const sal_Char *getName();
118 private:
119 ::rtl::OString m_sName;
122 inline Logfile::Logfile( const sal_Char *name )
123 : m_sName( name )
125 rtl_logfile_longTrace( "{ %s\n", name );
128 inline Logfile::Logfile( const sal_Char *project, const sal_Char *author, const sal_Char *name )
129 : m_sName( project)
131 m_sName += " (";
132 m_sName += author;
133 m_sName += ") ";
134 m_sName += name;
135 rtl_logfile_longTrace( "{ %s\n", m_sName.pData->buffer );
138 inline Logfile::~Logfile()
140 rtl_logfile_longTrace( "} %s\n", m_sName.pData->buffer );
143 inline const sal_Char * Logfile::getName()
145 return m_sName.getStr();
149 #ifdef TIMELOG
150 #define RTL_LOGFILE_CONTEXT( instance, name ) ::rtl::Logfile instance( name )
151 #define RTL_LOGFILE_CONTEXT_AUTHOR( instance, project, author, name ) ::rtl::Logfile instance(project, author, name )
152 #define RTL_LOGFILE_CONTEXT_TRACE( instance, message ) \
153 rtl_logfile_longTrace( "| %s : %s\n", \
154 instance.getName(), \
155 message )
156 #define RTL_LOGFILE_CONTEXT_TRACE1( instance , frmt, arg1 ) \
157 rtl_logfile_longTrace( "| %s : ", \
158 instance.getName() ); \
159 rtl_logfile_trace( frmt , arg1 ); \
160 rtl_logfile_trace( "\n" )
161 #define RTL_LOGFILE_CONTEXT_TRACE2( instance , frmt, arg1 , arg2 ) \
162 rtl_logfile_longTrace( "| %s : ", \
163 instance.getName() ); \
164 rtl_logfile_trace( frmt , arg1 , arg2 ); \
165 rtl_logfile_trace( "\n" )
166 #define RTL_LOGFILE_CONTEXT_TRACE3( instance , frmt, arg1 , arg2 , arg3 ) \
167 rtl_logfile_longTrace( "| %s : ", \
168 instance.getName() ); \
169 rtl_logfile_trace( frmt , arg1 , arg2 , arg3 ); \
170 rtl_logfile_trace( "\n" )
172 #else
173 #define RTL_LOGFILE_CONTEXT( instance, name ) ((void)0)
174 #define RTL_LOGFILE_CONTEXT_AUTHOR( instance, project, author, name ) ((void)0)
175 #define RTL_LOGFILE_CONTEXT_TRACE( instance, message ) ((void)0)
176 #define RTL_LOGFILE_CONTEXT_TRACE1( instance, frmt, arg1 ) ((void)0)
177 #define RTL_LOGFILE_CONTEXT_TRACE2( instance, frmt, arg1, arg2 ) ((void)0)
178 #define RTL_LOGFILE_CONTEXT_TRACE3( instance, frmt, arg1, arg2 , arg3 ) ((void)0)
179 #endif
181 // Normal RTL_LOGFILE_* entries will not make it into release versions,
182 // TIMELOG is disabled a few versions prior relase build.
184 // We need some logs also in these builds, eg. for making performance regression tests.
186 // POLICY: Don't use RTL_LOGFILE_PRODUCT_* for your personal logging information.
187 // Be aware that these logs make it into the product shipped to customers.
188 // If you have good reasons for doing this, please contact product management.
190 #define RTL_LOGFILE_PRODUCT_TRACE( string ) \
191 rtl_logfile_longTrace( "| : %s\n", string )
192 #define RTL_LOGFILE_PRODUCT_TRACE1( frmt, arg1 ) \
193 rtl_logfile_longTrace( "| : " ); \
194 rtl_logfile_trace( frmt, arg1 ); \
195 rtl_logfile_trace( "\n" )
196 #define RTL_LOGFILE_PRODUCT_CONTEXT( instance, name ) \
197 ::rtl::Logfile instance( name )
198 #define RTL_LOGFILE_PRODUCT_CONTEXT_TRACE1( instance, frmt, arg1 ) \
199 rtl_logfile_longTrace( "| %s : ", \
200 instance.getName() ); \
201 rtl_logfile_trace( frmt, arg1 ); \
202 rtl_logfile_trace( "\n" )
203 #define RTL_LOGFILE_HASLOGFILE() \
204 rtl_logfile_hasLogFile()
207 #endif