bump product version to 4.1.6.2
[LibreOffice.git] / sal / rtl / logfile.cxx
blobc33eb5800ae81947e68e23ca30718aaae79f08f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <cstdarg>
21 #include <cstdio>
22 #include <stdio.h>
23 #include <stdarg.h>
25 #include <rtl/logfile.h>
26 #include <osl/process.h>
27 #include <osl/time.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"
37 #include <algorithm>
39 #ifdef _MSC_VER
40 #define vsnprintf _vsnprintf
41 #endif
43 using namespace osl;
44 using namespace std;
46 using ::rtl::OUString;
47 using ::rtl::OUStringBuffer;
49 namespace {
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;
56 class LoggerGuard
58 public:
59 ~LoggerGuard();
62 LoggerGuard::~LoggerGuard()
64 if( g_buffer )
66 sal_Int64 nWritten, nConverted =
67 sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
68 if( nConverted > 0 )
69 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
70 osl_closeFile( g_aFile );
71 g_aFile = 0;
73 rtl_freeMemory( g_buffer );
74 g_buffer = 0;
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;
83 namespace
85 class theLogMutex : public rtl::Static<osl::Mutex, theLogMutex>{};
88 static Mutex & getLogMutex()
90 return theLogMutex::get();
93 OUString getFileUrl( const OUString &name )
95 OUString aRet;
96 if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
97 != osl_File_E_None )
99 SAL_WARN(
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) );
107 return aRet;
110 void init() {
111 if( !g_bHasBeenCalled )
113 MutexGuard guard( getLogMutex() );
114 if( ! g_bHasBeenCalled )
116 OUString name( "RTL_LOGFILE" );
117 OUString value;
118 if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
120 // Obtain process id.
121 oslProcessIdentifier aProcessId = 0;
122 oslProcessInfo info;
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 );
129 buf.append( value );
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 (
153 g_buffer,
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());
159 if( nConverted > 0 )
161 sal_Int64 nWritten;
162 osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
166 nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
167 if( nConverted )
169 sal_Int64 nWritten;
170 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
173 else
175 SAL_WARN(
176 "sal.rtl",
177 "Couldn't open logfile " << o << '(' << +e << ')');
180 g_bHasBeenCalled = sal_True;
187 extern "C" void SAL_CALL rtl_logfile_trace ( const char *pszFormat, ... )
189 init();
190 if( g_buffer )
192 va_list args;
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 );
199 if( nConverted > 0 )
200 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
202 va_end(args);
206 extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
207 init();
208 if (g_buffer != 0) {
209 sal_uInt32 time = osl_getGlobalTimer();
210 oslThreadIdentifier threadId = osl_getThreadIdentifier(0);
211 va_list args;
212 va_start(args, format);
214 MutexGuard g(getLogMutex());
215 int n1 = snprintf(
216 g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
217 if (n1 >= 0) {
218 sal_uInt64 n2;
219 osl_writeFile(
220 g_aFile, g_buffer,
221 static_cast< sal_uInt64 >(
222 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
223 &n2);
224 n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
225 if (n1 > 0) {
226 osl_writeFile(
227 g_aFile, g_buffer,
228 static_cast< sal_uInt64 >(
229 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
230 &n2);
234 va_end(args);
238 extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
239 init();
240 return g_buffer != 0;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */