LCOV - code coverage report
Current view: top level - libreoffice/extensions/source/logging - logger.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 97 0.0 %
Date: 2012-12-27 Functions: 0 32 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10