1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
35 #include <rtl/logfile.h>
36 #include <osl/process.h>
41 #include <osl/mutex.hxx>
42 #include <rtl/bootstrap.h>
43 #include <rtl/ustring.hxx>
44 #ifndef _RTL_STRBUF_HXX_
45 #include <rtl/ustrbuf.hxx>
47 #include <rtl/alloc.h>
48 #include "osl/thread.h"
53 #define vsnprintf _vsnprintf
62 static oslFileHandle g_aFile
= 0;
63 static sal_Bool g_bHasBeenCalled
= sal_False
;
64 static const sal_Int32 g_BUFFERSIZE
= 4096;
65 static sal_Char
*g_buffer
= 0;
73 LoggerGuard::~LoggerGuard()
77 sal_Int64 nWritten
, nConverted
=
78 sprintf( g_buffer
, "closing log file at %06" SAL_PRIuUINT32
, osl_getGlobalTimer() );
80 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
81 osl_closeFile( g_aFile
);
84 rtl_freeMemory( g_buffer
);
86 g_bHasBeenCalled
= sal_False
;
90 // The destructor of this static LoggerGuard is "activated" by the assignment to
91 // g_buffer in init():
92 LoggerGuard loggerGuard
;
96 static Mutex
*pMutex
= 0;
99 MutexGuard
guard( Mutex::getGlobalMutex() );
109 OUString
getFileUrl( const OUString
&name
)
112 if ( osl_getFileURLFromSystemPath( name
.pData
, &aRet
.pData
)
118 OUString aWorkingDirectory
;
119 osl_getProcessWorkingDir( &(aWorkingDirectory
.pData
) );
120 osl_getAbsoluteFileURL( aWorkingDirectory
.pData
, aRet
.pData
, &(aRet
.pData
) );
126 if( !g_bHasBeenCalled
)
128 MutexGuard
guard( getLogMutex() );
129 if( ! g_bHasBeenCalled
)
131 OUString
name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
133 if( rtl_bootstrap_get( name
.pData
, &value
.pData
, 0 ) )
135 // Obtain process id.
136 oslProcessIdentifier aProcessId
= 0;
138 info
.Size
= sizeof (oslProcessInfo
);
139 if (osl_getProcessInfo (0, osl_Process_IDENTIFIER
, &info
) == osl_Process_E_None
)
140 aProcessId
= info
.Ident
;
142 // Construct name of log file and open the file.
143 OUStringBuffer
buf( 128 );
146 // if the filename ends with .nopid, the incoming filename is not modified
147 if( value
.getLength() < 6 /* ".nopid" */ ||
148 rtl_ustr_ascii_compare_WithLength(
149 value
.getStr() + (value
.getLength()-6) , 6 , ".nopid" ) )
151 buf
.appendAscii( "_" );
152 buf
.append( (sal_Int32
) aProcessId
);
153 buf
.appendAscii( ".log" );
156 OUString o
= getFileUrl( buf
.makeStringAndClear() );
157 oslFileError e
= osl_openFile(
158 o
.pData
, &g_aFile
, osl_File_OpenFlag_Write
|osl_File_OpenFlag_Create
);
160 if( osl_File_E_None
== e
)
162 TimeValue aCurrentTime
;
163 g_buffer
= ( sal_Char
* ) rtl_allocateMemory( g_BUFFERSIZE
);
164 sal_Int64 nConverted
= 0;
165 if (osl_getSystemTime (&aCurrentTime
))
167 nConverted
= (sal_Int64
) sprintf (
169 "opening log file %f seconds past January 1st 1970\n"
170 "corresponding to %" SAL_PRIuUINT32
" ms after timer start\n",
171 aCurrentTime
.Seconds
+ 1e-9 * aCurrentTime
.Nanosec
,
172 osl_getGlobalTimer());
177 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
181 nConverted
= sprintf (g_buffer
, "Process id is %" SAL_PRIuUINT32
"\n", aProcessId
);
185 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
190 OSL_TRACE( "Couldn't open logfile %s(%d)" , o
.getStr(), e
);
193 g_bHasBeenCalled
= sal_True
;
200 extern "C" void SAL_CALL
rtl_logfile_trace ( const char *pszFormat
, ... )
206 va_start(args
, pszFormat
);
208 sal_Int64 nConverted
, nWritten
;
209 MutexGuard
guard( getLogMutex() );
210 nConverted
= vsnprintf( g_buffer
, g_BUFFERSIZE
, pszFormat
, args
);
211 nConverted
= (nConverted
> g_BUFFERSIZE
? g_BUFFERSIZE
: nConverted
);
213 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
219 extern "C" void SAL_CALL
rtl_logfile_longTrace(char const * format
, ...) {
222 sal_uInt32 time
= osl_getGlobalTimer();
223 oslThreadIdentifier threadId
= osl_getThreadIdentifier(0);
225 va_start(args
, format
);
227 MutexGuard
g(getLogMutex());
229 g_buffer
, g_BUFFERSIZE
, "%06" SAL_PRIuUINT32
" %" SAL_PRIuUINT32
" ", time
, threadId
);
234 static_cast< sal_uInt64
>(
235 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
237 n1
= vsnprintf(g_buffer
, g_BUFFERSIZE
, format
, args
);
241 static_cast< sal_uInt64
>(
242 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
251 extern "C" sal_Bool SAL_CALL
rtl_logfile_hasLogFile( void ) {
253 return g_buffer
!= 0;