LCOV - code coverage report
Current view: top level - comphelper/source/misc - logging.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 18 96 18.8 %
Date: 2012-08-25 Functions: 6 17 35.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 196 7.1 %

           Branch data     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                 :            : #include <comphelper/componentcontext.hxx>
      23                 :            : 
      24                 :            : #include <com/sun/star/logging/LoggerPool.hpp>
      25                 :            : #include <com/sun/star/logging/LogLevel.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                 :            :     /** === begin UNO using === **/
      37                 :            :     using ::com::sun::star::uno::Reference;
      38                 :            :     using ::com::sun::star::uno::XComponentContext;
      39                 :            :     using ::com::sun::star::logging::XLoggerPool;
      40                 :            :     using ::com::sun::star::logging::LoggerPool;
      41                 :            :     using ::com::sun::star::logging::XLogger;
      42                 :            :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      43                 :            :     using ::com::sun::star::uno::Exception;
      44                 :            :     using ::com::sun::star::logging::XLogHandler;
      45                 :            :     using ::com::sun::star::resource::XResourceBundle;
      46                 :            :     using ::com::sun::star::resource::XResourceBundleLoader;
      47                 :            :     /** === end UNO using === **/
      48                 :            :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      49                 :            : 
      50                 :            :     //====================================================================
      51                 :            :     //= EventLogger_Impl - declaration
      52                 :            :     //====================================================================
      53                 :          0 :     class EventLogger_Impl
      54                 :            :     {
      55                 :            :     private:
      56                 :            :         ::comphelper::ComponentContext  m_aContext;
      57                 :            :         ::rtl::OUString                 m_sLoggerName;
      58                 :            :         Reference< XLogger >            m_xLogger;
      59                 :            : 
      60                 :            :     public:
      61                 :          9 :         EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName )
      62                 :            :             :m_aContext( _rxContext )
      63                 :          9 :             ,m_sLoggerName( _rLoggerName )
      64                 :            :         {
      65         [ +  - ]:          9 :             impl_createLogger_nothrow();
      66                 :          9 :         }
      67                 :            : 
      68                 :        417 :         inline bool isValid() const { return m_xLogger.is(); }
      69                 :            :         inline const ::rtl::OUString&  getName() const { return m_sLoggerName; }
      70                 :        417 :         inline const Reference< XLogger >& getLogger() const { return m_xLogger; }
      71                 :          0 :         inline const ::comphelper::ComponentContext& getContext() const { return m_aContext; }
      72                 :            : 
      73                 :            :     private:
      74                 :            :         void    impl_createLogger_nothrow();
      75                 :            :     };
      76                 :            : 
      77                 :            :     //====================================================================
      78                 :            :     //= EventLogger_Impl - implementation
      79                 :            :     //====================================================================
      80                 :            :     //--------------------------------------------------------------------
      81                 :          9 :     void EventLogger_Impl::impl_createLogger_nothrow()
      82                 :            :     {
      83                 :            :         try
      84                 :            :         {
      85 [ +  - ][ +  - ]:          9 :             Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext.getUNOContext() ), UNO_QUERY_THROW );
                 [ +  - ]
      86         [ +  - ]:          9 :             if ( !m_sLoggerName.isEmpty() )
      87 [ +  - ][ +  - ]:          9 :                 m_xLogger = xPool->getNamedLogger( m_sLoggerName );
                 [ +  - ]
      88                 :            :             else
      89 [ #  # ][ #  # ]:          9 :                 m_xLogger = xPool->getDefaultLogger();
         [ #  # ][ #  # ]
      90                 :            :         }
      91                 :          0 :         catch( const Exception& e )
      92                 :            :         {
      93                 :            :             (void)e;
      94                 :            :             OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
      95                 :            :         }
      96                 :          9 :     }
      97                 :            : 
      98                 :            :     //====================================================================
      99                 :            :     //= EventLogger
     100                 :            :     //====================================================================
     101                 :            :     //--------------------------------------------------------------------
     102                 :          9 :     EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName )
     103 [ +  - ][ +  - ]:          9 :         :m_pImpl( new EventLogger_Impl( _rxContext, ::rtl::OUString::createFromAscii( _pAsciiLoggerName ) ) )
                 [ +  - ]
     104                 :            :     {
     105                 :          9 :     }
     106                 :            : 
     107                 :            :     //--------------------------------------------------------------------
     108                 :          0 :     EventLogger::~EventLogger()
     109                 :            :     {
     110                 :          0 :     }
     111                 :            : 
     112                 :            :     //--------------------------------------------------------------------
     113                 :        417 :     bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
     114                 :            :     {
     115         [ -  + ]:        417 :         if ( !m_pImpl->isValid() )
     116                 :          0 :             return false;
     117                 :            : 
     118                 :            :         try
     119                 :            :         {
     120 [ +  - ][ +  - ]:        417 :             return m_pImpl->getLogger()->isLoggable( _nLogLevel );
                 [ #  # ]
     121                 :            :         }
     122                 :          0 :         catch( const Exception& e )
     123                 :            :         {
     124                 :            :             (void)e;
     125                 :            :             OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
     126                 :            :         }
     127                 :            : 
     128                 :          0 :         return false;
     129                 :            :     }
     130                 :            : 
     131                 :            :     //--------------------------------------------------------------------
     132                 :            :     namespace
     133                 :            :     {
     134                 :          0 :         void    lcl_replaceParameter( ::rtl::OUString& _inout_Message, const ::rtl::OUString& _rPlaceHolder, const ::rtl::OUString& _rReplacement )
     135                 :            :         {
     136                 :          0 :             sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder );
     137                 :            :             OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
     138         [ #  # ]:          0 :             if ( nPlaceholderPosition < 0 )
     139                 :          0 :                 return;
     140                 :            : 
     141                 :          0 :             _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement );
     142                 :            :         }
     143                 :            :     }
     144                 :            : 
     145                 :            :     //--------------------------------------------------------------------
     146                 :          0 :     bool EventLogger::impl_log( const sal_Int32 _nLogLevel,
     147                 :            :         const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const ::rtl::OUString& _rMessage,
     148                 :            :         const OptionalString& _rArgument1, const OptionalString& _rArgument2,
     149                 :            :         const OptionalString& _rArgument3, const OptionalString& _rArgument4,
     150                 :            :         const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
     151                 :            :     {
     152                 :            :         // (if ::rtl::OUString had an indexOfAscii, we could save those ugly statics ...)
     153 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH1( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$1$" ) ) );
         [ #  # ][ #  # ]
     154 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH2( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$2$" ) ) );
         [ #  # ][ #  # ]
     155 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH3( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$3$" ) ) );
         [ #  # ][ #  # ]
     156 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH4( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$4$" ) ) );
         [ #  # ][ #  # ]
     157 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH5( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$5$" ) ) );
         [ #  # ][ #  # ]
     158 [ #  # ][ #  # ]:          0 :         static ::rtl::OUString sPH6( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$6$" ) ) );
         [ #  # ][ #  # ]
     159                 :            : 
     160                 :          0 :         ::rtl::OUString sMessage( _rMessage );
     161 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument1 )
     162         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH1, *_rArgument1 );
     163                 :            : 
     164 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument2 )
     165         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH2, *_rArgument2 );
     166                 :            : 
     167 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument3 )
     168         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH3, *_rArgument3 );
     169                 :            : 
     170 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument4 )
     171         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH4, *_rArgument4 );
     172                 :            : 
     173 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument5 )
     174         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH5, *_rArgument5 );
     175                 :            : 
     176 [ #  # ][ #  # ]:          0 :         if ( !!_rArgument6 )
     177         [ #  # ]:          0 :             lcl_replaceParameter( sMessage, sPH6, *_rArgument6 );
     178                 :            : 
     179                 :            :         try
     180                 :            :         {
     181                 :          0 :             Reference< XLogger > xLogger( m_pImpl->getLogger() );
     182                 :            :             OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
     183 [ #  # ][ #  # ]:          0 :             if ( _pSourceClass && _pSourceMethod )
     184                 :            :             {
     185         [ #  # ]:          0 :                 xLogger->logp(
     186                 :            :                     _nLogLevel,
     187                 :            :                     ::rtl::OUString::createFromAscii( _pSourceClass ),
     188                 :            :                     ::rtl::OUString::createFromAscii( _pSourceMethod ),
     189                 :            :                     sMessage
     190         [ #  # ]:          0 :                 );
     191                 :            :             }
     192                 :            :             else
     193                 :            :             {
     194 [ #  # ][ #  # ]:          0 :                 xLogger->log( _nLogLevel, sMessage );
     195         [ #  # ]:          0 :             }
     196                 :            :         }
     197         [ #  # ]:          0 :         catch( const Exception& e )
     198                 :            :         {
     199                 :            :             (void)e;
     200                 :            :             OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
     201                 :            :         }
     202                 :            : 
     203                 :          0 :         return false;
     204                 :            :     }
     205                 :            : 
     206                 :            :     //====================================================================
     207                 :            :     //= ResourceBasedEventLogger_Data
     208                 :            :     //====================================================================
     209                 :          0 :     struct ResourceBasedEventLogger_Data
     210                 :            :     {
     211                 :            :         /// the base name of the resource bundle
     212                 :            :         ::rtl::OUString                 sBundleBaseName;
     213                 :            :         /// did we already attempt to load the bundle?
     214                 :            :         bool                            bBundleLoaded;
     215                 :            :         /// the lazily loaded bundle
     216                 :            :         Reference< XResourceBundle >    xBundle;
     217                 :            : 
     218                 :          0 :         ResourceBasedEventLogger_Data()
     219                 :            :             :sBundleBaseName()
     220                 :            :             ,bBundleLoaded( false )
     221                 :          0 :             ,xBundle()
     222                 :            :         {
     223                 :          0 :         }
     224                 :            :     };
     225                 :            : 
     226                 :            :     //--------------------------------------------------------------------
     227                 :          0 :     bool    lcl_loadBundle_nothrow( const ComponentContext& _rContext, ResourceBasedEventLogger_Data& _rLoggerData )
     228                 :            :     {
     229         [ #  # ]:          0 :         if ( _rLoggerData.bBundleLoaded )
     230                 :          0 :             return _rLoggerData.xBundle.is();
     231                 :            : 
     232                 :            :         // no matter what happens below, don't attempt creation ever again
     233                 :          0 :         _rLoggerData.bBundleLoaded = true;
     234                 :            : 
     235                 :            :         try
     236                 :            :         {
     237 [ #  # ][ #  # ]:          0 :             Reference< XResourceBundleLoader > xLoader( _rContext.getSingleton( "com.sun.star.resource.OfficeResourceLoader" ), UNO_QUERY_THROW );
     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 :     ::rtl::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 :         ::rtl::OUString sMessage;
     254                 :            :         try
     255                 :            :         {
     256                 :          0 :             ::rtl::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 = ::rtl::OUString::createFromAscii( _pResourceBundleBaseName );
     279                 :          0 :     }
     280                 :            : 
     281                 :            :     //--------------------------------------------------------------------
     282                 :          0 :     ::rtl::OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const
     283                 :            :     {
     284                 :          0 :         ::rtl::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 :             ::rtl::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