LCOV - code coverage report
Current view: top level - libreoffice/extensions/source/logging - loggerconfig.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 82 0.0 %
Date: 2012-12-27 Functions: 0 6 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 "loggerconfig.hxx"
      22             : 
      23             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      24             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      25             : #include <com/sun/star/container/XNameContainer.hpp>
      26             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      27             : #include <com/sun/star/util/XChangesBatch.hpp>
      28             : #include <com/sun/star/logging/LogLevel.hpp>
      29             : #include <com/sun/star/lang/NullPointerException.hpp>
      30             : #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
      31             : #include <com/sun/star/beans/NamedValue.hpp>
      32             : #include <com/sun/star/logging/XLogHandler.hpp>
      33             : #include <com/sun/star/logging/XLogFormatter.hpp>
      34             : 
      35             : #include <tools/diagnose_ex.h>
      36             : 
      37             : #include <comphelper/componentcontext.hxx>
      38             : 
      39             : #include <cppuhelper/component_context.hxx>
      40             : 
      41             : #include <vector>
      42             : #include <sal/macros.h>
      43             : 
      44             : //........................................................................
      45             : namespace logging
      46             : {
      47             : //........................................................................
      48             : 
      49             :     /** === begin UNO using === **/
      50             :     using ::com::sun::star::uno::Reference;
      51             :     using ::com::sun::star::logging::XLogger;
      52             :     using ::com::sun::star::lang::XMultiServiceFactory;
      53             :     using ::com::sun::star::uno::Sequence;
      54             :     using ::com::sun::star::uno::Any;
      55             :     using ::com::sun::star::container::XNameContainer;
      56             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      57             :     using ::com::sun::star::lang::XSingleServiceFactory;
      58             :     using ::com::sun::star::uno::XInterface;
      59             :     using ::com::sun::star::util::XChangesBatch;
      60             :     using ::com::sun::star::uno::makeAny;
      61             :     using ::com::sun::star::lang::NullPointerException;
      62             :     using ::com::sun::star::uno::Exception;
      63             :     using ::com::sun::star::lang::ServiceNotRegisteredException;
      64             :     using ::com::sun::star::beans::NamedValue;
      65             :     using ::com::sun::star::logging::XLogHandler;
      66             :     using ::com::sun::star::logging::XLogFormatter;
      67             :     using ::com::sun::star::container::XNameAccess;
      68             :     using ::com::sun::star::uno::XComponentContext;
      69             :     /** === end UNO using === **/
      70             :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      71             : 
      72             :     namespace
      73             :     {
      74             :         //----------------------------------------------------------------
      75             :         typedef void (*SettingTranslation)( const Reference< XLogger >&, const ::rtl::OUString&, Any& );
      76             : 
      77             :         //----------------------------------------------------------------
      78           0 :         void    lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, ::rtl::OUString& _inout_rFileURL )
      79             :         {
      80           0 :             struct Variable
      81             :             {
      82             :                 const sal_Char*         pVariablePattern;
      83             :                 const sal_Int32         nPatternLength;
      84             :                 rtl_TextEncoding        eEncoding;
      85             :                 const ::rtl::OUString   sVariableValue;
      86             : 
      87           0 :                 Variable( const sal_Char* _pVariablePattern,  const sal_Int32 _nPatternLength, rtl_TextEncoding _eEncoding,
      88             :                         const ::rtl::OUString& _rVariableValue )
      89             :                     :pVariablePattern( _pVariablePattern )
      90             :                     ,nPatternLength( _nPatternLength )
      91             :                     ,eEncoding( _eEncoding )
      92           0 :                     ,sVariableValue( _rVariableValue )
      93             :                 {
      94           0 :                 }
      95             :             };
      96             : 
      97           0 :             ::rtl::OUString sLoggerName;
      98           0 :             try { sLoggerName = _rxLogger->getName(); }
      99           0 :             catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
     100             : 
     101             :             Variable aVariables[] =
     102             :             {
     103             :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName )
     104           0 :             };
     105             : 
     106           0 :             for ( size_t i = 0; i < SAL_N_ELEMENTS( aVariables ); ++i )
     107             :             {
     108           0 :                 ::rtl::OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
     109           0 :                 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
     110           0 :                 if  (   ( nVariableIndex == 0 )
     111             :                     ||  (   ( nVariableIndex > 0 )
     112           0 :                         &&  ( sPattern[ nVariableIndex - 1 ] != '$' )
     113             :                         )
     114             :                     )
     115             :                 {
     116             :                     // found an (unescaped) variable
     117           0 :                     _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
     118             :                 }
     119           0 :             }
     120           0 :         }
     121             : 
     122             :         //----------------------------------------------------------------
     123           0 :         void    lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const ::rtl::OUString& _rSettingName, Any& _inout_rSettingValue )
     124             :         {
     125           0 :             if ( _rSettingName != "FileURL" )
     126             :                 // not interested in this setting
     127           0 :                 return;
     128             : 
     129           0 :             ::rtl::OUString sURL;
     130           0 :             OSL_VERIFY( _inout_rSettingValue >>= sURL );
     131           0 :             lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
     132           0 :             _inout_rSettingValue <<= sURL;
     133             :         }
     134             : 
     135             :         //----------------------------------------------------------------
     136           0 :         Reference< XInterface > lcl_createInstanceFromSetting_throw(
     137             :                 const ::comphelper::ComponentContext& _rContext,
     138             :                 const Reference< XLogger >& _rxLogger,
     139             :                 const Reference< XNameAccess >& _rxLoggerSettings,
     140             :                 const sal_Char* _pServiceNameAsciiNodeName,
     141             :                 const sal_Char* _pServiceSettingsAsciiNodeName,
     142             :                 SettingTranslation _pSettingTranslation = NULL
     143             :             )
     144             :         {
     145           0 :             Reference< XInterface > xInstance;
     146             : 
     147             :             // read the settings for the to-be-created service
     148           0 :             Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
     149           0 :                 ::rtl::OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
     150             : 
     151           0 :             Sequence< ::rtl::OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
     152           0 :             size_t nServiceSettingCount( aSettingNames.getLength() );
     153           0 :             Sequence< NamedValue > aSettings( nServiceSettingCount );
     154           0 :             if ( nServiceSettingCount )
     155             :             {
     156           0 :                 const ::rtl::OUString* pSettingNames = aSettingNames.getConstArray();
     157           0 :                 const ::rtl::OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
     158           0 :                 NamedValue* pSetting = aSettings.getArray();
     159             : 
     160           0 :                 for (   ;
     161             :                         pSettingNames != pSettingNamesEnd;
     162             :                         ++pSettingNames, ++pSetting
     163             :                     )
     164             :                 {
     165           0 :                     pSetting->Name = *pSettingNames;
     166           0 :                     pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
     167             : 
     168           0 :                     if ( _pSettingTranslation )
     169           0 :                         (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
     170             :                 }
     171             :             }
     172             : 
     173           0 :             ::rtl::OUString sServiceName;
     174           0 :             _rxLoggerSettings->getByName( ::rtl::OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
     175           0 :             if ( !sServiceName.isEmpty() )
     176             :             {
     177           0 :                 bool bSuccess = false;
     178           0 :                 if ( aSettings.getLength() )
     179             :                 {
     180           0 :                     Sequence< Any > aConstructionArgs(1);
     181           0 :                     aConstructionArgs[0] <<= aSettings;
     182           0 :                     bSuccess = _rContext.createComponentWithArguments( sServiceName, aConstructionArgs, xInstance );
     183             :                 }
     184             :                 else
     185             :                 {
     186           0 :                     bSuccess = _rContext.createComponent( sServiceName, xInstance );
     187             :                 }
     188             : 
     189           0 :                 if ( !bSuccess )
     190           0 :                     throw ServiceNotRegisteredException( sServiceName, NULL );
     191             :             }
     192             : 
     193           0 :             return xInstance;
     194             :         }
     195             :     }
     196             : 
     197             :     //--------------------------------------------------------------------
     198           0 :     void initializeLoggerFromConfiguration( const ::comphelper::ComponentContext& _rContext, const Reference< XLogger >& _rxLogger )
     199             :     {
     200             :         try
     201             :         {
     202           0 :             if ( !_rxLogger.is() )
     203           0 :                 throw NullPointerException();
     204             : 
     205             :             Reference< XMultiServiceFactory > xConfigProvider(
     206             :                 com::sun::star::configuration::theDefaultProvider::get(
     207           0 :                     _rContext.getUNOContext()));
     208             : 
     209             :             // write access to the "Settings" node (which includes settings for all loggers)
     210           0 :             Sequence< Any > aArguments(1);
     211           0 :             aArguments[0] <<= NamedValue(
     212             :                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) ),
     213             :                 makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Logging/Settings" ) ) )
     214           0 :             );
     215           0 :             Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
     216             :                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
     217             :                 aArguments
     218           0 :             ), UNO_QUERY_THROW );
     219             : 
     220           0 :             ::rtl::OUString sLoggerName( _rxLogger->getName() );
     221           0 :             if ( !xAllSettings->hasByName( sLoggerName ) )
     222             :             {
     223             :                 // no node yet for this logger. Create default settings.
     224           0 :                 Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
     225           0 :                 Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
     226           0 :                 xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
     227           0 :                 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
     228           0 :                 xChanges->commitChanges();
     229             :             }
     230             : 
     231             :             // actually read and forward the settings
     232           0 :             Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
     233             : 
     234             :             // the log level
     235           0 :             sal_Int32 nLogLevel( LogLevel::OFF );
     236           0 :             OSL_VERIFY( xLoggerSettings->getByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "LogLevel" ) ) ) >>= nLogLevel );
     237           0 :             _rxLogger->setLevel( nLogLevel );
     238             : 
     239             :             // the default handler, if any
     240           0 :             Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
     241           0 :             if ( !xUntyped.is() )
     242             :                 // no handler -> we're done
     243             :                 return;
     244           0 :             Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
     245           0 :             _rxLogger->addLogHandler( xHandler );
     246             : 
     247             :             // The newly created handler might have an own (default) level. Ensure that it uses
     248             :             // the same level as the logger.
     249           0 :             xHandler->setLevel( nLogLevel );
     250             : 
     251             :             // the default formatter for the handler
     252           0 :             xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
     253           0 :             if ( !xUntyped.is() )
     254             :                 // no formatter -> we're done
     255             :                 return;
     256           0 :             Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
     257           0 :             xHandler->setFormatter( xFormatter );
     258             : 
     259             :             // TODO: we could first create the formatter, then the handler. This would allow
     260             :             // passing the formatter as value in the component context, so the handler would
     261             :             // not create an own default formatter
     262             :         }
     263           0 :         catch( const Exception& )
     264             :         {
     265             :             DBG_UNHANDLED_EXCEPTION();
     266             :         }
     267             :     }
     268             : 
     269             : //........................................................................
     270             : } // namespace logging
     271             : //........................................................................
     272             : 
     273             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10