LCOV - code coverage report
Current view: top level - comphelper/source/misc - logging.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 96 0.0 %
Date: 2014-04-14 Functions: 0 17 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 <comphelper/logging.hxx>
      22             : 
      23             : #include <com/sun/star/logging/LoggerPool.hpp>
      24             : #include <com/sun/star/logging/LogLevel.hpp>
      25             : #include <com/sun/star/resource/OfficeResourceLoader.hpp>
      26             : #include <com/sun/star/resource/XResourceBundle.hpp>
      27             : #include <com/sun/star/resource/XResourceBundleLoader.hpp>
      28             : 
      29             : #include <rtl/ustrbuf.hxx>
      30             : 
      31             : 
      32             : namespace comphelper
      33             : {
      34             : 
      35             : 
      36             :     using ::com::sun::star::uno::Reference;
      37             :     using ::com::sun::star::uno::XComponentContext;
      38             :     using ::com::sun::star::logging::XLoggerPool;
      39             :     using ::com::sun::star::logging::LoggerPool;
      40             :     using ::com::sun::star::logging::XLogger;
      41             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      42             :     using ::com::sun::star::uno::Exception;
      43             :     using ::com::sun::star::logging::XLogHandler;
      44             :     using ::com::sun::star::resource::XResourceBundle;
      45             :     using ::com::sun::star::resource::XResourceBundleLoader;
      46             : 
      47             :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      48             : 
      49             : 
      50             :     //= EventLogger_Impl - declaration
      51             : 
      52           0 :     class EventLogger_Impl
      53             :     {
      54             :     private:
      55             :         Reference< XComponentContext >  m_aContext;
      56             :         OUString                 m_sLoggerName;
      57             :         Reference< XLogger >            m_xLogger;
      58             : 
      59             :     public:
      60           0 :         EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
      61             :             :m_aContext( _rxContext )
      62           0 :             ,m_sLoggerName( _rLoggerName )
      63             :         {
      64           0 :             impl_createLogger_nothrow();
      65           0 :         }
      66             : 
      67           0 :         inline bool isValid() const { return m_xLogger.is(); }
      68           0 :         inline const Reference< XLogger >& getLogger() const { return m_xLogger; }
      69           0 :         inline Reference< XComponentContext > getContext() const { return m_aContext; }
      70             : 
      71             :     private:
      72             :         void    impl_createLogger_nothrow();
      73             :     };
      74             : 
      75             : 
      76             :     //= EventLogger_Impl - implementation
      77             : 
      78             : 
      79           0 :     void EventLogger_Impl::impl_createLogger_nothrow()
      80             :     {
      81             :         try
      82             :         {
      83           0 :             Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext ) );
      84           0 :             if ( !m_sLoggerName.isEmpty() )
      85           0 :                 m_xLogger = xPool->getNamedLogger( m_sLoggerName );
      86             :             else
      87           0 :                 m_xLogger = xPool->getDefaultLogger();
      88             :         }
      89           0 :         catch( const Exception& e )
      90             :         {
      91             :             (void)e;
      92             :             OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
      93             :         }
      94           0 :     }
      95             : 
      96             : 
      97             :     //= EventLogger
      98             : 
      99             : 
     100           0 :     EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName )
     101           0 :         :m_pImpl( new EventLogger_Impl( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
     102             :     {
     103           0 :     }
     104             : 
     105             : 
     106           0 :     EventLogger::~EventLogger()
     107             :     {
     108           0 :     }
     109             : 
     110             : 
     111           0 :     bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
     112             :     {
     113           0 :         if ( !m_pImpl->isValid() )
     114           0 :             return false;
     115             : 
     116             :         try
     117             :         {
     118           0 :             return m_pImpl->getLogger()->isLoggable( _nLogLevel );
     119             :         }
     120           0 :         catch( const Exception& e )
     121             :         {
     122             :             (void)e;
     123             :             OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
     124             :         }
     125             : 
     126           0 :         return false;
     127             :     }
     128             : 
     129             : 
     130             :     namespace
     131             :     {
     132           0 :         void    lcl_replaceParameter( OUString& _inout_Message, const OUString& _rPlaceHolder, const OUString& _rReplacement )
     133             :         {
     134           0 :             sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder );
     135             :             OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
     136           0 :             if ( nPlaceholderPosition < 0 )
     137           0 :                 return;
     138             : 
     139           0 :             _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement );
     140             :         }
     141             :     }
     142             : 
     143             : 
     144           0 :     bool EventLogger::impl_log( const sal_Int32 _nLogLevel,
     145             :         const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage,
     146             :         const OptionalString& _rArgument1, const OptionalString& _rArgument2,
     147             :         const OptionalString& _rArgument3, const OptionalString& _rArgument4,
     148             :         const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
     149             :     {
     150             :         // (if OUString had an indexOfAscii, we could save those ugly statics ...)
     151           0 :         static OUString sPH1( "$1$" );
     152           0 :         static OUString sPH2( "$2$" );
     153           0 :         static OUString sPH3( "$3$" );
     154           0 :         static OUString sPH4( "$4$" );
     155           0 :         static OUString sPH5( "$5$" );
     156           0 :         static OUString sPH6( "$6$" );
     157             : 
     158           0 :         OUString sMessage( _rMessage );
     159           0 :         if ( !!_rArgument1 )
     160           0 :             lcl_replaceParameter( sMessage, sPH1, *_rArgument1 );
     161             : 
     162           0 :         if ( !!_rArgument2 )
     163           0 :             lcl_replaceParameter( sMessage, sPH2, *_rArgument2 );
     164             : 
     165           0 :         if ( !!_rArgument3 )
     166           0 :             lcl_replaceParameter( sMessage, sPH3, *_rArgument3 );
     167             : 
     168           0 :         if ( !!_rArgument4 )
     169           0 :             lcl_replaceParameter( sMessage, sPH4, *_rArgument4 );
     170             : 
     171           0 :         if ( !!_rArgument5 )
     172           0 :             lcl_replaceParameter( sMessage, sPH5, *_rArgument5 );
     173             : 
     174           0 :         if ( !!_rArgument6 )
     175           0 :             lcl_replaceParameter( sMessage, sPH6, *_rArgument6 );
     176             : 
     177             :         try
     178             :         {
     179           0 :             Reference< XLogger > xLogger( m_pImpl->getLogger() );
     180             :             OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
     181           0 :             if ( _pSourceClass && _pSourceMethod )
     182             :             {
     183           0 :                 xLogger->logp(
     184             :                     _nLogLevel,
     185             :                     OUString::createFromAscii( _pSourceClass ),
     186             :                     OUString::createFromAscii( _pSourceMethod ),
     187             :                     sMessage
     188           0 :                 );
     189             :             }
     190             :             else
     191             :             {
     192           0 :                 xLogger->log( _nLogLevel, sMessage );
     193           0 :             }
     194             :         }
     195           0 :         catch( const Exception& e )
     196             :         {
     197             :             (void)e;
     198             :             OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
     199             :         }
     200             : 
     201           0 :         return false;
     202             :     }
     203             : 
     204             : 
     205             :     //= ResourceBasedEventLogger_Data
     206             : 
     207           0 :     struct ResourceBasedEventLogger_Data
     208             :     {
     209             :         /// the base name of the resource bundle
     210             :         OUString                 sBundleBaseName;
     211             :         /// did we already attempt to load the bundle?
     212             :         bool                            bBundleLoaded;
     213             :         /// the lazily loaded bundle
     214             :         Reference< XResourceBundle >    xBundle;
     215             : 
     216           0 :         ResourceBasedEventLogger_Data()
     217             :             :sBundleBaseName()
     218             :             ,bBundleLoaded( false )
     219           0 :             ,xBundle()
     220             :         {
     221           0 :         }
     222             :     };
     223             : 
     224             : 
     225           0 :     bool    lcl_loadBundle_nothrow( Reference< XComponentContext > const & _rContext, ResourceBasedEventLogger_Data& _rLoggerData )
     226             :     {
     227           0 :         if ( _rLoggerData.bBundleLoaded )
     228           0 :             return _rLoggerData.xBundle.is();
     229             : 
     230             :         // no matter what happens below, don't attempt creation ever again
     231           0 :         _rLoggerData.bBundleLoaded = true;
     232             : 
     233             :         try
     234             :         {
     235             :             Reference< XResourceBundleLoader > xLoader(
     236             :                 com::sun::star::resource::OfficeResourceLoader::get(
     237           0 :                     _rContext ) );
     238           0 :             _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW );
     239             :         }
     240           0 :         catch( const Exception& e )
     241             :         {
     242             :             (void)e;
     243             :             OSL_FAIL( "lcl_loadBundle_nothrow: caught an exception!" );
     244             :         }
     245             : 
     246           0 :         return _rLoggerData.xBundle.is();
     247             :     }
     248             : 
     249             : 
     250           0 :     OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID )
     251             :     {
     252             :         OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" );
     253           0 :         OUString sMessage;
     254             :         try
     255             :         {
     256           0 :             OUStringBuffer aBuffer;
     257           0 :             aBuffer.appendAscii( "string:" );
     258           0 :             aBuffer.append( _nMessageResID );
     259           0 :             OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage );
     260             :         }
     261           0 :         catch( const Exception& e )
     262             :         {
     263             :             (void)e;
     264             :             OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" );
     265             :         }
     266           0 :         return sMessage;
     267             :     }
     268             : 
     269             : 
     270             :     //= ResourceBasedEventLogger
     271             : 
     272             : 
     273           0 :     ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName,
     274             :         const sal_Char* _pAsciiLoggerName )
     275             :         :EventLogger( _rxContext, _pAsciiLoggerName )
     276           0 :         ,m_pData( new ResourceBasedEventLogger_Data )
     277             :     {
     278           0 :         m_pData->sBundleBaseName = OUString::createFromAscii( _pResourceBundleBaseName );
     279           0 :     }
     280             : 
     281             : 
     282           0 :     OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const
     283             :     {
     284           0 :         OUString sMessage;
     285           0 :         if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) )
     286           0 :             sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID );
     287           0 :         if ( sMessage.isEmpty() )
     288             :         {
     289           0 :             OUStringBuffer aBuffer;
     290           0 :             aBuffer.appendAscii( "<invalid event resource: '" );
     291           0 :             aBuffer.append( m_pData->sBundleBaseName );
     292           0 :             aBuffer.appendAscii( ":" );
     293           0 :             aBuffer.append( _nMessageResID );
     294           0 :             aBuffer.appendAscii( "'>" );
     295           0 :             sMessage = aBuffer.makeStringAndClear();
     296             :         }
     297           0 :         return sMessage;
     298             :     }
     299             : 
     300             : 
     301             : } // namespace comphelper
     302             : 
     303             : 
     304             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10