LCOV - code coverage report
Current view: top level - libreoffice/sal/rtl/source - logfile.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 24 88 27.3 %
Date: 2012-12-27 Functions: 7 9 77.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <cstdarg>
      21             : #include <cstdio>
      22             : #include <stdio.h>
      23             : #include <stdarg.h>
      24             : 
      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             : 
      37             : #include <algorithm>
      38             : 
      39             : #ifdef _MSC_VER
      40             : #define vsnprintf _vsnprintf
      41             : #endif
      42             : 
      43             : using namespace osl;
      44             : using namespace std;
      45             : 
      46             : using ::rtl::OUString;
      47             : using ::rtl::OUStringBuffer;
      48             : 
      49             : namespace {
      50             : 
      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;
      55             : 
      56             : class   LoggerGuard
      57             : {
      58             : public:
      59             :     ~LoggerGuard();
      60             : };
      61             : 
      62        1277 : LoggerGuard::~LoggerGuard()
      63             : {
      64        1277 :     if( g_buffer )
      65             :     {
      66             :         sal_Int64 nWritten, nConverted =
      67           0 :             sprintf( g_buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
      68           0 :         if( nConverted > 0 )
      69           0 :             osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
      70           0 :         osl_closeFile( g_aFile );
      71           0 :         g_aFile = 0;
      72             : 
      73           0 :         rtl_freeMemory( g_buffer );
      74           0 :         g_buffer = 0;
      75           0 :         g_bHasBeenCalled = sal_False;
      76             :     }
      77        1277 : }
      78             : 
      79             : // The destructor of this static LoggerGuard is "activated" by the assignment to
      80             : // g_buffer in init():
      81        1277 : LoggerGuard loggerGuard;
      82             : 
      83             : namespace
      84             : {
      85             :     class theLogMutex : public rtl::Static<osl::Mutex, theLogMutex>{};
      86             : }
      87             : 
      88          13 : static Mutex & getLogMutex()
      89             : {
      90          13 :     return theLogMutex::get();
      91             : }
      92             : 
      93           0 : OUString getFileUrl( const OUString &name )
      94             : {
      95           0 :     OUString aRet;
      96           0 :     if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
      97             :          != osl_File_E_None )
      98             :     {
      99             :         SAL_WARN(
     100             :             "sal.rtl", "osl_getFileURLFromSystemPath failed for \"" << name << '"');
     101             :     }
     102             : 
     103           0 :     OUString aWorkingDirectory;
     104           0 :     osl_getProcessWorkingDir( &(aWorkingDirectory.pData) );
     105           0 :     osl_getAbsoluteFileURL( aWorkingDirectory.pData, aRet.pData, &(aRet.pData) );
     106             : 
     107           0 :     return aRet;
     108             : }
     109             : 
     110        3282 : void init() {
     111        3282 :     if( !g_bHasBeenCalled )
     112             :     {
     113          13 :         MutexGuard guard( getLogMutex() );
     114          13 :         if( ! g_bHasBeenCalled )
     115             :         {
     116          13 :             OUString name( RTL_CONSTASCII_USTRINGPARAM( "RTL_LOGFILE" ) );
     117          13 :             OUString value;
     118          13 :             if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
     119             :             {
     120             :                 //  Obtain process id.
     121           0 :                 oslProcessIdentifier aProcessId = 0;
     122             :                 oslProcessInfo info;
     123           0 :                 info.Size = sizeof (oslProcessInfo);
     124           0 :                 if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
     125           0 :                     aProcessId = info.Ident;
     126             : 
     127             :                 //  Construct name of log file and open the file.
     128           0 :                 OUStringBuffer buf( 128 );
     129           0 :                 buf.append( value );
     130             : 
     131             :                 // if the filename ends with .nopid, the incoming filename is not modified
     132           0 :                 if( value.getLength() < 6 /* ".nopid" */ ||
     133             :                     rtl_ustr_ascii_compare_WithLength(
     134           0 :                         value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
     135             :                 {
     136           0 :                     buf.appendAscii( "_" );
     137           0 :                     buf.append( (sal_Int32) aProcessId );
     138           0 :                     buf.appendAscii( ".log" );
     139             :                 }
     140             : 
     141           0 :                 OUString o = getFileUrl( buf.makeStringAndClear() );
     142             :                 oslFileError e = osl_openFile(
     143           0 :                     o.pData, &g_aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
     144             : 
     145           0 :                 if( osl_File_E_None == e )
     146             :                 {
     147             :                     TimeValue aCurrentTime;
     148           0 :                     g_buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
     149           0 :                     sal_Int64 nConverted = 0;
     150           0 :                     if (osl_getSystemTime (&aCurrentTime))
     151             :                     {
     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           0 :                                 osl_getGlobalTimer());
     158             : 
     159           0 :                         if( nConverted > 0 )
     160             :                         {
     161             :                             sal_Int64 nWritten;
     162           0 :                             osl_writeFile( g_aFile, g_buffer, nConverted , (sal_uInt64 *)&nWritten );
     163             :                         }
     164             :                     }
     165             : 
     166           0 :                     nConverted = sprintf (g_buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
     167           0 :                     if( nConverted )
     168             :                     {
     169             :                         sal_Int64 nWritten;
     170           0 :                         osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64 *)&nWritten );
     171             :                     }
     172             :                 }
     173             :                 else
     174             :                 {
     175             :                     SAL_WARN(
     176             :                         "sal.rtl",
     177             :                         "Couldn't open logfile " << o << '(' << +e << ')');
     178           0 :                 }
     179             :             }
     180          13 :             g_bHasBeenCalled = sal_True;
     181          13 :         }
     182             :     }
     183        3282 : }
     184             : 
     185             : }
     186             : 
     187           0 : extern "C" void SAL_CALL rtl_logfile_trace  ( const char *pszFormat, ... )
     188             : {
     189           0 :     init();
     190           0 :     if( g_buffer )
     191             :     {
     192             :         va_list args;
     193           0 :         va_start(args, pszFormat);
     194             :         {
     195             :             sal_Int64 nConverted, nWritten;
     196           0 :             MutexGuard guard( getLogMutex() );
     197           0 :             nConverted = vsnprintf( g_buffer , g_BUFFERSIZE, pszFormat, args );
     198           0 :             nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
     199           0 :             if( nConverted > 0 )
     200           0 :                 osl_writeFile( g_aFile, g_buffer, nConverted, (sal_uInt64*)&nWritten );
     201             :         }
     202           0 :         va_end(args);
     203             :     }
     204           0 : }
     205             : 
     206        3216 : extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
     207        3216 :     init();
     208        3216 :     if (g_buffer != 0) {
     209           0 :         sal_uInt32 time = osl_getGlobalTimer();
     210           0 :         oslThreadIdentifier threadId = osl_getThreadIdentifier(0);
     211             :         va_list args;
     212           0 :         va_start(args, format);
     213             :         {
     214           0 :             MutexGuard g(getLogMutex());
     215             :             int n1 = snprintf(
     216           0 :                 g_buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
     217           0 :             if (n1 >= 0) {
     218             :                 sal_uInt64 n2;
     219             :                 osl_writeFile(
     220             :                     g_aFile, g_buffer,
     221             :                     static_cast< sal_uInt64 >(
     222           0 :                         std::min(n1, static_cast< int >(g_BUFFERSIZE))),
     223           0 :                     &n2);
     224           0 :                 n1 = vsnprintf(g_buffer, g_BUFFERSIZE, format, args);
     225           0 :                 if (n1 > 0) {
     226             :                     osl_writeFile(
     227             :                         g_aFile, g_buffer,
     228             :                         static_cast< sal_uInt64 >(
     229           0 :                             std::min(n1, static_cast< int >(g_BUFFERSIZE))),
     230           0 :                         &n2);
     231             :                 }
     232           0 :             }
     233             :         }
     234           0 :         va_end(args);
     235             :     }
     236        3216 : }
     237             : 
     238          66 : extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
     239          66 :     init();
     240          66 :     return g_buffer != 0;
     241        3831 : }
     242             : 
     243             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10