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

Generated by: LCOV version 1.11