LCOV - code coverage report
Current view: top level - extensions/source/logging - loggerconfig.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 109 0.0 %
Date: 2014-04-14 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             : #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           0 :         void    lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, 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 OUString   sVariableValue;
      86             : 
      87           0 :                 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           0 :                     ,sVariableValue( _rVariableValue )
      93             :                 {
      94           0 :                 }
      95             :             };
      96             : 
      97           0 :             OUString sLoggerName;
      98           0 :             try { sLoggerName = _rxLogger->getName(); }
      99           0 :             catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
     100             : 
     101             :             TimeValue aTimeValue;
     102             :             oslDateTime aDateTime;
     103           0 :             OSL_VERIFY( osl_getSystemTime( &aTimeValue ) );
     104           0 :             OSL_VERIFY( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) );
     105             : 
     106             :             char buffer[ 30 ];
     107           0 :             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           0 :                       (int)aDateTime.Day );
     113           0 :             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           0 :                 ::sal::static_int_cast< sal_Int16 >( aDateTime.NanoSeconds / 10000000 ) );
     120           0 :             rtl::OUString sTime = rtl::OUString::createFromAscii( buffer );
     121             : 
     122           0 :             rtl::OUStringBuffer aBuff;
     123           0 :             aBuff.append( sDate );
     124           0 :             aBuff.append( '.' );
     125           0 :             aBuff.append( sTime );
     126           0 :             rtl::OUString sDateTime = aBuff.makeStringAndClear();
     127             : 
     128           0 :             oslProcessIdentifier aProcessId = 0;
     129             :             oslProcessInfo info;
     130           0 :             info.Size = sizeof (oslProcessInfo);
     131           0 :             if ( osl_getProcessInfo ( 0, osl_Process_IDENTIFIER, &info ) == osl_Process_E_None)
     132           0 :                 aProcessId = info.Ident;
     133           0 :             rtl::OUString aPID = OUString::number( aProcessId );
     134             : 
     135             :             Variable aVariables[] =
     136             :             {
     137           0 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName ),
     138           0 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(date)" ), sDate ),
     139           0 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(time)" ), sTime ),
     140           0 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(datetime)" ), sDateTime ),
     141           0 :                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(pid)" ), aPID )
     142           0 :             };
     143             : 
     144           0 :             for ( size_t i = 0; i < SAL_N_ELEMENTS( aVariables ); ++i )
     145             :             {
     146           0 :                 OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
     147           0 :                 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
     148           0 :                 if  (   ( nVariableIndex == 0 )
     149           0 :                     ||  (   ( nVariableIndex > 0 )
     150           0 :                         &&  ( sPattern[ nVariableIndex - 1 ] != '$' )
     151             :                         )
     152             :                     )
     153             :                 {
     154             :                     // found an (unescaped) variable
     155           0 :                     _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
     156             :                 }
     157           0 :             }
     158           0 :         }
     159             : 
     160             : 
     161           0 :         void    lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const OUString& _rSettingName, Any& _inout_rSettingValue )
     162             :         {
     163           0 :             if ( _rSettingName != "FileURL" )
     164             :                 // not interested in this setting
     165           0 :                 return;
     166             : 
     167           0 :             OUString sURL;
     168           0 :             OSL_VERIFY( _inout_rSettingValue >>= sURL );
     169           0 :             lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
     170           0 :             _inout_rSettingValue <<= sURL;
     171             :         }
     172             : 
     173             : 
     174           0 :         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           0 :             Reference< XInterface > xInstance;
     184             : 
     185             :             // read the settings for the to-be-created service
     186           0 :             Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
     187           0 :                 OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
     188             : 
     189           0 :             Sequence< OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
     190           0 :             size_t nServiceSettingCount( aSettingNames.getLength() );
     191           0 :             Sequence< NamedValue > aSettings( nServiceSettingCount );
     192           0 :             if ( nServiceSettingCount )
     193             :             {
     194           0 :                 const OUString* pSettingNames = aSettingNames.getConstArray();
     195           0 :                 const OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
     196           0 :                 NamedValue* pSetting = aSettings.getArray();
     197             : 
     198           0 :                 for (   ;
     199             :                         pSettingNames != pSettingNamesEnd;
     200             :                         ++pSettingNames, ++pSetting
     201             :                     )
     202             :                 {
     203           0 :                     pSetting->Name = *pSettingNames;
     204           0 :                     pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
     205             : 
     206           0 :                     if ( _pSettingTranslation )
     207           0 :                         (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
     208             :                 }
     209             :             }
     210             : 
     211           0 :             OUString sServiceName;
     212           0 :             _rxLoggerSettings->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
     213           0 :             if ( !sServiceName.isEmpty() )
     214             :             {
     215           0 :                 bool bSuccess = false;
     216           0 :                 if ( aSettings.getLength() )
     217             :                 {
     218           0 :                     Sequence< Any > aConstructionArgs(1);
     219           0 :                     aConstructionArgs[0] <<= aSettings;
     220           0 :                     xInstance = _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aConstructionArgs, _rContext);
     221           0 :                     bSuccess = xInstance.is();
     222             :                 }
     223             :                 else
     224             :                 {
     225           0 :                     xInstance = _rContext->getServiceManager()->createInstanceWithContext(sServiceName, _rContext);
     226           0 :                     bSuccess = xInstance.is();
     227             :                 }
     228             : 
     229           0 :                 if ( !bSuccess )
     230           0 :                     throw ServiceNotRegisteredException( sServiceName, NULL );
     231             :             }
     232             : 
     233           0 :             return xInstance;
     234             :         }
     235             :     }
     236             : 
     237             : 
     238           0 :     void initializeLoggerFromConfiguration( const Reference<XComponentContext>& _rContext, const Reference< XLogger >& _rxLogger )
     239             :     {
     240             :         try
     241             :         {
     242           0 :             if ( !_rxLogger.is() )
     243           0 :                 throw NullPointerException();
     244             : 
     245             :             Reference< XMultiServiceFactory > xConfigProvider(
     246           0 :                 com::sun::star::configuration::theDefaultProvider::get(_rContext));
     247             : 
     248             :             // write access to the "Settings" node (which includes settings for all loggers)
     249           0 :             Sequence< Any > aArguments(1);
     250           0 :             aArguments[0] <<= NamedValue(
     251             :                 OUString( "nodepath" ),
     252             :                 makeAny( OUString( "/org.openoffice.Office.Logging/Settings" ) )
     253           0 :             );
     254           0 :             Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
     255             :                 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
     256             :                 aArguments
     257           0 :             ), UNO_QUERY_THROW );
     258             : 
     259           0 :             OUString sLoggerName( _rxLogger->getName() );
     260           0 :             if ( !xAllSettings->hasByName( sLoggerName ) )
     261             :             {
     262             :                 // no node yet for this logger. Create default settings.
     263           0 :                 Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
     264           0 :                 Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
     265           0 :                 xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
     266           0 :                 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
     267           0 :                 xChanges->commitChanges();
     268             :             }
     269             : 
     270             :             // actually read and forward the settings
     271           0 :             Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
     272             : 
     273             :             // the log level
     274           0 :             sal_Int32 nLogLevel( LogLevel::OFF );
     275           0 :             OSL_VERIFY( xLoggerSettings->getByName("LogLevel") >>= nLogLevel );
     276           0 :             _rxLogger->setLevel( nLogLevel );
     277             : 
     278             :             // the default handler, if any
     279           0 :             Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
     280           0 :             if ( !xUntyped.is() )
     281             :                 // no handler -> we're done
     282           0 :                 return;
     283           0 :             Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
     284           0 :             _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           0 :             xHandler->setLevel( nLogLevel );
     289             : 
     290             :             // the default formatter for the handler
     291           0 :             xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
     292           0 :             if ( !xUntyped.is() )
     293             :                 // no formatter -> we're done
     294           0 :                 return;
     295           0 :             Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
     296           0 :             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