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

Generated by: LCOV version 1.10