1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir #include <cstdarg>
27cdf0e10cSrcweir #include <cstdio>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <stdarg.h>
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <rtl/logfile.h>
32cdf0e10cSrcweir #include <osl/process.h>
33cdf0e10cSrcweir #ifndef _OSL_FILE_H_
34cdf0e10cSrcweir #include <osl/time.h>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <osl/time.h>
37cdf0e10cSrcweir #include <osl/mutex.hxx>
38cdf0e10cSrcweir #include <rtl/bootstrap.h>
39cdf0e10cSrcweir #include <rtl/ustring.hxx>
40cdf0e10cSrcweir #ifndef _RTL_STRBUF_HXX_
41cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <rtl/alloc.h>
44cdf0e10cSrcweir #include "osl/thread.h"
45cdf0e10cSrcweir
46cdf0e10cSrcweir #include <algorithm>
47cdf0e10cSrcweir
48cdf0e10cSrcweir #ifdef _MSC_VER
49cdf0e10cSrcweir #define vsnprintf _vsnprintf
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir using namespace osl;
54cdf0e10cSrcweir using namespace std;
55cdf0e10cSrcweir
56cdf0e10cSrcweir namespace {
57cdf0e10cSrcweir
58cdf0e10cSrcweir static oslFileHandle g_aFile = 0;
59cdf0e10cSrcweir static sal_Bool g_bHasBeenCalled = sal_False;
60cdf0e10cSrcweir static const sal_Int32 g_BUFFERSIZE = 4096;
61cdf0e10cSrcweir static sal_Char *g_buffer = 0;
62cdf0e10cSrcweir
63cdf0e10cSrcweir class LoggerGuard
64cdf0e10cSrcweir {
65cdf0e10cSrcweir public:
66cdf0e10cSrcweir ~LoggerGuard();
67cdf0e10cSrcweir };
68cdf0e10cSrcweir
~LoggerGuard()69cdf0e10cSrcweir LoggerGuard::~LoggerGuard()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir if( g_buffer )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir sal_Int64 nWritten, nConverted =
74cdf0e10cSrcweir sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
75cdf0e10cSrcweir if( nConverted > 0 )
76cdf0e10cSrcweir osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
77cdf0e10cSrcweir osl_closeFile( g_aFile );
78cdf0e10cSrcweir g_aFile = 0;
79cdf0e10cSrcweir
80cdf0e10cSrcweir rtl_freeMemory( g_buffer );
81cdf0e10cSrcweir g_buffer = 0;
82cdf0e10cSrcweir g_bHasBeenCalled = sal_False;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
86cdf0e10cSrcweir // The destructor of this static LoggerGuard is "activated" by the assignment to
87cdf0e10cSrcweir // g_buffer in init():
88cdf0e10cSrcweir LoggerGuard loggerGuard;
89cdf0e10cSrcweir
getLogMutex()90cdf0e10cSrcweir Mutex & getLogMutex()
91cdf0e10cSrcweir {
92cdf0e10cSrcweir static Mutex *pMutex = 0;
93cdf0e10cSrcweir if( !pMutex )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir MutexGuard guard( Mutex::getGlobalMutex() );
96cdf0e10cSrcweir if( ! pMutex )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir static Mutex mutex;
99cdf0e10cSrcweir pMutex = &mutex;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir }
102cdf0e10cSrcweir return *pMutex;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
getFileUrl(const OUString & name)105cdf0e10cSrcweir OUString getFileUrl( const OUString &name )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir OUString aRet;
108cdf0e10cSrcweir if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
109cdf0e10cSrcweir != osl_File_E_None )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir OSL_ASSERT( false );
112cdf0e10cSrcweir }
113cdf0e10cSrcweir
114cdf0e10cSrcweir OUString aWorkingDirectory;
115cdf0e10cSrcweir osl_getProcessWorkingDir( &(aWorkingDirectory.pData) );
116cdf0e10cSrcweir osl_getAbsoluteFileURL( aWorkingDirectory.pData, aRet.pData, &(aRet.pData) );
117cdf0e10cSrcweir
118cdf0e10cSrcweir return aRet;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
init()121cdf0e10cSrcweir void init() {
122cdf0e10cSrcweir if( !g_bHasBeenCalled )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir MutexGuard guard( getLogMutex() );
125cdf0e10cSrcweir if( ! g_bHasBeenCalled )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir OUString name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
128cdf0e10cSrcweir OUString value;
129cdf0e10cSrcweir if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir // Obtain process id.
132cdf0e10cSrcweir oslProcessIdentifier aProcessId = 0;
133cdf0e10cSrcweir oslProcessInfo info;
134cdf0e10cSrcweir info.Size = sizeof (oslProcessInfo);
135cdf0e10cSrcweir if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
136cdf0e10cSrcweir aProcessId = info.Ident;
137cdf0e10cSrcweir
138cdf0e10cSrcweir // Construct name of log file and open the file.
139cdf0e10cSrcweir OUStringBuffer buf( 128 );
140cdf0e10cSrcweir buf.append( value );
141cdf0e10cSrcweir
142cdf0e10cSrcweir // if the filename ends with .nopid, the incoming filename is not modified
143cdf0e10cSrcweir if( value.getLength() < 6 /* ".nopid" */ ||
144cdf0e10cSrcweir rtl_ustr_ascii_compare_WithLength(
145cdf0e10cSrcweir value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir buf.appendAscii( "_" );
148cdf0e10cSrcweir buf.append( (sal_Int32) aProcessId );
149cdf0e10cSrcweir buf.appendAscii( ".log" );
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir OUString o = getFileUrl( buf.makeStringAndClear() );
153cdf0e10cSrcweir oslFileError e = osl_openFile(
154cdf0e10cSrcweir o.pData, &g_aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
155cdf0e10cSrcweir
156cdf0e10cSrcweir if( osl_File_E_None == e )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir TimeValue aCurrentTime;
159cdf0e10cSrcweir g_buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
160cdf0e10cSrcweir sal_Int64 nConverted = 0;
161cdf0e10cSrcweir if (osl_getSystemTime (&aCurrentTime))
162cdf0e10cSrcweir {
163cdf0e10cSrcweir nConverted = (sal_Int64 ) sprintf (
164cdf0e10cSrcweir g_buffer,
165cdf0e10cSrcweir "opening log file %f seconds past January 1st 1970\n"
166cdf0e10cSrcweir "corresponding to %" SAL_PRIuUINT32 " ms after timer start\n",
167cdf0e10cSrcweir aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
168cdf0e10cSrcweir osl_getGlobalTimer());
169cdf0e10cSrcweir
170cdf0e10cSrcweir if( nConverted > 0 )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir sal_Int64 nWritten;
173cdf0e10cSrcweir osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
174cdf0e10cSrcweir }
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177cdf0e10cSrcweir nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
178cdf0e10cSrcweir if( nConverted )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir sal_Int64 nWritten;
181cdf0e10cSrcweir osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir }
184cdf0e10cSrcweir else
185cdf0e10cSrcweir {
186cdf0e10cSrcweir OSL_TRACE( "Couldn't open logfile %s(%d)" , o.getStr(), e );
187cdf0e10cSrcweir }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir g_bHasBeenCalled = sal_True;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir }
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
rtl_logfile_trace(const char * pszFormat,...)196cdf0e10cSrcweir extern "C" void SAL_CALL rtl_logfile_trace ( const char *pszFormat, ... )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir init();
199cdf0e10cSrcweir if( g_buffer )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir va_list args;
202cdf0e10cSrcweir va_start(args, pszFormat);
203cdf0e10cSrcweir {
204cdf0e10cSrcweir sal_Int64 nConverted, nWritten;
205cdf0e10cSrcweir MutexGuard guard( getLogMutex() );
206cdf0e10cSrcweir nConverted = vsnprintf( g_buffer , g_BUFFERSIZE, pszFormat, args );
207cdf0e10cSrcweir nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
208cdf0e10cSrcweir if( nConverted > 0 )
209cdf0e10cSrcweir osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
210cdf0e10cSrcweir }
211cdf0e10cSrcweir va_end(args);
212cdf0e10cSrcweir }
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
rtl_logfile_longTrace(char const * format,...)215cdf0e10cSrcweir extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
216cdf0e10cSrcweir init();
217cdf0e10cSrcweir if (g_buffer != 0) {
218cdf0e10cSrcweir sal_uInt32 time = osl_getGlobalTimer();
219cdf0e10cSrcweir oslThreadIdentifier threadId = osl_getThreadIdentifier(0);
220cdf0e10cSrcweir va_list args;
221cdf0e10cSrcweir va_start(args, format);
222cdf0e10cSrcweir {
223cdf0e10cSrcweir MutexGuard g(getLogMutex());
224cdf0e10cSrcweir int n1 = snprintf(
225cdf0e10cSrcweir g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
226cdf0e10cSrcweir if (n1 >= 0) {
227cdf0e10cSrcweir sal_uInt64 n2;
228cdf0e10cSrcweir osl_writeFile(
229cdf0e10cSrcweir g_aFile, g_buffer,
230cdf0e10cSrcweir static_cast< sal_uInt64 >(
231cdf0e10cSrcweir std::min(n1, static_cast< int >(g_BUFFERSIZE))),
232cdf0e10cSrcweir &n2);
233cdf0e10cSrcweir n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
234cdf0e10cSrcweir if (n1 > 0) {
235cdf0e10cSrcweir osl_writeFile(
236cdf0e10cSrcweir g_aFile, g_buffer,
237cdf0e10cSrcweir static_cast< sal_uInt64 >(
238cdf0e10cSrcweir std::min(n1, static_cast< int >(g_BUFFERSIZE))),
239cdf0e10cSrcweir &n2);
240cdf0e10cSrcweir }
241cdf0e10cSrcweir }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir va_end(args);
244cdf0e10cSrcweir }
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
rtl_logfile_hasLogFile(void)247cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
248cdf0e10cSrcweir init();
249cdf0e10cSrcweir return g_buffer != 0;
250cdf0e10cSrcweir }
251