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

Generated by: LCOV version 1.10