LCOV - code coverage report
Current view: top level - extensions/source/logging - logger.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 50 90 55.6 %
Date: 2015-06-13 12:38:46 Functions: 19 31 61.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "log_module.hxx"
      22             : #include "log_services.hxx"
      23             : #include "logrecord.hxx"
      24             : #include "loggerconfig.hxx"
      25             : 
      26             : #include <com/sun/star/logging/XLogger.hpp>
      27             : #include <com/sun/star/logging/LogLevel.hpp>
      28             : #include <com/sun/star/uno/XComponentContext.hpp>
      29             : #include <com/sun/star/lang/XServiceInfo.hpp>
      30             : #include <com/sun/star/logging/XLoggerPool.hpp>
      31             : 
      32             : #include <boost/bind.hpp>
      33             : #include <cppuhelper/basemutex.hxx>
      34             : #include <cppuhelper/interfacecontainer.hxx>
      35             : #include <cppuhelper/implbase2.hxx>
      36             : #include <cppuhelper/supportsservice.hxx>
      37             : #include <cppuhelper/weakref.hxx>
      38             : #include <map>
      39             : 
      40             : 
      41             : namespace logging
      42             : {
      43             : 
      44             :     using ::com::sun::star::logging::XLogger;
      45             :     using ::com::sun::star::uno::Reference;
      46             :     using ::com::sun::star::uno::XComponentContext;
      47             :     using ::com::sun::star::lang::XServiceInfo;
      48             :     using ::com::sun::star::uno::RuntimeException;
      49             :     using ::com::sun::star::uno::Sequence;
      50             :     using ::com::sun::star::uno::XInterface;
      51             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      52             :     using ::com::sun::star::uno::Any;
      53             :     using ::com::sun::star::uno::Exception;
      54             :     using ::com::sun::star::uno::WeakReference;
      55             :     using ::com::sun::star::logging::XLogHandler;
      56             :     using ::com::sun::star::logging::XLoggerPool;
      57             :     using ::com::sun::star::logging::LogRecord;
      58             : 
      59             :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      60             : 
      61             :     typedef ::cppu::WeakImplHelper2 <   XLogger
      62             :                                     ,   XServiceInfo
      63             :                                     >   EventLogger_Base;
      64             :     class EventLogger   :public ::cppu::BaseMutex
      65             :                         ,public EventLogger_Base
      66             :     {
      67             :     private:
      68             :         ::cppu::OInterfaceContainerHelper   m_aHandlers;
      69             :         oslInterlockedCount                 m_nEventNumber;
      70             : 
      71             :         // <attributes>
      72             :         sal_Int32       m_nLogLevel;
      73             :         OUString m_sName;
      74             :         // </attributes>
      75             : 
      76             :     public:
      77             :         EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName );
      78             : 
      79             :         // XServiceInfo
      80             :         virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
      81             :         virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
      82             :         virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
      83             : 
      84             :         // XLogger
      85             :         virtual OUString SAL_CALL getName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
      86             :         virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException, std::exception) SAL_OVERRIDE;
      87             :         virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      88             :         virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      89             :         virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      90             :         virtual sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      91             :         virtual void SAL_CALL log( ::sal_Int32 Level, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      92             :         virtual void SAL_CALL logp( ::sal_Int32 Level, const OUString& SourceClass, const OUString& SourceMethod, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      93             : 
      94             :     protected:
      95             :         virtual ~EventLogger();
      96             : 
      97             :     private:
      98             :         /** logs the given log record
      99             :         */
     100             :         void    impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
     101             : 
     102             :         /** non-threadsafe impl-version of isLoggable
     103             :         */
     104             :         bool    impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
     105             :     };
     106             : 
     107             :     typedef ::cppu::WeakImplHelper2 <   XLoggerPool
     108             :                                     ,   XServiceInfo
     109             :                                     >   LoggerPool_Base;
     110             :     /** administrates a pool of XLogger instances, where a logger is keyed by its name,
     111             :         and subsequent requests for a logger with the same name return the same instance.
     112             :     */
     113          24 :     class LoggerPool : public LoggerPool_Base
     114             :     {
     115             :     private:
     116             :         typedef ::std::map< OUString, WeakReference< XLogger > > ImplPool;
     117             : 
     118             :     private:
     119             :         ::osl::Mutex                    m_aMutex;
     120             :         Reference<XComponentContext>    m_xContext;
     121             :         ImplPool                        m_aImpl;
     122             : 
     123             :     public:
     124             :         LoggerPool( const Reference< XComponentContext >& _rxContext );
     125             : 
     126             :         // XServiceInfo
     127             :         virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
     128             :         virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     129             :         virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
     130             : 
     131             :         // helper for factories
     132             :         static Sequence< OUString > getSupportedServiceNames_static();
     133             :         static OUString  getImplementationName_static();
     134             :         static OUString  getSingletonName_static();
     135             :         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
     136             : 
     137             :         // XLoggerPool
     138             :         virtual Reference< XLogger > SAL_CALL getNamedLogger( const OUString& Name ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     139             :         virtual Reference< XLogger > SAL_CALL getDefaultLogger(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     140             :     };
     141             : 
     142          16 :     EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName )
     143             :         :m_aHandlers( m_aMutex )
     144             :         ,m_nEventNumber( 0 )
     145             :         ,m_nLogLevel( LogLevel::OFF )
     146          16 :         ,m_sName( _rName )
     147             :     {
     148          16 :         osl_atomic_increment( &m_refCount );
     149             :         {
     150          16 :             initializeLoggerFromConfiguration( _rxContext, this );
     151             :         }
     152          16 :         osl_atomic_decrement( &m_refCount );
     153          16 :     }
     154             : 
     155           2 :     EventLogger::~EventLogger()
     156             :     {
     157           2 :     }
     158             : 
     159       44267 :     bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
     160             :     {
     161       44267 :         if ( _nLevel < m_nLogLevel )
     162       44267 :             return false;
     163             : 
     164           0 :         if ( !m_aHandlers.getLength() )
     165           0 :             return false;
     166             : 
     167           0 :         return true;
     168             :     }
     169             : 
     170           0 :     void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
     171             :     {
     172           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     173             : 
     174           0 :         if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
     175           0 :             return;
     176             : 
     177             :         m_aHandlers.forEach< XLogHandler >(
     178           0 :             ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
     179             :         m_aHandlers.forEach< XLogHandler >(
     180           0 :             ::boost::bind( &XLogHandler::flush, _1 ) );
     181             :     }
     182             : 
     183          32 :     OUString SAL_CALL EventLogger::getName() throw (RuntimeException, std::exception)
     184             :     {
     185          32 :         return m_sName;
     186             :     }
     187             : 
     188           0 :     ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException, std::exception)
     189             :     {
     190           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     191           0 :         return m_nLogLevel;
     192             :     }
     193             : 
     194          16 :     void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception)
     195             :     {
     196          16 :         ::osl::MutexGuard aGuard( m_aMutex );
     197          16 :         m_nLogLevel = _level;
     198          16 :     }
     199             : 
     200          16 :     void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
     201             :     {
     202          16 :         if ( _rxLogHandler.is() )
     203          16 :             m_aHandlers.addInterface( _rxLogHandler );
     204          16 :     }
     205             : 
     206           0 :     void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
     207             :     {
     208           0 :         if ( _rxLogHandler.is() )
     209           0 :             m_aHandlers.removeInterface( _rxLogHandler );
     210           0 :     }
     211             : 
     212       44267 :     sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception)
     213             :     {
     214       44267 :         ::osl::MutexGuard aGuard( m_aMutex );
     215       44267 :         return impl_nts_isLoggable_nothrow( _nLevel );
     216             :     }
     217             : 
     218           0 :     void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const OUString& _rMessage ) throw (RuntimeException, std::exception)
     219             :     {
     220             :         impl_ts_logEvent_nothrow( createLogRecord(
     221             :             m_sName,
     222             :             _rMessage,
     223             :             _nLevel,
     224           0 :             osl_atomic_increment( &m_nEventNumber )
     225           0 :         ) );
     226           0 :     }
     227             : 
     228           0 :     void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const OUString& _rSourceClass, const OUString& _rSourceMethod, const OUString& _rMessage ) throw (RuntimeException, std::exception)
     229             :     {
     230             :         impl_ts_logEvent_nothrow( createLogRecord(
     231             :             m_sName,
     232             :             _rSourceClass,
     233             :             _rSourceMethod,
     234             :             _rMessage,
     235             :             _nLevel,
     236           0 :             osl_atomic_increment( &m_nEventNumber )
     237           0 :         ) );
     238           0 :     }
     239             : 
     240           0 :     OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException, std::exception)
     241             :     {
     242           0 :         return OUString( "com.sun.star.comp.extensions.EventLogger" );
     243             :     }
     244             : 
     245           0 :     sal_Bool EventLogger::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
     246             :     {
     247           0 :         return cppu::supportsService(this, _rServiceName);
     248             :     }
     249             : 
     250           0 :     Sequence< OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException, std::exception)
     251             :     {
     252           0 :         Sequence< OUString > aServiceNames(1);
     253           0 :         aServiceNames[0] = "com.sun.star.logging.Logger";
     254           0 :         return aServiceNames;
     255             :     }
     256             : 
     257          13 :     LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
     258          13 :         :m_xContext( _rxContext )
     259             :     {
     260          13 :     }
     261             : 
     262           0 :     OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException, std::exception)
     263             :     {
     264           0 :         return getImplementationName_static();
     265             :     }
     266             : 
     267           0 :     sal_Bool SAL_CALL LoggerPool::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
     268             :     {
     269           0 :         return cppu::supportsService(this, _rServiceName);
     270             :     }
     271             : 
     272           0 :     Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException, std::exception)
     273             :     {
     274           0 :         return getSupportedServiceNames_static();
     275             :     }
     276             : 
     277          13 :     OUString SAL_CALL LoggerPool::getImplementationName_static()
     278             :     {
     279          13 :         return OUString( "com.sun.star.comp.extensions.LoggerPool" );
     280             :     }
     281             : 
     282          13 :     Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
     283             :     {
     284          13 :         Sequence< OUString > aServiceNames(1);
     285          13 :         aServiceNames[0] = getSingletonName_static();
     286          13 :         return aServiceNames;
     287             :     }
     288             : 
     289          26 :     OUString LoggerPool::getSingletonName_static()
     290             :     {
     291          26 :         return OUString( "com.sun.star.logging.LoggerPool" );
     292             :     }
     293             : 
     294          13 :     Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
     295             :     {
     296          13 :         return *( new LoggerPool( _rxContext ) );
     297             :     }
     298             : 
     299          16 :     Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const OUString& _rName ) throw (RuntimeException, std::exception)
     300             :     {
     301          16 :         ::osl::MutexGuard aGuard( m_aMutex );
     302             : 
     303          16 :         WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
     304          16 :         Reference< XLogger > xLogger( rLogger );
     305          16 :         if ( !xLogger.is() )
     306             :         {
     307             :             // never requested before, or already dead
     308          16 :             xLogger = new EventLogger( m_xContext, _rName );
     309          16 :             rLogger = xLogger;
     310             :         }
     311             : 
     312          16 :         return xLogger;
     313             :     }
     314             : 
     315           0 :     Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger(  ) throw (RuntimeException, std::exception)
     316             :     {
     317           0 :         return getNamedLogger( OUString( "org.openoffice.logging.DefaultLogger" ) );
     318             :     }
     319             : 
     320          40 :     void createRegistryInfo_LoggerPool()
     321             :     {
     322          40 :         static OSingletonRegistration< LoggerPool > aAutoRegistration;
     323          40 :     }
     324             : 
     325          39 : } // namespace logging
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11