LCOV - code coverage report
Current view: top level - extensions/source/logging - filehandler.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 120 0.0 %
Date: 2014-04-14 Functions: 0 25 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             : 
      21             : #include "log_module.hxx"
      22             : #include "methodguard.hxx"
      23             : #include "loghandler.hxx"
      24             : 
      25             : #include <com/sun/star/logging/XLogHandler.hpp>
      26             : #include <com/sun/star/lang/XServiceInfo.hpp>
      27             : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
      28             : #include <com/sun/star/lang/XInitialization.hpp>
      29             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      30             : #include <com/sun/star/util/PathSubstitution.hpp>
      31             : #include <com/sun/star/util/XStringSubstitution.hpp>
      32             : 
      33             : #include <tools/diagnose_ex.h>
      34             : 
      35             : #include <cppuhelper/compbase3.hxx>
      36             : #include <cppuhelper/basemutex.hxx>
      37             : #include <cppuhelper/supportsservice.hxx>
      38             : 
      39             : #include <osl/thread.h>
      40             : #include <osl/file.hxx>
      41             : 
      42             : #include <rtl/strbuf.hxx>
      43             : 
      44             : #include <memory>
      45             : 
      46             : 
      47             : namespace logging
      48             : {
      49             : 
      50             : 
      51             :     using ::com::sun::star::uno::Reference;
      52             :     using ::com::sun::star::logging::LogRecord;
      53             :     using ::com::sun::star::uno::RuntimeException;
      54             :     using ::com::sun::star::logging::XLogFormatter;
      55             :     using ::com::sun::star::uno::Sequence;
      56             :     using ::com::sun::star::uno::XInterface;
      57             :     using ::com::sun::star::uno::XComponentContext;
      58             :     using ::com::sun::star::logging::XLogHandler;
      59             :     using ::com::sun::star::lang::XServiceInfo;
      60             :     using ::com::sun::star::ucb::AlreadyInitializedException;
      61             :     using ::com::sun::star::lang::XInitialization;
      62             :     using ::com::sun::star::uno::Any;
      63             :     using ::com::sun::star::uno::Exception;
      64             :     using ::com::sun::star::lang::IllegalArgumentException;
      65             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      66             :     using ::com::sun::star::util::PathSubstitution;
      67             :     using ::com::sun::star::util::XStringSubstitution;
      68             :     using ::com::sun::star::beans::NamedValue;
      69             : 
      70             : 
      71             :     //= FileHandler - declaration
      72             : 
      73             :     typedef ::cppu::WeakComponentImplHelper3    <   XLogHandler
      74             :                                                 ,   XServiceInfo
      75             :                                                 ,   XInitialization
      76             :                                                 >   FileHandler_Base;
      77             :     class FileHandler   :public ::cppu::BaseMutex
      78             :                         ,public FileHandler_Base
      79             :     {
      80             :     private:
      81             :         enum FileValidity
      82             :         {
      83             :             /// never attempted to open the file
      84             :             eUnknown,
      85             :             /// file is valid
      86             :             eValid,
      87             :             /// file is invalid
      88             :             eInvalid
      89             :         };
      90             : 
      91             :     private:
      92             :         Reference<XComponentContext>    m_xContext;
      93             :         LogHandlerHelper                m_aHandlerHelper;
      94             :         OUString                 m_sFileURL;
      95             :         ::std::auto_ptr< ::osl::File >  m_pFile;
      96             :         FileValidity                    m_eFileValidity;
      97             : 
      98             :     protected:
      99             :         FileHandler( const Reference< XComponentContext >& _rxContext );
     100             :         virtual ~FileHandler();
     101             : 
     102             :         // XLogHandler
     103             :         virtual OUString SAL_CALL getEncoding() throw (RuntimeException, std::exception) SAL_OVERRIDE;
     104             :         virtual void SAL_CALL setEncoding( const OUString& _encoding ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     105             :         virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException, std::exception) SAL_OVERRIDE;
     106             :         virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     107             :         virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException, std::exception) SAL_OVERRIDE;
     108             :         virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     109             :         virtual void SAL_CALL flush(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     110             :         virtual sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     111             : 
     112             :         // XInitialization
     113             :         virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     114             : 
     115             :         // XServiceInfo
     116             :         virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
     117             :         virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     118             :         virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
     119             : 
     120             :         // OComponentHelper
     121             :         virtual void SAL_CALL disposing() SAL_OVERRIDE;
     122             : 
     123             :     public:
     124             :         // XServiceInfo - static version
     125             :         static OUString SAL_CALL getImplementationName_static();
     126             :         static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
     127             :         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
     128             : 
     129             :     public:
     130             :         typedef ComponentMethodGuard< FileHandler > MethodGuard;
     131             :         void    enterMethod( MethodGuard::Access );
     132             :         void    leaveMethod( MethodGuard::Access );
     133             : 
     134             :     private:
     135             :         /** prepares our output file for writing
     136             :         */
     137             :         bool    impl_prepareFile_nothrow();
     138             : 
     139             :         /// writes the given string to our file
     140             :         void    impl_writeString_nothrow( const OString& _rEntry );
     141             : 
     142             :         /** does string substitution on a (usually externally provided) file url
     143             :         */
     144             :         void    impl_doStringsubstitution_nothrow( OUString& _inout_rURL );
     145             :     };
     146             : 
     147             : 
     148             :     //= FileHandler - implementation
     149             : 
     150             : 
     151           0 :     FileHandler::FileHandler( const Reference< XComponentContext >& _rxContext )
     152             :         :FileHandler_Base( m_aMutex )
     153             :         ,m_xContext( _rxContext )
     154             :         ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
     155             :         ,m_sFileURL( )
     156             :         ,m_pFile( )
     157           0 :         ,m_eFileValidity( eUnknown )
     158             :     {
     159           0 :     }
     160             : 
     161             : 
     162           0 :     FileHandler::~FileHandler()
     163             :     {
     164           0 :         if ( !rBHelper.bDisposed )
     165             :         {
     166           0 :             acquire();
     167           0 :             dispose();
     168             :         }
     169           0 :     }
     170             : 
     171             : 
     172           0 :     bool FileHandler::impl_prepareFile_nothrow()
     173             :     {
     174           0 :         if ( m_eFileValidity == eUnknown )
     175             :         {
     176           0 :             m_pFile.reset( new ::osl::File( m_sFileURL ) );
     177             :             // check whether the log file already exists
     178           0 :             ::osl::DirectoryItem aFileItem;
     179           0 :             ::osl::DirectoryItem::get( m_sFileURL, aFileItem );
     180           0 :             ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
     181           0 :             if ( ::osl::FileBase::E_None == aFileItem.getFileStatus( aStatus ) )
     182           0 :                 ::osl::File::remove( m_sFileURL );
     183             : 
     184           0 :             ::osl::FileBase::RC res = m_pFile->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
     185             :             m_eFileValidity =   res == ::osl::FileBase::E_None
     186             :                             ?   eValid
     187           0 :                             :   eInvalid;
     188             :         #if OSL_DEBUG_LEVEL > 0
     189             :             if ( m_eFileValidity == eInvalid )
     190             :             {
     191             :                 OStringBuffer sMessage;
     192             :                 sMessage.append( "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:" );
     193             :                 sMessage.append( "\nURL: " );
     194             :                 sMessage.append( OString( m_sFileURL.getStr(), m_sFileURL.getLength(), osl_getThreadTextEncoding() ) );
     195             :                 sMessage.append( "\nerror code: " );
     196             :                 sMessage.append( (sal_Int32)res );
     197             :                 OSL_FAIL( sMessage.makeStringAndClear().getStr() );
     198             :             }
     199             :         #endif
     200           0 :             if ( m_eFileValidity == eValid )
     201             :             {
     202           0 :                 OString sHead;
     203           0 :                 if ( m_aHandlerHelper.getEncodedHead( sHead ) )
     204           0 :                     impl_writeString_nothrow( sHead );
     205           0 :             }
     206             :         }
     207             : 
     208           0 :         return m_eFileValidity == eValid;
     209             :     }
     210             : 
     211             : 
     212           0 :     void FileHandler::impl_writeString_nothrow( const OString& _rEntry )
     213             :     {
     214             :         OSL_PRECOND( m_pFile.get(), "FileHandler::impl_writeString_nothrow: no file!" );
     215             : 
     216           0 :         sal_uInt64 nBytesToWrite( _rEntry.getLength() );
     217           0 :         sal_uInt64 nBytesWritten( 0 );
     218             :     #if OSL_DEBUG_LEVEL > 0
     219             :         ::osl::FileBase::RC res =
     220             :     #endif
     221           0 :         m_pFile->write( _rEntry.getStr(), nBytesToWrite, nBytesWritten );
     222             :         OSL_ENSURE( ( res == ::osl::FileBase::E_None ) && ( nBytesWritten == nBytesToWrite ),
     223             :             "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
     224           0 :     }
     225             : 
     226             : 
     227           0 :     void FileHandler::impl_doStringsubstitution_nothrow( OUString& _inout_rURL )
     228             :     {
     229             :         try
     230             :         {
     231           0 :             Reference< XStringSubstitution > xStringSubst(PathSubstitution::create(m_xContext));
     232           0 :             _inout_rURL = xStringSubst->substituteVariables( _inout_rURL, true );
     233             :         }
     234           0 :         catch( const Exception& )
     235             :         {
     236             :             DBG_UNHANDLED_EXCEPTION();
     237             :         }
     238           0 :     }
     239             : 
     240             : 
     241           0 :     void SAL_CALL FileHandler::disposing()
     242             :     {
     243           0 :         if ( m_eFileValidity == eValid )
     244             :         {
     245           0 :             OString sTail;
     246           0 :             if ( m_aHandlerHelper.getEncodedTail( sTail ) )
     247           0 :                 impl_writeString_nothrow( sTail );
     248             :         }
     249             : 
     250           0 :         m_pFile.reset();
     251           0 :         m_aHandlerHelper.setFormatter( NULL );
     252           0 :     }
     253             : 
     254             : 
     255           0 :     void FileHandler::enterMethod( MethodGuard::Access )
     256             :     {
     257           0 :         m_aHandlerHelper.enterMethod();
     258           0 :     }
     259             : 
     260             : 
     261           0 :     void FileHandler::leaveMethod( MethodGuard::Access )
     262             :     {
     263           0 :         m_aMutex.release();
     264           0 :     }
     265             : 
     266             : 
     267           0 :     OUString SAL_CALL FileHandler::getEncoding() throw (RuntimeException, std::exception)
     268             :     {
     269           0 :         MethodGuard aGuard( *this );
     270           0 :         OUString sEncoding;
     271           0 :         OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
     272           0 :         return sEncoding;
     273             :     }
     274             : 
     275             : 
     276           0 :     void SAL_CALL FileHandler::setEncoding( const OUString& _rEncoding ) throw (RuntimeException, std::exception)
     277             :     {
     278           0 :         MethodGuard aGuard( *this );
     279           0 :         OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
     280           0 :     }
     281             : 
     282             : 
     283           0 :     Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter() throw (RuntimeException, std::exception)
     284             :     {
     285           0 :         MethodGuard aGuard( *this );
     286           0 :         return m_aHandlerHelper.getFormatter();
     287             :     }
     288             : 
     289             : 
     290           0 :     void SAL_CALL FileHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException, std::exception)
     291             :     {
     292           0 :         MethodGuard aGuard( *this );
     293           0 :         m_aHandlerHelper.setFormatter( _rxFormatter );
     294           0 :     }
     295             : 
     296             : 
     297           0 :     ::sal_Int32 SAL_CALL FileHandler::getLevel() throw (RuntimeException, std::exception)
     298             :     {
     299           0 :         MethodGuard aGuard( *this );
     300           0 :         return m_aHandlerHelper.getLevel();
     301             :     }
     302             : 
     303             : 
     304           0 :     void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception)
     305             :     {
     306           0 :         MethodGuard aGuard( *this );
     307           0 :         m_aHandlerHelper.setLevel( _nLevel );
     308           0 :     }
     309             : 
     310             : 
     311           0 :     void SAL_CALL FileHandler::flush(  ) throw (RuntimeException, std::exception)
     312             :     {
     313           0 :         MethodGuard aGuard( *this );
     314           0 :         if(!m_pFile.get())
     315             :         {
     316             :             OSL_PRECOND(false, "FileHandler::flush: no file!");
     317           0 :             return;
     318             :         }
     319             :         #if OSL_DEBUG_LEVEL > 0
     320             :             ::osl::FileBase::RC res =
     321             :         #endif
     322           0 :                 m_pFile->sync();
     323           0 :         OSL_ENSURE(res == ::osl::FileBase::E_None, "FileHandler::flush: Could not sync logfile to filesystem.");
     324             :     }
     325             : 
     326             : 
     327           0 :     sal_Bool SAL_CALL FileHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException, std::exception)
     328             :     {
     329           0 :         MethodGuard aGuard( *this );
     330             : 
     331           0 :         if ( !impl_prepareFile_nothrow() )
     332           0 :             return sal_False;
     333             : 
     334           0 :         OString sEntry;
     335           0 :         if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
     336           0 :             return sal_False;
     337             : 
     338           0 :         impl_writeString_nothrow( sEntry );
     339           0 :         return sal_True;
     340             :     }
     341             : 
     342             : 
     343           0 :     void SAL_CALL FileHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
     344             :     {
     345           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     346             : 
     347           0 :         if ( m_aHandlerHelper.getIsInitialized() )
     348           0 :             throw AlreadyInitializedException();
     349             : 
     350           0 :         if ( _rArguments.getLength() != 1 )
     351           0 :             throw IllegalArgumentException( OUString(), *this, 1 );
     352             : 
     353           0 :         Sequence< NamedValue > aSettings;
     354           0 :         if ( _rArguments[0] >>= m_sFileURL )
     355             :         {
     356             :             // create( [in] string URL );
     357           0 :             impl_doStringsubstitution_nothrow( m_sFileURL );
     358             :         }
     359           0 :         else if ( _rArguments[0] >>= aSettings )
     360             :         {
     361             :             // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
     362           0 :             ::comphelper::NamedValueCollection aTypedSettings( aSettings );
     363           0 :             m_aHandlerHelper.initFromSettings( aTypedSettings );
     364             : 
     365           0 :             if ( aTypedSettings.get_ensureType( "FileURL", m_sFileURL ) )
     366           0 :                 impl_doStringsubstitution_nothrow( m_sFileURL );
     367             :         }
     368             :         else
     369           0 :             throw IllegalArgumentException( OUString(), *this, 1 );
     370             : 
     371           0 :         m_aHandlerHelper.setIsInitialized();
     372           0 :     }
     373             : 
     374             : 
     375           0 :     OUString SAL_CALL FileHandler::getImplementationName() throw(RuntimeException, std::exception)
     376             :     {
     377           0 :         return getImplementationName_static();
     378             :     }
     379             : 
     380           0 :     sal_Bool SAL_CALL FileHandler::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
     381             :     {
     382           0 :         return cppu::supportsService(this, _rServiceName);
     383             :     }
     384             : 
     385             : 
     386           0 :     Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames() throw(RuntimeException, std::exception)
     387             :     {
     388           0 :         return getSupportedServiceNames_static();
     389             :     }
     390             : 
     391             : 
     392           0 :     OUString SAL_CALL FileHandler::getImplementationName_static()
     393             :     {
     394           0 :         return OUString( "com.sun.star.comp.extensions.FileHandler" );
     395             :     }
     396             : 
     397             : 
     398           0 :     Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames_static()
     399             :     {
     400           0 :         Sequence< OUString > aServiceNames(1);
     401           0 :         aServiceNames[0] = "com.sun.star.logging.FileHandler";
     402           0 :         return aServiceNames;
     403             :     }
     404             : 
     405             : 
     406           0 :     Reference< XInterface > FileHandler::Create( const Reference< XComponentContext >& _rxContext )
     407             :     {
     408           0 :         return *( new FileHandler( _rxContext ) );
     409             :     }
     410             : 
     411             : 
     412           0 :     void createRegistryInfo_FileHandler()
     413             :     {
     414           0 :         static OAutoRegistration< FileHandler > aAutoRegistration;
     415           0 :     }
     416             : 
     417             : 
     418             : } // namespace logging
     419             : 
     420             : 
     421             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10