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"
36 #include "osl/thread.hxx"
41 #define vsnprintf _vsnprintf
47 using ::rtl::OUString
;
48 using ::rtl::OUStringBuffer
;
52 OUString
getFileUrl( const OUString
&name
)
55 if ( osl_getFileURLFromSystemPath( name
.pData
, &aRet
.pData
)
59 "sal.rtl", "osl_getFileURLFromSystemPath failed for \"" << name
<< '"');
62 OUString aWorkingDirectory
;
63 osl_getProcessWorkingDir( &(aWorkingDirectory
.pData
) );
64 osl_getAbsoluteFileURL( aWorkingDirectory
.pData
, aRet
.pData
, &(aRet
.pData
) );
69 static const sal_Int32 g_BUFFERSIZE
= 4096;
81 Logger::Logger(): aFile(0), buffer(0)
83 OUString
name( "RTL_LOGFILE" );
85 if( rtl_bootstrap_get( name
.pData
, &value
.pData
, 0 ) )
88 oslProcessIdentifier aProcessId
= 0;
90 info
.Size
= sizeof (oslProcessInfo
);
91 if (osl_getProcessInfo (0, osl_Process_IDENTIFIER
, &info
) == osl_Process_E_None
)
92 aProcessId
= info
.Ident
;
94 // Construct name of log file and open the file.
95 OUStringBuffer
buf( 128 );
98 // if the filename ends with .nopid, the incoming filename is not modified
99 if( value
.getLength() < 6 /* ".nopid" */ ||
100 rtl_ustr_ascii_compare_WithLength(
101 value
.getStr() + (value
.getLength()-6) , 6 , ".nopid" ) )
103 buf
.appendAscii( "_" );
104 buf
.append( (sal_Int32
) aProcessId
);
105 buf
.appendAscii( ".log" );
108 OUString o
= getFileUrl( buf
.makeStringAndClear() );
109 oslFileError e
= osl_openFile(
110 o
.pData
, &aFile
, osl_File_OpenFlag_Write
|osl_File_OpenFlag_Create
);
112 if( osl_File_E_None
== e
)
114 TimeValue aCurrentTime
;
115 buffer
= ( sal_Char
* ) rtl_allocateMemory( g_BUFFERSIZE
);
116 sal_Int64 nConverted
= 0;
117 if (osl_getSystemTime (&aCurrentTime
))
119 nConverted
= (sal_Int64
) sprintf (
121 "opening log file %f seconds past January 1st 1970\n"
122 "corresponding to %" SAL_PRIuUINT32
" ms after timer start\n",
123 aCurrentTime
.Seconds
+ 1e-9 * aCurrentTime
.Nanosec
,
124 osl_getGlobalTimer());
129 osl_writeFile( aFile
, buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
133 nConverted
= sprintf (buffer
, "Process id is %" SAL_PRIuUINT32
"\n", aProcessId
);
137 osl_writeFile( aFile
, buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
144 "Couldn't open logfile " << o
<< '(' << +e
<< ')');
153 sal_Int64 nWritten
, nConverted
=
154 sprintf( buffer
, "closing log file at %06" SAL_PRIuUINT32
, osl_getGlobalTimer() );
156 osl_writeFile( aFile
, buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
157 osl_closeFile( aFile
);
158 rtl_freeMemory( buffer
);
162 struct theLogger
: public rtl::Static
<Logger
, theLogger
> {};
166 extern "C" void SAL_CALL
rtl_logfile_trace ( const char *pszFormat
, ... )
168 Logger
& logger
= theLogger::get();
172 va_start(args
, pszFormat
);
174 sal_Int64 nConverted
, nWritten
;
175 MutexGuard
guard( logger
.mutex
);
176 nConverted
= vsnprintf( logger
.buffer
, g_BUFFERSIZE
, pszFormat
, args
);
177 nConverted
= (nConverted
> g_BUFFERSIZE
? g_BUFFERSIZE
: nConverted
);
179 osl_writeFile( logger
.aFile
, logger
.buffer
, nConverted
, (sal_uInt64
*)&nWritten
);
185 extern "C" void SAL_CALL
rtl_logfile_longTrace(char const * format
, ...) {
186 Logger
& logger
= theLogger::get();
187 if (logger
.buffer
!= 0) {
188 sal_uInt32 time
= osl_getGlobalTimer();
189 oslThreadIdentifier threadId
= osl::Thread::getCurrentIdentifier();
191 va_start(args
, format
);
193 MutexGuard
g(logger
.mutex
);
195 logger
.buffer
, g_BUFFERSIZE
, "%06" SAL_PRIuUINT32
" %" SAL_PRIuUINT32
" ", time
, threadId
);
199 logger
.aFile
, logger
.buffer
,
200 static_cast< sal_uInt64
>(
201 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
203 n1
= vsnprintf(logger
.buffer
, g_BUFFERSIZE
, format
, args
);
206 logger
.aFile
, logger
.buffer
,
207 static_cast< sal_uInt64
>(
208 std::min(n1
, static_cast< int >(g_BUFFERSIZE
))),
217 extern "C" sal_Bool SAL_CALL
rtl_logfile_hasLogFile( void ) {
218 return theLogger::get().buffer
!= 0;
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */