LCOV - code coverage report
Current view: top level - extensions/source/logging - loggerconfig.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 103 109 94.5 %
Date: 2014-04-11 Functions: 6 6 100.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             : #include <stdio.h>
      23             : 
      24             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      25             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      26             : #include <com/sun/star/container/XNameContainer.hpp>
      27             : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
      28             : #include <com/sun/star/util/XChangesBatch.hpp>
      29             : #include <com/sun/star/logging/LogLevel.hpp>
      30             : #include <com/sun/star/lang/NullPointerException.hpp>
      31             : #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
      32             : #include <com/sun/star/beans/NamedValue.hpp>
      33             : #include <com/sun/star/logging/XLogHandler.hpp>
      34             : #include <com/sun/star/logging/XLogFormatter.hpp>
      35             : 
      36             : #include <tools/diagnose_ex.h>
      37             : #include <osl/process.h>
      38             : #include <rtl/ustrbuf.hxx>
      39             : 
      40             : #include <cppuhelper/component_context.hxx>
      41             : 
      42             : #include <vector>
      43             : #include <sal/macros.h>
      44             : 
      45             : 
      46             : namespace logging
      47             : {
      48             : 
      49             : 
      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             : 
      70             :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      71             : 
      72             :     namespace
      73             :     {
      74             : 
      75             :         typedef void (*SettingTranslation)( const Reference< XLogger >&, const OUString&, Any& );
      76             : 
      77             : 
      78           6 :         void    lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, OUString& _inout_rFileURL )
      79             :         {
      80          30 :             struct Variable
      81             :             {
      82             :                 const sal_Char*         pVariablePattern;
      83             :                 const sal_Int32         nPatternLength;
      84             :                 rtl_TextEncoding        eEncoding;
      85             :                 const OUString   sVariableValue;
      86             : 
      87          30 :                 Variable( const sal_Char* _pVariablePattern,  const sal_Int32 _nPatternLength, rtl_TextEncoding _eEncoding,
      88             :                         const OUString& _rVariableValue )
      89             :                     :pVariablePattern( _pVariablePattern )
      90             :                     ,nPatternLength( _nPatternLength )
      91             :                     ,eEncoding( _eEncoding )
      92          30 :                     ,sVariableValue( _rVariableValue )
      93             :                 {
      94          30 :                 }
      95             :             };
      96             : 
      97           6 :             OUString sLoggerName;
      98           6 :             try { sLoggerName = _rxLogger->getName(); }
      99           0 :             catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
     100             : 
     101             :             TimeValue aTimeValue;
     102             :             oslDateTime aDateTime;
     103           6 :             OSL_VERIFY( osl_getSystemTime( &aTimeValue ) );
     104           6 :             OSL_VERIFY( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) );
     105             : 
     106             :             char buffer[ 30 ];
     107           6 :             const size_t buffer_size = sizeof( buffer );
     108             : 
     109             :             snprintf( buffer, buffer_size, "%04i-%02i-%02i",
     110             :                       (int)aDateTime.Year,
     111             :                       (int)aDateTime.Month,
     112           6 :                       (int)aDateTime.Day );
     113          12 :             rtl::OUString sDate = rtl::OUString::createFromAscii( buffer );
     114             : 
     115             :             snprintf( buffer, buffer_size, "%02i-%02i-%02i.%03i",
     116             :                 (int)aDateTime.Hours,
     117             :                 (int)aDateTime.Minutes,
     118             :                 (int)aDateTime.Seconds,
     119           6 :                 ::sal::static_int_cast< sal_Int16 >( aDateTime.NanoSeconds / 10000000 ) );
     120          12 :             rtl::OUString sTime = rtl::OUString::createFromAscii( buffer );
     121             : 
     122          12 :             rtl::OUStringBuffer aBuff;
     123           6 :             aBuff.append( sDate );
     124           6 :             aBuff.append( '.' );
     125           6 :             aBuff.append( sTime );
     126          12 :             rtl::OUString sDateTime = aBuff.makeStringAndClear();
     127             : 
     128           6 :             oslProcessIdentifier aProcessId = 0;
     129             :             oslProcessInfo info;
     130           6 :             info.Size = sizeof (oslProcessInfo);
     131           6 :             if ( osl_getProcessInfo ( 0, osl_Process_IDENTIFIER, &info ) == osl_Process_E_None)
     132           6 :                 aProcessId = info.Ident;
     133          12 :             rtl::OUString aPID = OUString::number( aProcessId );
     134             : 
     135             :             Variable aVariables[] =
     136             :             {
     137           6 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName ),
     138           6 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(date)" ), sDate ),
     139           6 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(time)" ), sTime ),
     140           6 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(datetime)" ), sDateTime ),
     141           6 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(pid)" ), aPID )
     142          42 :             };
     143             : 
     144          36 :             for ( size_t i = 0; i < SAL_N_ELEMENTS( aVariables ); ++i )
     145             :             {
     146          30 :                 OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
     147          30 :                 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
     148          30 :                 if  (   ( nVariableIndex == 0 )
     149          36 :                     ||  (   ( nVariableIndex > 0 )
     150           6 :                         &&  ( sPattern[ nVariableIndex - 1 ] != '$' )
     151             :                         )
     152             :                     )
     153             :                 {
     154             :                     // found an (unescaped) variable
     155           6 :                     _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
     156             :                 }
     157          36 :             }
     158           6 :         }
     159             : 
     160             : 
     161           6 :         void    lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const OUString& _rSettingName, Any& _inout_rSettingValue )
     162             :         {
     163           6 :             if ( _rSettingName != "FileURL" )
     164             :                 // not interested in this setting
     165           6 :                 return;
     166             : 
     167           6 :             OUString sURL;
     168           6 :             OSL_VERIFY( _inout_rSettingValue >>= sURL );
     169           6 :             lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
     170           6 :             _inout_rSettingValue <<= sURL;
     171             :         }
     172             : 
     173             : 
     174          12 :         Reference< XInterface > lcl_createInstanceFromSetting_throw(
     175             :                 const Reference<XComponentContext>& _rContext,
     176             :                 const Reference< XLogger >& _rxLogger,
     177             :                 const Reference< XNameAccess >& _rxLoggerSettings,
     178             :                 const sal_Char* _pServiceNameAsciiNodeName,
     179             :                 const sal_Char* _pServiceSettingsAsciiNodeName,
     180             :                 SettingTranslation _pSettingTranslation = NULL
     181             :             )
     182             :         {
     183          12 :             Reference< XInterface > xInstance;
     184             : 
     185             :             // read the settings for the to-be-created service
     186          12 :             Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
     187          24 :                 OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
     188             : 
     189          24 :             Sequence< OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
     190          12 :             size_t nServiceSettingCount( aSettingNames.getLength() );
     191          24 :             Sequence< NamedValue > aSettings( nServiceSettingCount );
     192          12 :             if ( nServiceSettingCount )
     193             :             {
     194           6 :                 const OUString* pSettingNames = aSettingNames.getConstArray();
     195           6 :                 const OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
     196           6 :                 NamedValue* pSetting = aSettings.getArray();
     197             : 
     198          12 :                 for (   ;
     199             :                         pSettingNames != pSettingNamesEnd;
     200             :                         ++pSettingNames, ++pSetting
     201             :                     )
     202             :                 {
     203           6 :                     pSetting->Name = *pSettingNames;
     204           6 :                     pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
     205             : 
     206           6 :                     if ( _pSettingTranslation )
     207           6 :                         (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
     208             :                 }
     209             :             }
     210             : 
     211          24 :             OUString sServiceName;
     212          12 :             _rxLoggerSettings->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
     213          12 :             if ( !sServiceName.isEmpty() )
     214             :             {
     215          12 :                 bool bSuccess = false;
     216          12 :                 if ( aSettings.getLength() )
     217             :                 {
     218           6 :                     Sequence< Any > aConstructionArgs(1);
     219           6 :                     aConstructionArgs[0] <<= aSettings;
     220           6 :                     xInstance = _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aConstructionArgs, _rContext);
     221           6 :                     bSuccess = xInstance.is();
     222             :                 }
     223             :                 else
     224             :                 {
     225           6 :                     xInstance = _rContext->getServiceManager()->createInstanceWithContext(sServiceName, _rContext);
     226           6 :                     bSuccess = xInstance.is();
     227             :                 }
     228             : 
     229          12 :                 if ( !bSuccess )
     230           0 :                     throw ServiceNotRegisteredException( sServiceName, NULL );
     231             :             }
     232             : 
     233          24 :             return xInstance;
     234             :         }
     235             :     }
     236             : 
     237             : 
     238           6 :     void initializeLoggerFromConfiguration( const Reference<XComponentContext>& _rContext, const Reference< XLogger >& _rxLogger )
     239             :     {
     240             :         try
     241             :         {
     242           6 :             if ( !_rxLogger.is() )
     243           0 :                 throw NullPointerException();
     244             : 
     245             :             Reference< XMultiServiceFactory > xConfigProvider(
     246           6 :                 com::sun::star::configuration::theDefaultProvider::get(_rContext));
     247             : 
     248             :             // write access to the "Settings" node (which includes settings for all loggers)
     249          12 :             Sequence< Any > aArguments(1);
     250          12 :             aArguments[0] <<= NamedValue(
     251             :                 OUString( "nodepath" ),
     252             :                 makeAny( OUString( "/org.openoffice.Office.Logging/Settings" ) )
     253           6 :             );
     254           6 :             Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
     255             :                 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
     256             :                 aArguments
     257          12 :             ), UNO_QUERY_THROW );
     258             : 
     259          12 :             OUString sLoggerName( _rxLogger->getName() );
     260           6 :             if ( !xAllSettings->hasByName( sLoggerName ) )
     261             :             {
     262             :                 // no node yet for this logger. Create default settings.
     263           6 :                 Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
     264          12 :                 Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
     265           6 :                 xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
     266          12 :                 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
     267          12 :                 xChanges->commitChanges();
     268             :             }
     269             : 
     270             :             // actually read and forward the settings
     271          12 :             Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
     272             : 
     273             :             // the log level
     274           6 :             sal_Int32 nLogLevel( LogLevel::OFF );
     275           6 :             OSL_VERIFY( xLoggerSettings->getByName("LogLevel") >>= nLogLevel );
     276           6 :             _rxLogger->setLevel( nLogLevel );
     277             : 
     278             :             // the default handler, if any
     279          12 :             Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
     280           6 :             if ( !xUntyped.is() )
     281             :                 // no handler -> we're done
     282           0 :                 return;
     283          12 :             Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
     284           6 :             _rxLogger->addLogHandler( xHandler );
     285             : 
     286             :             // The newly created handler might have an own (default) level. Ensure that it uses
     287             :             // the same level as the logger.
     288           6 :             xHandler->setLevel( nLogLevel );
     289             : 
     290             :             // the default formatter for the handler
     291           6 :             xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
     292           6 :             if ( !xUntyped.is() )
     293             :                 // no formatter -> we're done
     294           0 :                 return;
     295          12 :             Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
     296          12 :             xHandler->setFormatter( xFormatter );
     297             : 
     298             :             // TODO: we could first create the formatter, then the handler. This would allow
     299             :             // passing the formatter as value in the component context, so the handler would
     300             :             // not create an own default formatter
     301             :         }
     302           0 :         catch( const Exception& )
     303             :         {
     304             :             DBG_UNHANDLED_EXCEPTION();
     305             :         }
     306             :     }
     307             : 
     308             : 
     309             : } // namespace logging
     310             : 
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10