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

Generated by: LCOV version 1.10