LCOV - code coverage report
Current view: top level - extensions/source/logging - logger.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 49 95 51.6 %
Date: 2012-08-25 Functions: 17 32 53.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 132 29.5 %

           Branch data     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 "logrecord.hxx"
      23                 :            : #include "loggerconfig.hxx"
      24                 :            : 
      25                 :            : #include <com/sun/star/logging/XLogger.hpp>
      26                 :            : #include <com/sun/star/logging/LogLevel.hpp>
      27                 :            : #include <com/sun/star/uno/XComponentContext.hpp>
      28                 :            : #include <com/sun/star/lang/XServiceInfo.hpp>
      29                 :            : #include <com/sun/star/logging/XLoggerPool.hpp>
      30                 :            : 
      31                 :            : #include <tools/diagnose_ex.h>
      32                 :            : 
      33                 :            : #include <comphelper/componentcontext.hxx>
      34                 :            : 
      35                 :            : #include <cppuhelper/basemutex.hxx>
      36                 :            : #include <cppuhelper/interfacecontainer.hxx>
      37                 :            : #include <cppuhelper/implbase2.hxx>
      38                 :            : #include <cppuhelper/weakref.hxx>
      39                 :            : 
      40                 :            : #include <boost/bind.hpp>
      41                 :            : 
      42                 :            : #include <map>
      43                 :            : 
      44                 :            : //........................................................................
      45                 :            : namespace logging
      46                 :            : {
      47                 :            : //........................................................................
      48                 :            : 
      49                 :            :     /** === begin UNO using === **/
      50                 :            :     using ::com::sun::star::logging::XLogger;
      51                 :            :     using ::com::sun::star::uno::Reference;
      52                 :            :     using ::com::sun::star::uno::XComponentContext;
      53                 :            :     using ::com::sun::star::lang::XServiceInfo;
      54                 :            :     using ::com::sun::star::uno::RuntimeException;
      55                 :            :     using ::com::sun::star::uno::Sequence;
      56                 :            :     using ::com::sun::star::uno::XInterface;
      57                 :            :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      58                 :            :     using ::com::sun::star::uno::Any;
      59                 :            :     using ::com::sun::star::uno::Exception;
      60                 :            :     using ::com::sun::star::uno::WeakReference;
      61                 :            :     using ::com::sun::star::logging::XLogHandler;
      62                 :            :     using ::com::sun::star::logging::XLoggerPool;
      63                 :            :     using ::com::sun::star::logging::LogRecord;
      64                 :            :     /** === end UNO using === **/
      65                 :            :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      66                 :            : 
      67                 :            :     //====================================================================
      68                 :            :     //= helper
      69                 :            :     //====================================================================
      70                 :            :     namespace
      71                 :            :     {
      72                 :          0 :         sal_Bool lcl_supportsService_nothrow( XServiceInfo& _rSI, const ::rtl::OUString& _rServiceName )
      73                 :            :         {
      74         [ #  # ]:          0 :             const Sequence< ::rtl::OUString > aServiceNames( _rSI.getSupportedServiceNames() );
      75         [ #  # ]:          0 :             for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
      76                 :          0 :                     pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
      77                 :            :                     ++pServiceNames
      78                 :            :                 )
      79         [ #  # ]:          0 :                 if ( _rServiceName == *pServiceNames )
      80                 :          0 :                     return sal_True;
      81         [ #  # ]:          0 :             return sal_False;
      82                 :            :         }
      83                 :            :     }
      84                 :            : 
      85                 :            :     //====================================================================
      86                 :            :     //= EventLogger - declaration
      87                 :            :     //====================================================================
      88                 :            :     typedef ::cppu::WeakImplHelper2 <   XLogger
      89                 :            :                                     ,   XServiceInfo
      90                 :            :                                     >   EventLogger_Base;
      91                 :            :     class EventLogger   :public ::cppu::BaseMutex
      92                 :            :                         ,public EventLogger_Base
      93                 :            :     {
      94                 :            :     private:
      95                 :            :         ::comphelper::ComponentContext      m_aContext;
      96                 :            :         ::cppu::OInterfaceContainerHelper   m_aHandlers;
      97                 :            :         oslInterlockedCount                 m_nEventNumber;
      98                 :            : 
      99                 :            :         // <attributes>
     100                 :            :         sal_Int32       m_nLogLevel;
     101                 :            :         ::rtl::OUString m_sName;
     102                 :            :         // </attributes>
     103                 :            : 
     104                 :            :     public:
     105                 :            :         EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName );
     106                 :            : 
     107                 :            :         // XServiceInfo
     108                 :            :         virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
     109                 :            :         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
     110                 :            :         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
     111                 :            : 
     112                 :            :         // XLogger
     113                 :            :         virtual ::rtl::OUString SAL_CALL getName() throw (RuntimeException);
     114                 :            :         virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
     115                 :            :         virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
     116                 :            :         virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
     117                 :            :         virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
     118                 :            :         virtual ::sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException);
     119                 :            :         virtual void SAL_CALL log( ::sal_Int32 Level, const ::rtl::OUString& Message ) throw (RuntimeException);
     120                 :            :         virtual void SAL_CALL logp( ::sal_Int32 Level, const ::rtl::OUString& SourceClass, const ::rtl::OUString& SourceMethod, const ::rtl::OUString& Message ) throw (RuntimeException);
     121                 :            : 
     122                 :            :     protected:
     123                 :            :         ~EventLogger();
     124                 :            : 
     125                 :            :     private:
     126                 :            :         /** logs the given log record
     127                 :            :         */
     128                 :            :         void    impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
     129                 :            : 
     130                 :            :         /** non-threadsafe impl-version of isLoggable
     131                 :            :         */
     132                 :            :         bool    impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
     133                 :            :     };
     134                 :            : 
     135                 :            :     //====================================================================
     136                 :            :     //= LoggerPool - declaration
     137                 :            :     //====================================================================
     138                 :            :     typedef ::cppu::WeakImplHelper2 <   XLoggerPool
     139                 :            :                                     ,   XServiceInfo
     140                 :            :                                     >   LoggerPool_Base;
     141                 :            :     /** administrates a pool of XLogger instances, where a logger is keyed by its name,
     142                 :            :         and subsequent requests for a logger with the same name return the same instance.
     143                 :            :     */
     144 [ +  - ][ +  - ]:         18 :     class LoggerPool : public LoggerPool_Base
                 [ -  + ]
     145                 :            :     {
     146                 :            :     private:
     147                 :            :         typedef ::std::map< ::rtl::OUString, WeakReference< XLogger > > ImplPool;
     148                 :            : 
     149                 :            :     private:
     150                 :            :         ::osl::Mutex                    m_aMutex;
     151                 :            :         ::comphelper::ComponentContext  m_aContext;
     152                 :            :         ImplPool                        m_aImpl;
     153                 :            : 
     154                 :            :     public:
     155                 :            :         LoggerPool( const Reference< XComponentContext >& _rxContext );
     156                 :            : 
     157                 :            :         // XServiceInfo
     158                 :            :         virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
     159                 :            :         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
     160                 :            :         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
     161                 :            : 
     162                 :            :         // helper for factories
     163                 :            :         static Sequence< ::rtl::OUString > getSupportedServiceNames_static();
     164                 :            :         static ::rtl::OUString  getImplementationName_static();
     165                 :            :         static ::rtl::OUString  getSingletonName_static();
     166                 :            :         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
     167                 :            : 
     168                 :            :         // XLoggerPool
     169                 :            :         virtual Reference< XLogger > SAL_CALL getNamedLogger( const ::rtl::OUString& Name ) throw (RuntimeException);
     170                 :            :         virtual Reference< XLogger > SAL_CALL getDefaultLogger(  ) throw (RuntimeException);
     171                 :            :     };
     172                 :            : 
     173                 :            :     //====================================================================
     174                 :            :     //= EventLogger - implementation
     175                 :            :     //====================================================================
     176                 :            :     //--------------------------------------------------------------------
     177                 :          9 :     EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName )
     178                 :            :         :m_aContext( _rxContext )
     179                 :            :         ,m_aHandlers( m_aMutex )
     180                 :            :         ,m_nEventNumber( 0 )
     181                 :            :         ,m_nLogLevel( LogLevel::OFF )
     182 [ +  - ][ +  - ]:          9 :         ,m_sName( _rName )
                 [ +  - ]
     183                 :            :     {
     184         [ +  - ]:          9 :         osl_incrementInterlockedCount( &m_refCount );
     185                 :            :         {
     186 [ +  - ][ +  - ]:          9 :             initializeLoggerFromConfiguration( m_aContext, this );
     187                 :            :         }
     188         [ +  - ]:          9 :         osl_decrementInterlockedCount( &m_refCount );
     189                 :          9 :     }
     190                 :            : 
     191                 :            :     //--------------------------------------------------------------------
     192 [ #  # ][ #  # ]:          0 :     EventLogger::~EventLogger()
                 [ #  # ]
     193                 :            :     {
     194         [ #  # ]:          0 :     }
     195                 :            : 
     196                 :            :     //--------------------------------------------------------------------
     197                 :        417 :     bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
     198                 :            :     {
     199         [ +  - ]:        417 :         if ( _nLevel < m_nLogLevel )
     200                 :        417 :             return false;
     201                 :            : 
     202         [ #  # ]:          0 :         if ( !m_aHandlers.getLength() )
     203                 :          0 :             return false;
     204                 :            : 
     205                 :        417 :         return true;
     206                 :            :     }
     207                 :            : 
     208                 :            :     //--------------------------------------------------------------------
     209                 :          0 :     void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
     210                 :            :     {
     211         [ #  # ]:          0 :         ::osl::MutexGuard aGuard( m_aMutex );
     212                 :            : 
     213 [ #  # ][ #  # ]:          0 :         if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
     214                 :          0 :             return;
     215                 :            : 
     216                 :            :         m_aHandlers.forEach< XLogHandler >(
     217 [ #  # ][ #  # ]:          0 :             ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
                 [ #  # ]
     218                 :            :         m_aHandlers.forEach< XLogHandler >(
     219 [ #  # ][ #  # ]:          0 :             ::boost::bind( &XLogHandler::flush, _1 ) );
         [ #  # ][ #  # ]
     220                 :            :     }
     221                 :            : 
     222                 :            :     //--------------------------------------------------------------------
     223                 :         18 :     ::rtl::OUString SAL_CALL EventLogger::getName() throw (RuntimeException)
     224                 :            :     {
     225                 :         18 :         return m_sName;
     226                 :            :     }
     227                 :            : 
     228                 :            :     //--------------------------------------------------------------------
     229                 :          0 :     ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException)
     230                 :            :     {
     231         [ #  # ]:          0 :         ::osl::MutexGuard aGuard( m_aMutex );
     232         [ #  # ]:          0 :         return m_nLogLevel;
     233                 :            :     }
     234                 :            : 
     235                 :            :     //--------------------------------------------------------------------
     236                 :          9 :     void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException)
     237                 :            :     {
     238         [ +  - ]:          9 :         ::osl::MutexGuard aGuard( m_aMutex );
     239         [ +  - ]:          9 :         m_nLogLevel = _level;
     240                 :          9 :     }
     241                 :            : 
     242                 :            :     //--------------------------------------------------------------------
     243                 :          9 :     void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
     244                 :            :     {
     245         [ +  - ]:          9 :         if ( _rxLogHandler.is() )
     246                 :          9 :             m_aHandlers.addInterface( _rxLogHandler );
     247                 :          9 :     }
     248                 :            : 
     249                 :            :     //--------------------------------------------------------------------
     250                 :          0 :     void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
     251                 :            :     {
     252         [ #  # ]:          0 :         if ( _rxLogHandler.is() )
     253                 :          0 :             m_aHandlers.removeInterface( _rxLogHandler );
     254                 :          0 :     }
     255                 :            : 
     256                 :            :     //--------------------------------------------------------------------
     257                 :        417 :     ::sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException)
     258                 :            :     {
     259         [ +  - ]:        417 :         ::osl::MutexGuard aGuard( m_aMutex );
     260 [ +  - ][ +  - ]:        417 :         return impl_nts_isLoggable_nothrow( _nLevel );
     261                 :            :     }
     262                 :            : 
     263                 :            :     //--------------------------------------------------------------------
     264                 :          0 :     void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
     265                 :            :     {
     266                 :            :         impl_ts_logEvent_nothrow( createLogRecord(
     267                 :            :             m_sName,
     268                 :            :             _rMessage,
     269                 :            :             _nLevel,
     270                 :            :             osl_incrementInterlockedCount( &m_nEventNumber )
     271         [ #  # ]:          0 :         ) );
     272                 :          0 :     }
     273                 :            : 
     274                 :            :     //--------------------------------------------------------------------
     275                 :          0 :     void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const ::rtl::OUString& _rSourceClass, const ::rtl::OUString& _rSourceMethod, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
     276                 :            :     {
     277                 :            :         impl_ts_logEvent_nothrow( createLogRecord(
     278                 :            :             m_sName,
     279                 :            :             _rSourceClass,
     280                 :            :             _rSourceMethod,
     281                 :            :             _rMessage,
     282                 :            :             _nLevel,
     283                 :            :             osl_incrementInterlockedCount( &m_nEventNumber )
     284         [ #  # ]:          0 :         ) );
     285                 :          0 :     }
     286                 :            : 
     287                 :            :     //--------------------------------------------------------------------
     288                 :          0 :     ::rtl::OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException)
     289                 :            :     {
     290                 :          0 :         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.EventLogger" ) );
     291                 :            :     }
     292                 :            : 
     293                 :            :     //--------------------------------------------------------------------
     294                 :          0 :     ::sal_Bool EventLogger::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
     295                 :            :     {
     296                 :          0 :         return lcl_supportsService_nothrow( *this, _rServiceName );
     297                 :            :     }
     298                 :            : 
     299                 :            :     //--------------------------------------------------------------------
     300                 :          0 :     Sequence< ::rtl::OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException)
     301                 :            :     {
     302                 :          0 :         Sequence< ::rtl::OUString > aServiceNames(1);
     303 [ #  # ][ #  # ]:          0 :         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.Logger" ) );
     304                 :          0 :         return aServiceNames;
     305                 :            :     }
     306                 :            : 
     307                 :            :     //====================================================================
     308                 :            :     //= LoggerPool - implementation
     309                 :            :     //====================================================================
     310                 :            :     //--------------------------------------------------------------------
     311                 :          9 :     LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
     312 [ +  - ][ +  - ]:          9 :         :m_aContext( _rxContext )
                 [ +  - ]
     313                 :            :     {
     314                 :          9 :     }
     315                 :            : 
     316                 :            :     //--------------------------------------------------------------------
     317                 :          0 :     ::rtl::OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException)
     318                 :            :     {
     319                 :          0 :         return getImplementationName_static();
     320                 :            :     }
     321                 :            : 
     322                 :            :     //--------------------------------------------------------------------
     323                 :          0 :     ::sal_Bool SAL_CALL LoggerPool::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
     324                 :            :     {
     325                 :          0 :         return lcl_supportsService_nothrow( *this, _rServiceName );
     326                 :            :     }
     327                 :            : 
     328                 :            :     //--------------------------------------------------------------------
     329                 :          0 :     Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException)
     330                 :            :     {
     331                 :          0 :         return getSupportedServiceNames_static();
     332                 :            :     }
     333                 :            : 
     334                 :            :     //--------------------------------------------------------------------
     335                 :          9 :     ::rtl::OUString SAL_CALL LoggerPool::getImplementationName_static()
     336                 :            :     {
     337                 :          9 :         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.LoggerPool" ) );
     338                 :            :     }
     339                 :            : 
     340                 :            :     //--------------------------------------------------------------------
     341                 :          9 :     Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
     342                 :            :     {
     343                 :          9 :         Sequence< ::rtl::OUString > aServiceNames(1);
     344 [ +  - ][ +  - ]:          9 :         aServiceNames[0] = getSingletonName_static();
     345                 :          9 :         return aServiceNames;
     346                 :            :     }
     347                 :            : 
     348                 :            :     //--------------------------------------------------------------------
     349                 :         18 :     ::rtl::OUString LoggerPool::getSingletonName_static()
     350                 :            :     {
     351                 :         18 :         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.LoggerPool" ) );
     352                 :            :     }
     353                 :            : 
     354                 :            :     //--------------------------------------------------------------------
     355                 :          9 :     Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
     356                 :            :     {
     357         [ +  - ]:          9 :         return *( new LoggerPool( _rxContext ) );
     358                 :            :     }
     359                 :            : 
     360                 :            :     //--------------------------------------------------------------------
     361                 :          9 :     Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const ::rtl::OUString& _rName ) throw (RuntimeException)
     362                 :            :     {
     363         [ +  - ]:          9 :         ::osl::MutexGuard aGuard( m_aMutex );
     364                 :            : 
     365         [ +  - ]:          9 :         WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
     366         [ +  - ]:          9 :         Reference< XLogger > xLogger( (Reference< XLogger >)rLogger );
     367         [ +  - ]:          9 :         if ( !xLogger.is() )
     368                 :            :         {
     369                 :            :             // never requested before, or already dead
     370 [ +  - ][ +  - ]:          9 :             xLogger = new EventLogger( m_aContext.getUNOContext(), _rName );
         [ +  - ][ +  - ]
     371         [ +  - ]:          9 :             rLogger = xLogger;
     372                 :            :         }
     373                 :            : 
     374         [ +  - ]:          9 :         return xLogger;
     375                 :            :     }
     376                 :            : 
     377                 :            :     //--------------------------------------------------------------------
     378                 :          0 :     Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger(  ) throw (RuntimeException)
     379                 :            :     {
     380         [ #  # ]:          0 :         return getNamedLogger( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.logging.DefaultLogger" ) ) );
     381                 :            :     }
     382                 :            : 
     383                 :            :     //--------------------------------------------------------------------
     384                 :         27 :     void createRegistryInfo_LoggerPool()
     385                 :            :     {
     386 [ +  + ][ +  - ]:         27 :         static OSingletonRegistration< LoggerPool > aAutoRegistration;
         [ +  - ][ #  # ]
     387                 :         27 :     }
     388                 :            : 
     389                 :            : //........................................................................
     390 [ +  - ][ +  - ]:         27 : } // namespace logging
     391                 :            : //........................................................................
     392                 :            : 
     393                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10