1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: logfile.cxx,v $
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"
38 #include <rtl/logfile.h>
39 #include <osl/process.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>
50 #include <rtl/alloc.h>
51 #include "osl/thread.h"
56 #define vsnprintf _vsnprintf
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;
76 LoggerGuard::~LoggerGuard()
80 sal_Int64 nWritten
, nConverted
=
81 sprintf( g_buffer
, "closing log file at %06" SAL_PRIuUINT32
, osl_getGlobalTimer() );
83 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
84 osl_closeFile( g_aFile
);
87 rtl_freeMemory( g_buffer
);
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
;
99 static Mutex
*pMutex
= 0;
102 MutexGuard
guard( Mutex::getGlobalMutex() );
112 OUString
getFileUrl( const OUString
&name
)
115 if ( osl_getFileURLFromSystemPath( name
.pData
, &aRet
.pData
)
121 OUString aWorkingDirectory
;
122 osl_getProcessWorkingDir( &(aWorkingDirectory
.pData
) );
123 osl_getAbsoluteFileURL( aWorkingDirectory
.pData
, aRet
.pData
, &(aRet
.pData
) );
129 if( !g_bHasBeenCalled
)
131 MutexGuard
guard( getLogMutex() );
132 if( ! g_bHasBeenCalled
)
134 OUString
name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
136 if( rtl_bootstrap_get( name
.pData
, &value
.pData
, 0 ) )
138 // Obtain process id.
139 oslProcessIdentifier aProcessId
= 0;
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 );
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 (
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());
180 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
184 nConverted
= sprintf (g_buffer
, "Process id is %" SAL_PRIuUINT32
"\n", aProcessId
);
188 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
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
, ... )
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
);
216 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
222 extern "C" void SAL_CALL
rtl_logfile_longTrace(char const * format
, ...) {
225 sal_uInt32 time
= osl_getGlobalTimer();
226 oslThreadIdentifier threadId
= osl_getThreadIdentifier(0);
228 va_start(args
, format
);
230 MutexGuard
g(getLogMutex());
232 g_buffer
, g_BUFFERSIZE
, "%06" SAL_PRIuUINT32
" %" SAL_PRIuUINT32
" ", time
, threadId
);
237 static_cast< sal_uInt64
>(
238 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
240 n1
= vsnprintf(g_buffer
, g_BUFFERSIZE
, format
, args
);
244 static_cast< sal_uInt64
>(
245 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
254 extern "C" sal_Bool SAL_CALL
rtl_logfile_hasLogFile( void ) {
256 return g_buffer
!= 0;