Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sal / rtl / logfile.cxx
bloba78eb084ffabe130d56fcc6f92b70178e8a25ba6
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"
36 #include "osl/thread.hxx"
38 #include <algorithm>
40 #ifdef _MSC_VER
41 #define vsnprintf _vsnprintf
42 #endif
44 using namespace osl;
45 using namespace std;
47 using ::rtl::OUString;
48 using ::rtl::OUStringBuffer;
50 namespace {
52 OUString getFileUrl( const OUString &name )
54 OUString aRet;
55 if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
56 != osl_File_E_None )
58 SAL_WARN(
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) );
66 return aRet;
69 static const sal_Int32 g_BUFFERSIZE = 4096;
71 struct Logger {
72 oslFileHandle aFile;
73 sal_Char *buffer;
74 osl::Mutex mutex;
76 Logger();
78 ~Logger();
81 Logger::Logger(): aFile(0), buffer(0)
83 OUString name( "RTL_LOGFILE" );
84 OUString value;
85 if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
87 // Obtain process id.
88 oslProcessIdentifier aProcessId = 0;
89 oslProcessInfo info;
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 );
96 buf.append( value );
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 (
120 buffer,
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());
126 if( nConverted > 0 )
128 sal_Int64 nWritten;
129 osl_writeFile( aFile, buffer, nConverted , (sal_uInt64 *)&nWritten );
133 nConverted = sprintf (buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
134 if( nConverted )
136 sal_Int64 nWritten;
137 osl_writeFile( aFile, buffer, nConverted, (sal_uInt64 *)&nWritten );
140 else
142 SAL_WARN(
143 "sal.rtl",
144 "Couldn't open logfile " << o << '(' << +e << ')');
149 Logger::~Logger()
151 if( buffer )
153 sal_Int64 nWritten, nConverted =
154 sprintf( buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
155 if( nConverted > 0 )
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();
169 if( logger.buffer )
171 va_list args;
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 );
178 if( nConverted > 0 )
179 osl_writeFile( logger.aFile, logger.buffer, nConverted, (sal_uInt64*)&nWritten );
181 va_end(args);
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();
190 va_list args;
191 va_start(args, format);
193 MutexGuard g(logger.mutex);
194 int n1 = snprintf(
195 logger.buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
196 if (n1 >= 0) {
197 sal_uInt64 n2;
198 osl_writeFile(
199 logger.aFile, logger.buffer,
200 static_cast< sal_uInt64 >(
201 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
202 &n2);
203 n1 = vsnprintf(logger.buffer, g_BUFFERSIZE, format, args);
204 if (n1 > 0) {
205 osl_writeFile(
206 logger.aFile, logger.buffer,
207 static_cast< sal_uInt64 >(
208 std::min(n1, static_cast< int >(g_BUFFERSIZE))),
209 &n2);
213 va_end(args);
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: */