LCOV - code coverage report
Current view: top level - sal/rtl - logfile.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 77 0.0 %
Date: 2014-11-03 Functions: 0 6 0.0 %
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             : #include "osl/thread.hxx"
      37             : 
      38             : #include <algorithm>
      39             : 
      40             : #ifdef _MSC_VER
      41             : #define vsnprintf _vsnprintf
      42             : #endif
      43             : 
      44             : using namespace osl;
      45             : using namespace std;
      46             : 
      47             : using ::rtl::OUString;
      48             : using ::rtl::OUStringBuffer;
      49             : 
      50             : namespace {
      51             : 
      52           0 : OUString getFileUrl( const OUString &name )
      53             : {
      54           0 :     OUString aRet;
      55           0 :     if ( osl_getFileURLFromSystemPath( name.pData, &aRet.pData )
      56             :          != osl_File_E_None )
      57             :     {
      58             :         SAL_WARN(
      59             :             "sal.rtl", "osl_getFileURLFromSystemPath failed for \"" << name << '"');
      60             :     }
      61             : 
      62           0 :     OUString aWorkingDirectory;
      63           0 :     osl_getProcessWorkingDir( &(aWorkingDirectory.pData) );
      64           0 :     osl_getAbsoluteFileURL( aWorkingDirectory.pData, aRet.pData, &(aRet.pData) );
      65             : 
      66           0 :     return aRet;
      67             : }
      68             : 
      69             : static const sal_Int32 g_BUFFERSIZE = 4096;
      70             : 
      71             : struct Logger {
      72             :     oslFileHandle aFile;
      73             :     sal_Char *buffer;
      74             :     osl::Mutex mutex;
      75             : 
      76             :     Logger();
      77             : 
      78             :     ~Logger();
      79             : };
      80             : 
      81           0 : Logger::Logger(): aFile(0), buffer(0)
      82             : {
      83           0 :     OUString name( "RTL_LOGFILE" );
      84           0 :     OUString value;
      85           0 :     if( rtl_bootstrap_get( name.pData, &value.pData, 0 ) )
      86             :     {
      87             :         //  Obtain process id.
      88           0 :         oslProcessIdentifier aProcessId = 0;
      89             :         oslProcessInfo info;
      90           0 :         info.Size = sizeof (oslProcessInfo);
      91           0 :         if (osl_getProcessInfo (0, osl_Process_IDENTIFIER, &info) == osl_Process_E_None)
      92           0 :             aProcessId = info.Ident;
      93             : 
      94             :         //  Construct name of log file and open the file.
      95           0 :         OUStringBuffer buf( 128 );
      96           0 :         buf.append( value );
      97             : 
      98             :         // if the filename ends with .nopid, the incoming filename is not modified
      99           0 :         if( value.getLength() < 6 /* ".nopid" */ ||
     100             :             rtl_ustr_ascii_compare_WithLength(
     101           0 :                 value.getStr() + (value.getLength()-6) , 6 , ".nopid" ) )
     102             :         {
     103           0 :             buf.appendAscii( "_" );
     104           0 :             buf.append( (sal_Int32) aProcessId );
     105           0 :             buf.appendAscii( ".log" );
     106             :         }
     107             : 
     108           0 :         OUString o = getFileUrl( buf.makeStringAndClear() );
     109             :         oslFileError e = osl_openFile(
     110           0 :             o.pData, &aFile, osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
     111             : 
     112           0 :         if( osl_File_E_None == e )
     113             :         {
     114             :             TimeValue aCurrentTime;
     115           0 :             buffer = ( sal_Char * ) rtl_allocateMemory( g_BUFFERSIZE );
     116           0 :             sal_Int64 nConverted = 0;
     117           0 :             if (osl_getSystemTime (&aCurrentTime))
     118             :             {
     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           0 :                     aCurrentTime.Seconds + 1e-9 * aCurrentTime.Nanosec,
     124           0 :                     osl_getGlobalTimer());
     125             : 
     126           0 :                 if( nConverted > 0 )
     127             :                 {
     128             :                     sal_Int64 nWritten;
     129           0 :                     osl_writeFile( aFile, buffer, nConverted , (sal_uInt64 *)&nWritten );
     130             :                 }
     131             :             }
     132             : 
     133           0 :             nConverted = sprintf (buffer, "Process id is %" SAL_PRIuUINT32 "\n", aProcessId);
     134           0 :             if( nConverted )
     135             :             {
     136             :                 sal_Int64 nWritten;
     137           0 :                 osl_writeFile( aFile, buffer, nConverted, (sal_uInt64 *)&nWritten );
     138             :             }
     139             :         }
     140             :         else
     141             :         {
     142             :             SAL_WARN(
     143             :                 "sal.rtl",
     144             :                 "Couldn't open logfile " << o << '(' << +e << ')');
     145           0 :         }
     146           0 :     }
     147           0 : }
     148             : 
     149           0 : Logger::~Logger()
     150             : {
     151           0 :     if( buffer )
     152             :     {
     153             :         sal_Int64 nWritten, nConverted =
     154           0 :             sprintf( buffer, "closing log file at %06" SAL_PRIuUINT32, osl_getGlobalTimer() );
     155           0 :         if( nConverted > 0 )
     156           0 :             osl_writeFile( aFile, buffer, nConverted, (sal_uInt64 *)&nWritten );
     157           0 :         osl_closeFile( aFile );
     158           0 :         rtl_freeMemory( buffer );
     159             :     }
     160           0 : }
     161             : 
     162             : struct theLogger: public rtl::Static<Logger, theLogger> {};
     163             : 
     164             : }
     165             : 
     166           0 : extern "C" void SAL_CALL rtl_logfile_trace  ( const char *pszFormat, ... )
     167             : {
     168           0 :     Logger & logger = theLogger::get();
     169           0 :     if( logger.buffer )
     170             :     {
     171             :         va_list args;
     172           0 :         va_start(args, pszFormat);
     173             :         {
     174             :             sal_Int64 nConverted, nWritten;
     175           0 :             MutexGuard guard( logger.mutex );
     176           0 :             nConverted = vsnprintf( logger.buffer , g_BUFFERSIZE, pszFormat, args );
     177           0 :             nConverted = (nConverted > g_BUFFERSIZE ? g_BUFFERSIZE : nConverted );
     178           0 :             if( nConverted > 0 )
     179           0 :                 osl_writeFile( logger.aFile, logger.buffer, nConverted, (sal_uInt64*)&nWritten );
     180             :         }
     181           0 :         va_end(args);
     182             :     }
     183           0 : }
     184             : 
     185           0 : extern "C" void SAL_CALL rtl_logfile_longTrace(char const * format, ...) {
     186           0 :     Logger & logger = theLogger::get();
     187           0 :     if (logger.buffer != 0) {
     188           0 :         sal_uInt32 time = osl_getGlobalTimer();
     189           0 :         oslThreadIdentifier threadId = osl::Thread::getCurrentIdentifier();
     190             :         va_list args;
     191           0 :         va_start(args, format);
     192             :         {
     193           0 :             MutexGuard g(logger.mutex);
     194             :             int n1 = snprintf(
     195           0 :                 logger.buffer, g_BUFFERSIZE, "%06" SAL_PRIuUINT32 " %" SAL_PRIuUINT32 " ", time, threadId);
     196           0 :             if (n1 >= 0) {
     197             :                 sal_uInt64 n2;
     198             :                 osl_writeFile(
     199             :                     logger.aFile, logger.buffer,
     200             :                     static_cast< sal_uInt64 >(
     201           0 :                         std::min(n1, static_cast< int >(g_BUFFERSIZE))),
     202           0 :                     &n2);
     203           0 :                 n1 = vsnprintf(logger.buffer, g_BUFFERSIZE, format, args);
     204           0 :                 if (n1 > 0) {
     205             :                     osl_writeFile(
     206             :                         logger.aFile, logger.buffer,
     207             :                         static_cast< sal_uInt64 >(
     208           0 :                             std::min(n1, static_cast< int >(g_BUFFERSIZE))),
     209           0 :                         &n2);
     210             :                 }
     211           0 :             }
     212             :         }
     213           0 :         va_end(args);
     214             :     }
     215           0 : }
     216             : 
     217           0 : extern "C" sal_Bool SAL_CALL rtl_logfile_hasLogFile( void ) {
     218           0 :     return theLogger::get().buffer != 0;
     219             : }
     220             : 
     221             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10