update dev300-m58
[ooovba.git] / sal / rtl / source / logfile.cxx
blob8d9c69e2aa803d3ba148d885c157819f2d363449
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.cxx,v $
10 * $Revision: 1.13 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
33 #include <cstdarg>
34 #include <cstdio>
35 #include <stdio.h>
36 #include <stdarg.h>
38 #include <rtl/logfile.h>
39 #include <osl/process.h>
40 #ifndef _OSL_FILE_H_
41 #include <osl/time.h>
42 #endif
43 #include <osl/time.h>
44 #include <osl/mutex.hxx>
45 #include <rtl/bootstrap.h>
46 #include <rtl/ustring.hxx>
47 #ifndef _RTL_STRBUF_HXX_
48 #include <rtl/ustrbuf.hxx>
49 #endif
50 #include <rtl/alloc.h>
51 #include "osl/thread.h"
53 #include <algorithm>
55 #ifdef _MSC_VER
56 #define vsnprintf _vsnprintf
57 #endif
59 using namespace rtl;
60 using namespace osl;
61 using namespace std;
63 namespace {
65 static oslFileHandle g_aFile = 0;
66 static sal_Bool g_bHasBeenCalled = sal_False;
67 static const sal_Int32 g_BUFFERSIZE = 4096;
68 static sal_Char *g_buffer = 0;
70 class LoggerGuard
72 public:
73 ~LoggerGuard();
76 LoggerGuard::~LoggerGuard()
78 if( g_buffer )
80 sal_Int64 nWritten, nConverted =
81 sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
82 if( nConverted > 0 )
83 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
84 osl_closeFile( g_aFile );
85 g_aFile = 0;
87 rtl_freeMemory( g_buffer );
88 g_buffer = 0;
89 g_bHasBeenCalled = sal_False;
93 // The destructor of this static LoggerGuard is "activated" by the assignment to
94 // g_buffer in init():
95 LoggerGuard loggerGuard;
97 Mutex & getLogMutex()
99 static Mutex *pMutex = 0;
100 if( !pMutex )
102 MutexGuard guard( Mutex::getGlobalMutex() );
103 if( ! pMutex )
105 static Mutex mutex;
106 pMutex = &mutex;
109 return *pMutex;
112 OUString getFileUrl( const OUString &name )
114 OUString aRet;
115 if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
116 != osl_File_E_None )
118 OSL_ASSERT( false );
121 OUString aWorkingDirectory;
122 osl_getProcessWorkingDir( &(aWorkingDirectory.pData) );
123 osl_getAbsoluteFileURL( aWorkingDirectory.pData, aRet.pData, &(aRet.pData) );
125 return aRet;
128 void init() {
129 if( !g_bHasBeenCalled )
131 MutexGuard guard( getLogMutex() );
132 if( ! g_bHasBeenCalled )
134 OUString name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
135 OUString value;
136 if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
138 // Obtain process id.
139 oslProcessIdentifier aProcessId = 0;
140 oslProcessInfo info;
141 info.Size = sizeof (oslProcessInfo);
142 if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
143 aProcessId = info.Ident;
145 // Construct name of log file and open the file.
146 OUStringBuffer buf( 128 );
147 buf.append( value );
149 // if the filename ends with .nopid, the incoming filename is not modified
150 if( value.getLength() < 6 /* ".nopid" */ ||
151 rtl_ustr_ascii_compare_WithLength(
152 value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
154 buf.appendAscii( "_" );
155 buf.append( (sal_Int32) aProcessId );
156 buf.appendAscii( ".log" );
159 OUString o = getFileUrl( buf.makeStringAndClear() );
160 oslFileError e = osl_openFile(
161 o.pData, &g_aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
163 if( osl_File_E_None == e )
165 TimeValue aCurrentTime;
166 g_buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
167 sal_Int64 nConverted = 0;
168 if (osl_getSystemTime (&aCurrentTime))
170 nConverted = (sal_Int64 ) sprintf (
171 g_buffer,
172 "opening log file %f seconds past January 1st 1970\n"
173 "corresponding to %" SAL_PRIuUINT32 " ms after timer start\n",
174 aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
175 osl_getGlobalTimer());
177 if( nConverted > 0 )
179 sal_Int64 nWritten;
180 osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
184 nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
185 if( nConverted )
187 sal_Int64 nWritten;
188 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
191 else
193 OSL_TRACE( "Couldn't open logfile %s(%d)" , o.getStr(), e );
196 g_bHasBeenCalled = sal_True;
203 extern "C" void SAL_CALL rtl_logfile_trace ( const char *pszFormat, ... )
205 init();
206 if( g_buffer )
208 va_list args;
209 va_start(args, pszFormat);
211 sal_Int64 nConverted, nWritten;
212 MutexGuard guard( getLogMutex() );
213 nConverted = vsnprintf( g_buffer , g_BUFFERSIZE, pszFormat, args );
214 nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
215 if( nConverted > 0 )
216 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
218 va_end(args);
222 extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
223 init();
224 if (g_buffer != 0) {
225 sal_uInt32 time = osl_getGlobalTimer();
226 oslThreadIdentifier threadId = osl_getThreadIdentifier(0);
227 va_list args;
228 va_start(args, format);
230 MutexGuard g(getLogMutex());
231 int n1 = snprintf(
232 g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
233 if (n1 >= 0) {
234 sal_uInt64 n2;
235 osl_writeFile(
236 g_aFile, g_buffer,
237 static_cast< sal_uInt64 >(
238 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
239 &n2);
240 n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
241 if (n1 > 0) {
242 osl_writeFile(
243 g_aFile, g_buffer,
244 static_cast< sal_uInt64 >(
245 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
246 &n2);
250 va_end(args);
254 extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
255 init();
256 return g_buffer != 0;