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

Generated by: LCOV version 1.10