xref: /AOO41X/main/sal/rtl/source/logfile.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir #include <cstdarg>
31*cdf0e10cSrcweir #include <cstdio>
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir #include <stdarg.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <rtl/logfile.h>
36*cdf0e10cSrcweir #include <osl/process.h>
37*cdf0e10cSrcweir #ifndef _OSL_FILE_H_
38*cdf0e10cSrcweir #include <osl/time.h>
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #include <osl/time.h>
41*cdf0e10cSrcweir #include <osl/mutex.hxx>
42*cdf0e10cSrcweir #include <rtl/bootstrap.h>
43*cdf0e10cSrcweir #include <rtl/ustring.hxx>
44*cdf0e10cSrcweir #ifndef _RTL_STRBUF_HXX_
45*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
46*cdf0e10cSrcweir #endif
47*cdf0e10cSrcweir #include <rtl/alloc.h>
48*cdf0e10cSrcweir #include "osl/thread.h"
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <algorithm>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #ifdef _MSC_VER
53*cdf0e10cSrcweir #define vsnprintf _vsnprintf
54*cdf0e10cSrcweir #endif
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using namespace rtl;
57*cdf0e10cSrcweir using namespace osl;
58*cdf0e10cSrcweir using namespace std;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir namespace {
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir static oslFileHandle g_aFile = 0;
63*cdf0e10cSrcweir static sal_Bool g_bHasBeenCalled = sal_False;
64*cdf0e10cSrcweir static const sal_Int32 g_BUFFERSIZE = 4096;
65*cdf0e10cSrcweir static sal_Char *g_buffer = 0;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir class	LoggerGuard
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir public:
70*cdf0e10cSrcweir 	~LoggerGuard();
71*cdf0e10cSrcweir };
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir LoggerGuard::~LoggerGuard()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	if( g_buffer )
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 		sal_Int64 nWritten, nConverted =
78*cdf0e10cSrcweir 			sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
79*cdf0e10cSrcweir 		if( nConverted > 0 )
80*cdf0e10cSrcweir 			osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
81*cdf0e10cSrcweir 		osl_closeFile( g_aFile );
82*cdf0e10cSrcweir 		g_aFile = 0;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 		rtl_freeMemory( g_buffer );
85*cdf0e10cSrcweir 		g_buffer = 0;
86*cdf0e10cSrcweir 		g_bHasBeenCalled = sal_False;
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir // The destructor of this static LoggerGuard is "activated" by the assignment to
91*cdf0e10cSrcweir // g_buffer in init():
92*cdf0e10cSrcweir LoggerGuard loggerGuard;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir Mutex & getLogMutex()
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	static Mutex *pMutex = 0;
97*cdf0e10cSrcweir 	if( !pMutex )
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
100*cdf0e10cSrcweir 		if( ! pMutex )
101*cdf0e10cSrcweir 		{
102*cdf0e10cSrcweir 			static Mutex mutex;
103*cdf0e10cSrcweir 			pMutex = &mutex;
104*cdf0e10cSrcweir 		}
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 	return *pMutex;
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir OUString getFileUrl( const OUString &name )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	OUString aRet;
112*cdf0e10cSrcweir 	if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
113*cdf0e10cSrcweir          != osl_File_E_None )
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         OSL_ASSERT( false );
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	OUString aWorkingDirectory;
119*cdf0e10cSrcweir 	osl_getProcessWorkingDir( &(aWorkingDirectory.pData) );
120*cdf0e10cSrcweir 	osl_getAbsoluteFileURL( aWorkingDirectory.pData, aRet.pData, &(aRet.pData) );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	return aRet;
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir void init() {
126*cdf0e10cSrcweir 	if( !g_bHasBeenCalled )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		MutexGuard guard( getLogMutex() );
129*cdf0e10cSrcweir 		if( ! g_bHasBeenCalled )
130*cdf0e10cSrcweir 		{
131*cdf0e10cSrcweir 			OUString name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
132*cdf0e10cSrcweir 			OUString value;
133*cdf0e10cSrcweir 			if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
134*cdf0e10cSrcweir 			{
135*cdf0e10cSrcweir 				//	Obtain process id.
136*cdf0e10cSrcweir 				oslProcessIdentifier aProcessId = 0;
137*cdf0e10cSrcweir 				oslProcessInfo info;
138*cdf0e10cSrcweir 				info.Size = sizeof (oslProcessInfo);
139*cdf0e10cSrcweir 				if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
140*cdf0e10cSrcweir 					aProcessId = info.Ident;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 				//	Construct name of log file and open the file.
143*cdf0e10cSrcweir 				OUStringBuffer buf( 128 );
144*cdf0e10cSrcweir 				buf.append( value );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 				// if the filename ends with .nopid, the incoming filename is not modified
147*cdf0e10cSrcweir 				if( value.getLength() < 6 /* ".nopid" */ ||
148*cdf0e10cSrcweir 					rtl_ustr_ascii_compare_WithLength(
149*cdf0e10cSrcweir 						value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
150*cdf0e10cSrcweir 				{
151*cdf0e10cSrcweir 					buf.appendAscii( "_" );
152*cdf0e10cSrcweir 					buf.append( (sal_Int32) aProcessId );
153*cdf0e10cSrcweir 					buf.appendAscii( ".log" );
154*cdf0e10cSrcweir 				}
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 				OUString o = getFileUrl( buf.makeStringAndClear() );
157*cdf0e10cSrcweir 				oslFileError e = osl_openFile(
158*cdf0e10cSrcweir 					o.pData, &g_aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 				if( osl_File_E_None == e )
161*cdf0e10cSrcweir 				{
162*cdf0e10cSrcweir 					TimeValue aCurrentTime;
163*cdf0e10cSrcweir 					g_buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
164*cdf0e10cSrcweir 					sal_Int64 nConverted = 0;
165*cdf0e10cSrcweir 					if (osl_getSystemTime (&aCurrentTime))
166*cdf0e10cSrcweir 					{
167*cdf0e10cSrcweir 						nConverted = (sal_Int64 ) sprintf (
168*cdf0e10cSrcweir 								g_buffer,
169*cdf0e10cSrcweir 								"opening log file %f seconds past January 1st 1970\n"
170*cdf0e10cSrcweir 								"corresponding to %" SAL_PRIuUINT32 " ms after timer start\n",
171*cdf0e10cSrcweir 								aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
172*cdf0e10cSrcweir 								osl_getGlobalTimer());
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 						if( nConverted > 0 )
175*cdf0e10cSrcweir                         {
176*cdf0e10cSrcweir                             sal_Int64 nWritten;
177*cdf0e10cSrcweir 							osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
178*cdf0e10cSrcweir                         }
179*cdf0e10cSrcweir 					}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 					nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
182*cdf0e10cSrcweir 					if( nConverted )
183*cdf0e10cSrcweir                     {
184*cdf0e10cSrcweir                         sal_Int64 nWritten;
185*cdf0e10cSrcweir 						osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
186*cdf0e10cSrcweir                     }
187*cdf0e10cSrcweir 				}
188*cdf0e10cSrcweir 				else
189*cdf0e10cSrcweir 				{
190*cdf0e10cSrcweir 					OSL_TRACE( "Couldn't open logfile %s(%d)" , o.getStr(), e );
191*cdf0e10cSrcweir 				}
192*cdf0e10cSrcweir 			}
193*cdf0e10cSrcweir 			g_bHasBeenCalled = sal_True;
194*cdf0e10cSrcweir 		}
195*cdf0e10cSrcweir 	}
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir extern "C" void	SAL_CALL rtl_logfile_trace	( const char *pszFormat, ... )
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	init();
203*cdf0e10cSrcweir 	if( g_buffer )
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 		va_list args;
206*cdf0e10cSrcweir 		va_start(args, pszFormat);
207*cdf0e10cSrcweir 		{
208*cdf0e10cSrcweir 			sal_Int64 nConverted, nWritten;
209*cdf0e10cSrcweir 			MutexGuard guard( getLogMutex() );
210*cdf0e10cSrcweir 			nConverted = vsnprintf( g_buffer , g_BUFFERSIZE, pszFormat, args );
211*cdf0e10cSrcweir 			nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
212*cdf0e10cSrcweir 			if( nConverted > 0 )
213*cdf0e10cSrcweir 				osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 		va_end(args);
216*cdf0e10cSrcweir 	}
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
220*cdf0e10cSrcweir     init();
221*cdf0e10cSrcweir     if (g_buffer != 0) {
222*cdf0e10cSrcweir         sal_uInt32 time = osl_getGlobalTimer();
223*cdf0e10cSrcweir         oslThreadIdentifier threadId = osl_getThreadIdentifier(0);
224*cdf0e10cSrcweir         va_list args;
225*cdf0e10cSrcweir         va_start(args, format);
226*cdf0e10cSrcweir         {
227*cdf0e10cSrcweir             MutexGuard g(getLogMutex());
228*cdf0e10cSrcweir             int n1 = snprintf(
229*cdf0e10cSrcweir                 g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
230*cdf0e10cSrcweir             if (n1 >= 0) {
231*cdf0e10cSrcweir                 sal_uInt64 n2;
232*cdf0e10cSrcweir                 osl_writeFile(
233*cdf0e10cSrcweir                     g_aFile, g_buffer,
234*cdf0e10cSrcweir                     static_cast< sal_uInt64 >(
235*cdf0e10cSrcweir                         std::min(n1, static_cast< int >(g_BUFFERSIZE))),
236*cdf0e10cSrcweir                     &n2);
237*cdf0e10cSrcweir                 n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
238*cdf0e10cSrcweir                 if (n1 > 0) {
239*cdf0e10cSrcweir                     osl_writeFile(
240*cdf0e10cSrcweir                         g_aFile, g_buffer,
241*cdf0e10cSrcweir                         static_cast< sal_uInt64 >(
242*cdf0e10cSrcweir                             std::min(n1, static_cast< int >(g_BUFFERSIZE))),
243*cdf0e10cSrcweir                         &n2);
244*cdf0e10cSrcweir                 }
245*cdf0e10cSrcweir             }
246*cdf0e10cSrcweir         }
247*cdf0e10cSrcweir         va_end(args);
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
252*cdf0e10cSrcweir 	init();
253*cdf0e10cSrcweir     return g_buffer != 0;
254*cdf0e10cSrcweir }
255