1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <rtl/logfile.h>
26 #include <osl/process.h>
28 #include <osl/mutex.hxx>
29 #include <rtl/bootstrap.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <rtl/alloc.h>
33 #include <rtl/instance.hxx>
34 #include <sal/log.hxx>
35 #include "osl/thread.h"
40 #define vsnprintf _vsnprintf
46 using ::rtl::OUString
;
47 using ::rtl::OUStringBuffer
;
51 static oslFileHandle g_aFile
= 0;
52 static sal_Bool g_bHasBeenCalled
= sal_False
;
53 static const sal_Int32 g_BUFFERSIZE
= 4096;
54 static sal_Char
*g_buffer
= 0;
62 LoggerGuard::~LoggerGuard()
66 sal_Int64 nWritten
, nConverted
=
67 sprintf( g_buffer
, "closing log file at %06" SAL_PRIuUINT32
, osl_getGlobalTimer() );
69 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
70 osl_closeFile( g_aFile
);
73 rtl_freeMemory( g_buffer
);
75 g_bHasBeenCalled
= sal_False
;
79 // The destructor of this static LoggerGuard is "activated" by the assignment to
80 // g_buffer in init():
81 LoggerGuard loggerGuard
;
85 class theLogMutex
: public rtl::Static
<osl::Mutex
, theLogMutex
>{};
88 static Mutex
& getLogMutex()
90 return theLogMutex::get();
93 OUString
getFileUrl( const OUString
&name
)
96 if ( osl_getFileURLFromSystemPath( name
.pData
, &aRet
.pData
)
100 "sal.rtl", "osl_getFileURLFromSystemPath failed for \"" << name
<< '"');
103 OUString aWorkingDirectory
;
104 osl_getProcessWorkingDir( &(aWorkingDirectory
.pData
) );
105 osl_getAbsoluteFileURL( aWorkingDirectory
.pData
, aRet
.pData
, &(aRet
.pData
) );
111 if( !g_bHasBeenCalled
)
113 MutexGuard
guard( getLogMutex() );
114 if( ! g_bHasBeenCalled
)
116 OUString
name( "RTL_LOGFILE" );
118 if( rtl_bootstrap_get( name
.pData
, &value
.pData
, 0 ) )
120 // Obtain process id.
121 oslProcessIdentifier aProcessId
= 0;
123 info
.Size
= sizeof (oslProcessInfo
);
124 if (osl_getProcessInfo (0, osl_Process_IDENTIFIER
, &info
) == osl_Process_E_None
)
125 aProcessId
= info
.Ident
;
127 // Construct name of log file and open the file.
128 OUStringBuffer
buf( 128 );
131 // if the filename ends with .nopid, the incoming filename is not modified
132 if( value
.getLength() < 6 /* ".nopid" */ ||
133 rtl_ustr_ascii_compare_WithLength(
134 value
.getStr() + (value
.getLength()-6) , 6 , ".nopid" ) )
136 buf
.appendAscii( "_" );
137 buf
.append( (sal_Int32
) aProcessId
);
138 buf
.appendAscii( ".log" );
141 OUString o
= getFileUrl( buf
.makeStringAndClear() );
142 oslFileError e
= osl_openFile(
143 o
.pData
, &g_aFile
, osl_File_OpenFlag_Write
|osl_File_OpenFlag_Create
);
145 if( osl_File_E_None
== e
)
147 TimeValue aCurrentTime
;
148 g_buffer
= ( sal_Char
* ) rtl_allocateMemory( g_BUFFERSIZE
);
149 sal_Int64 nConverted
= 0;
150 if (osl_getSystemTime (&aCurrentTime
))
152 nConverted
= (sal_Int64
) sprintf (
154 "opening log file %f seconds past January 1st 1970\n"
155 "corresponding to %" SAL_PRIuUINT32
" ms after timer start\n",
156 aCurrentTime
.Seconds
+ 1e-9 * aCurrentTime
.Nanosec
,
157 osl_getGlobalTimer());
162 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
166 nConverted
= sprintf (g_buffer
, "Process id is %" SAL_PRIuUINT32
"\n", aProcessId
);
170 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
177 "Couldn't open logfile " << o
<< '(' << +e
<< ')');
180 g_bHasBeenCalled
= sal_True
;
187 extern "C" void SAL_CALL
rtl_logfile_trace ( const char *pszFormat
, ... )
193 va_start(args
, pszFormat
);
195 sal_Int64 nConverted
, nWritten
;
196 MutexGuard
guard( getLogMutex() );
197 nConverted
= vsnprintf( g_buffer
, g_BUFFERSIZE
, pszFormat
, args
);
198 nConverted
= (nConverted
> g_BUFFERSIZE
? g_BUFFERSIZE
: nConverted
);
200 osl_writeFile( g_aFile
, g_buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
206 extern "C" void SAL_CALL
rtl_logfile_longTrace(char const * format
, ...) {
209 sal_uInt32 time
= osl_getGlobalTimer();
210 oslThreadIdentifier threadId
= osl_getThreadIdentifier(0);
212 va_start(args
, format
);
214 MutexGuard
g(getLogMutex());
216 g_buffer
, g_BUFFERSIZE
, "%06" SAL_PRIuUINT32
" %" SAL_PRIuUINT32
" ", time
, threadId
);
221 static_cast< sal_uInt64
>(
222 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
224 n1
= vsnprintf(g_buffer
, g_BUFFERSIZE
, format
, args
);
228 static_cast< sal_uInt64
>(
229 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
238 extern "C" sal_Bool SAL_CALL
rtl_logfile_hasLogFile( void ) {
240 return g_buffer
!= 0;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */