diff options
author | Jens-Heiner Rechtien <hr@openoffice.org> | 2007-06-27 13:54:48 +0000 |
---|---|---|
committer | Jens-Heiner Rechtien <hr@openoffice.org> | 2007-06-27 13:54:48 +0000 |
commit | 4b0f233742d809c27d9896fcfb16bad2e7feab62 (patch) | |
tree | 2a78beac0941bef9d5464035a5e19385e5584eff /comphelper | |
parent | 49a9d4d62f1e4c1f982e9de76f3c5128fe3cbfd8 (diff) |
INTEGRATION: CWS sdblogging (1.1.2); FILE ADDED
2007/04/13 06:08:06 fs 1.1.2.7: #i10000#
2007/04/12 09:52:40 fs 1.1.2.6: #i10000#
2007/04/12 09:05:31 fs 1.1.2.5: const as const can
2007/04/11 13:48:19 fs 1.1.2.4: #i76119# support for logging arguments which evaluate to empty strings (by introducing OptionalString as parameter type to pass to impl_log)
2007/04/11 08:48:43 fs 1.1.2.3: #i76119# support for log messages loaded from a resource file
2007/04/05 09:13:21 fs 1.1.2.2: #i76119# new ctor taking an ASCII logger name
2007/04/05 07:12:36 fs 1.1.2.1: #i76119# encapsulating access to XLogger
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/source/misc/logging.cxx | 421 |
1 files changed, 421 insertions, 0 deletions
diff --git a/comphelper/source/misc/logging.cxx b/comphelper/source/misc/logging.cxx new file mode 100644 index 000000000000..1bd0559bff26 --- /dev/null +++ b/comphelper/source/misc/logging.cxx @@ -0,0 +1,421 @@ +/************************************************************************* + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: logging.cxx,v $ + * + * $Revision: 1.2 $ + * + * last change: $Author: hr $ $Date: 2007-06-27 14:54:48 $ + * + * The Contents of this file are made available subject to + * the terms of GNU Lesser General Public License Version 2.1. + * + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2005 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_comphelper.hxx" + +#include <comphelper/logging.hxx> +#include <comphelper/componentcontext.hxx> + +/** === begin UNO includes === **/ +#ifndef _COM_SUN_STAR_LOGGING_LOGGERPOOL_HPP_ +#include <com/sun/star/logging/LoggerPool.hpp> +#endif +#ifndef _COM_SUN_STAR_LOGGING_LOGLEVEL_HPP_ +#include <com/sun/star/logging/LogLevel.hpp> +#endif +#ifndef _COM_SUN_STAR_RESOURCE_XRESOURCEBUNDLE_HPP_ +#include <com/sun/star/resource/XResourceBundle.hpp> +#endif +#ifndef _COM_SUN_STAR_RESOURCE_XRESOURCEBUNDLELOADER_HPP_ +#include <com/sun/star/resource/XResourceBundleLoader.hpp> +#endif +/** === end UNO includes === **/ + +#include <rtl/ustrbuf.hxx> + +//........................................................................ +namespace comphelper +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XComponentContext; + using ::com::sun::star::logging::XLoggerPool; + using ::com::sun::star::logging::LoggerPool; + using ::com::sun::star::logging::XLogger; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::logging::XLogHandler; + using ::com::sun::star::resource::XResourceBundle; + using ::com::sun::star::resource::XResourceBundleLoader; + /** === end UNO using === **/ + namespace LogLevel = ::com::sun::star::logging::LogLevel; + + //==================================================================== + //= EventLogger_Impl - declaration + //==================================================================== + class EventLogger_Impl + { + private: + ::comphelper::ComponentContext m_aContext; + ::rtl::OUString m_sLoggerName; + Reference< XLogger > m_xLogger; + + public: + EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName ) + :m_aContext( _rxContext ) + ,m_sLoggerName( _rLoggerName ) + { + impl_createLogger_nothrow(); + } + + inline bool isValid() const { return m_xLogger.is(); } + inline const ::rtl::OUString& getName() const { return m_sLoggerName; } + inline const Reference< XLogger >& getLogger() const { return m_xLogger; } + inline const ::comphelper::ComponentContext& getContext() const { return m_aContext; } + + private: + void impl_createLogger_nothrow(); + }; + + //==================================================================== + //= EventLogger_Impl - implementation + //==================================================================== + //-------------------------------------------------------------------- + void EventLogger_Impl::impl_createLogger_nothrow() + { + try + { + Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext.getUNOContext() ), UNO_QUERY_THROW ); + if ( m_sLoggerName.getLength() ) + m_xLogger = xPool->getNamedLogger( m_sLoggerName ); + else + m_xLogger = xPool->getDefaultLogger(); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" ); + } + } + + //==================================================================== + //= EventLogger + //==================================================================== + //-------------------------------------------------------------------- + EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName ) + :m_pImpl( new EventLogger_Impl( _rxContext, _rLoggerName ) ) + { + } + + //-------------------------------------------------------------------- + EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName ) + :m_pImpl( new EventLogger_Impl( _rxContext, ::rtl::OUString::createFromAscii( _pAsciiLoggerName ) ) ) + { + } + + //-------------------------------------------------------------------- + EventLogger::~EventLogger() + { + } + + //-------------------------------------------------------------------- + const ::rtl::OUString& EventLogger::getName() const + { + return m_pImpl->getName(); + } + + //-------------------------------------------------------------------- + sal_Int32 EventLogger::getLogLevel() const + { + try + { + if ( m_pImpl->isValid() ) + return m_pImpl->getLogger()->getLevel(); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::getLogLevel: caught an exception!" ); + } + + return LogLevel::OFF; + } + + //-------------------------------------------------------------------- + void EventLogger::setLogLevel( const sal_Int32 _nLogLevel ) const + { + try + { + if ( m_pImpl->isValid() ) + m_pImpl->getLogger()->setLevel( _nLogLevel ); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::setLogLevel: caught an exception!" ); + } + } + + //-------------------------------------------------------------------- + bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const + { + if ( !m_pImpl->isValid() ) + return false; + + try + { + return m_pImpl->getLogger()->isLoggable( _nLogLevel ); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::isLoggable: caught an exception!" ); + } + + return false; + } + + //-------------------------------------------------------------------- + bool EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) + { + try + { + if ( m_pImpl->isValid() ) + { + m_pImpl->getLogger()->addLogHandler( _rxLogHandler ); + return true; + } + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::addLogHandler: caught an exception!" ); + } + return false; + } + + //-------------------------------------------------------------------- + bool EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) + { + try + { + if ( m_pImpl->isValid() ) + { + m_pImpl->getLogger()->removeLogHandler( _rxLogHandler ); + return true; + } + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::removeLogHandler: caught an exception!" ); + } + return false; + } + + //-------------------------------------------------------------------- + namespace + { + void lcl_replaceParameter( ::rtl::OUString& _inout_Message, const ::rtl::OUString& _rPlaceHolder, const ::rtl::OUString& _rReplacement ) + { + sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder ); + OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" ); + if ( nPlaceholderPosition < 0 ) + return; + + _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement ); + } + } + + //-------------------------------------------------------------------- + bool EventLogger::impl_log( const sal_Int32 _nLogLevel, + const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const ::rtl::OUString& _rMessage, + const OptionalString& _rArgument1, const OptionalString& _rArgument2, + const OptionalString& _rArgument3, const OptionalString& _rArgument4, + const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const + { + // (if ::rtl::OUString had an indexOfAscii, we could save those ugly statics ...) + static ::rtl::OUString sPH1( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$1$" ) ) ); + static ::rtl::OUString sPH2( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$2$" ) ) ); + static ::rtl::OUString sPH3( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$3$" ) ) ); + static ::rtl::OUString sPH4( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$4$" ) ) ); + static ::rtl::OUString sPH5( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$5$" ) ) ); + static ::rtl::OUString sPH6( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$6$" ) ) ); + + ::rtl::OUString sMessage( _rMessage ); + if ( !!_rArgument1 ) + lcl_replaceParameter( sMessage, sPH1, *_rArgument1 ); + + if ( !!_rArgument2 ) + lcl_replaceParameter( sMessage, sPH2, *_rArgument2 ); + + if ( !!_rArgument3 ) + lcl_replaceParameter( sMessage, sPH3, *_rArgument3 ); + + if ( !!_rArgument4 ) + lcl_replaceParameter( sMessage, sPH4, *_rArgument4 ); + + if ( !!_rArgument5 ) + lcl_replaceParameter( sMessage, sPH5, *_rArgument5 ); + + if ( !!_rArgument6 ) + lcl_replaceParameter( sMessage, sPH6, *_rArgument6 ); + + try + { + Reference< XLogger > xLogger( m_pImpl->getLogger() ); + OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" ); + if ( _pSourceClass && _pSourceMethod ) + { + xLogger->logp( + _nLogLevel, + ::rtl::OUString::createFromAscii( _pSourceClass ), + ::rtl::OUString::createFromAscii( _pSourceMethod ), + sMessage + ); + } + else + { + xLogger->log( _nLogLevel, sMessage ); + } + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "EventLogger::impl_log: caught an exception!" ); + } + + return false; + } + + //==================================================================== + //= ResourceBasedEventLogger_Data + //==================================================================== + struct ResourceBasedEventLogger_Data + { + /// the base name of the resource bundle + ::rtl::OUString sBundleBaseName; + /// did we already attempt to load the bundle? + bool bBundleLoaded; + /// the lazily loaded bundle + Reference< XResourceBundle > xBundle; + + ResourceBasedEventLogger_Data() + :sBundleBaseName() + ,bBundleLoaded( false ) + ,xBundle() + { + } + }; + + //-------------------------------------------------------------------- + bool lcl_loadBundle_nothrow( const ComponentContext& _rContext, ResourceBasedEventLogger_Data& _rLoggerData ) + { + if ( _rLoggerData.bBundleLoaded ) + return _rLoggerData.xBundle.is(); + + // no matter what happens below, don't attempt creation ever again + _rLoggerData.bBundleLoaded = true; + + try + { + Reference< XResourceBundleLoader > xLoader( _rContext.getSingleton( "com.sun.star.resource.OfficeResourceLoader" ), UNO_QUERY_THROW ); + _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW ); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "lcl_loadBundle_nothrow: caught an exception!" ); + } + + return _rLoggerData.xBundle.is(); + } + + //-------------------------------------------------------------------- + ::rtl::OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID ) + { + OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" ); + ::rtl::OUString sMessage; + try + { + ::rtl::OUStringBuffer aBuffer; + aBuffer.appendAscii( "string:" ); + aBuffer.append( _nMessageResID ); + OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage ); + } + catch( const Exception& e ) + { + (void)e; + OSL_ENSURE( false, "lcl_loadString_nothrow: caught an exception!" ); + } + return sMessage; + } + + //==================================================================== + //= ResourceBasedEventLogger + //==================================================================== + //-------------------------------------------------------------------- + ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rResourceBundleBaseName, + const ::rtl::OUString& _rLoggerName ) + :EventLogger( _rxContext, _rLoggerName ) + ,m_pData( new ResourceBasedEventLogger_Data ) + { + m_pData->sBundleBaseName = _rResourceBundleBaseName; + } + + //-------------------------------------------------------------------- + ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName, + const sal_Char* _pAsciiLoggerName ) + :EventLogger( _rxContext, _pAsciiLoggerName ) + ,m_pData( new ResourceBasedEventLogger_Data ) + { + m_pData->sBundleBaseName = ::rtl::OUString::createFromAscii( _pResourceBundleBaseName ); + } + + //-------------------------------------------------------------------- + ::rtl::OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const + { + ::rtl::OUString sMessage; + if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) ) + sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID ); + if ( sMessage.getLength() == 0 ) + { + ::rtl::OUStringBuffer aBuffer; + aBuffer.appendAscii( "<invalid event resource: '" ); + aBuffer.append( m_pData->sBundleBaseName ); + aBuffer.appendAscii( ":" ); + aBuffer.append( _nMessageResID ); + aBuffer.appendAscii( "'>" ); + sMessage = aBuffer.makeStringAndClear(); + } + return sMessage; + } + +//........................................................................ +} // namespace comphelper +//........................................................................ |