1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_comphelper.hxx" 30 31 #include <comphelper/logging.hxx> 32 #include <comphelper/componentcontext.hxx> 33 34 /** === begin UNO includes === **/ 35 #include <com/sun/star/logging/LoggerPool.hpp> 36 #include <com/sun/star/logging/LogLevel.hpp> 37 #include <com/sun/star/resource/XResourceBundle.hpp> 38 #include <com/sun/star/resource/XResourceBundleLoader.hpp> 39 /** === end UNO includes === **/ 40 41 #include <rtl/ustrbuf.hxx> 42 43 //........................................................................ 44 namespace comphelper 45 { 46 //........................................................................ 47 48 /** === begin UNO using === **/ 49 using ::com::sun::star::uno::Reference; 50 using ::com::sun::star::uno::XComponentContext; 51 using ::com::sun::star::logging::XLoggerPool; 52 using ::com::sun::star::logging::LoggerPool; 53 using ::com::sun::star::logging::XLogger; 54 using ::com::sun::star::uno::UNO_QUERY_THROW; 55 using ::com::sun::star::uno::Exception; 56 using ::com::sun::star::logging::XLogHandler; 57 using ::com::sun::star::resource::XResourceBundle; 58 using ::com::sun::star::resource::XResourceBundleLoader; 59 /** === end UNO using === **/ 60 namespace LogLevel = ::com::sun::star::logging::LogLevel; 61 62 //==================================================================== 63 //= EventLogger_Impl - declaration 64 //==================================================================== 65 class EventLogger_Impl 66 { 67 private: 68 ::comphelper::ComponentContext m_aContext; 69 ::rtl::OUString m_sLoggerName; 70 Reference< XLogger > m_xLogger; 71 72 public: 73 EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName ) 74 :m_aContext( _rxContext ) 75 ,m_sLoggerName( _rLoggerName ) 76 { 77 impl_createLogger_nothrow(); 78 } 79 80 inline bool isValid() const { return m_xLogger.is(); } 81 inline const ::rtl::OUString& getName() const { return m_sLoggerName; } 82 inline const Reference< XLogger >& getLogger() const { return m_xLogger; } 83 inline const ::comphelper::ComponentContext& getContext() const { return m_aContext; } 84 85 private: 86 void impl_createLogger_nothrow(); 87 }; 88 89 //==================================================================== 90 //= EventLogger_Impl - implementation 91 //==================================================================== 92 //-------------------------------------------------------------------- 93 void EventLogger_Impl::impl_createLogger_nothrow() 94 { 95 try 96 { 97 Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext.getUNOContext() ), UNO_QUERY_THROW ); 98 if ( m_sLoggerName.getLength() ) 99 m_xLogger = xPool->getNamedLogger( m_sLoggerName ); 100 else 101 m_xLogger = xPool->getDefaultLogger(); 102 } 103 catch( const Exception& e ) 104 { 105 (void)e; 106 OSL_ENSURE( false, "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" ); 107 } 108 } 109 110 //==================================================================== 111 //= EventLogger 112 //==================================================================== 113 //-------------------------------------------------------------------- 114 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName ) 115 :m_pImpl( new EventLogger_Impl( _rxContext, _rLoggerName ) ) 116 { 117 } 118 119 //-------------------------------------------------------------------- 120 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName ) 121 :m_pImpl( new EventLogger_Impl( _rxContext, ::rtl::OUString::createFromAscii( _pAsciiLoggerName ) ) ) 122 { 123 } 124 125 //-------------------------------------------------------------------- 126 EventLogger::~EventLogger() 127 { 128 } 129 130 //-------------------------------------------------------------------- 131 const ::rtl::OUString& EventLogger::getName() const 132 { 133 return m_pImpl->getName(); 134 } 135 136 //-------------------------------------------------------------------- 137 sal_Int32 EventLogger::getLogLevel() const 138 { 139 try 140 { 141 if ( m_pImpl->isValid() ) 142 return m_pImpl->getLogger()->getLevel(); 143 } 144 catch( const Exception& e ) 145 { 146 (void)e; 147 OSL_ENSURE( false, "EventLogger::getLogLevel: caught an exception!" ); 148 } 149 150 return LogLevel::OFF; 151 } 152 153 //-------------------------------------------------------------------- 154 void EventLogger::setLogLevel( const sal_Int32 _nLogLevel ) const 155 { 156 try 157 { 158 if ( m_pImpl->isValid() ) 159 m_pImpl->getLogger()->setLevel( _nLogLevel ); 160 } 161 catch( const Exception& e ) 162 { 163 (void)e; 164 OSL_ENSURE( false, "EventLogger::setLogLevel: caught an exception!" ); 165 } 166 } 167 168 //-------------------------------------------------------------------- 169 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const 170 { 171 if ( !m_pImpl->isValid() ) 172 return false; 173 174 try 175 { 176 return m_pImpl->getLogger()->isLoggable( _nLogLevel ); 177 } 178 catch( const Exception& e ) 179 { 180 (void)e; 181 OSL_ENSURE( false, "EventLogger::isLoggable: caught an exception!" ); 182 } 183 184 return false; 185 } 186 187 //-------------------------------------------------------------------- 188 bool EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) 189 { 190 try 191 { 192 if ( m_pImpl->isValid() ) 193 { 194 m_pImpl->getLogger()->addLogHandler( _rxLogHandler ); 195 return true; 196 } 197 } 198 catch( const Exception& e ) 199 { 200 (void)e; 201 OSL_ENSURE( false, "EventLogger::addLogHandler: caught an exception!" ); 202 } 203 return false; 204 } 205 206 //-------------------------------------------------------------------- 207 bool EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) 208 { 209 try 210 { 211 if ( m_pImpl->isValid() ) 212 { 213 m_pImpl->getLogger()->removeLogHandler( _rxLogHandler ); 214 return true; 215 } 216 } 217 catch( const Exception& e ) 218 { 219 (void)e; 220 OSL_ENSURE( false, "EventLogger::removeLogHandler: caught an exception!" ); 221 } 222 return false; 223 } 224 225 //-------------------------------------------------------------------- 226 namespace 227 { 228 void lcl_replaceParameter( ::rtl::OUString& _inout_Message, const ::rtl::OUString& _rPlaceHolder, const ::rtl::OUString& _rReplacement ) 229 { 230 sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder ); 231 OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" ); 232 if ( nPlaceholderPosition < 0 ) 233 return; 234 235 _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement ); 236 } 237 } 238 239 //-------------------------------------------------------------------- 240 bool EventLogger::impl_log( const sal_Int32 _nLogLevel, 241 const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const ::rtl::OUString& _rMessage, 242 const OptionalString& _rArgument1, const OptionalString& _rArgument2, 243 const OptionalString& _rArgument3, const OptionalString& _rArgument4, 244 const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const 245 { 246 // (if ::rtl::OUString had an indexOfAscii, we could save those ugly statics ...) 247 static ::rtl::OUString sPH1( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$1$" ) ) ); 248 static ::rtl::OUString sPH2( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$2$" ) ) ); 249 static ::rtl::OUString sPH3( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$3$" ) ) ); 250 static ::rtl::OUString sPH4( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$4$" ) ) ); 251 static ::rtl::OUString sPH5( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$5$" ) ) ); 252 static ::rtl::OUString sPH6( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$6$" ) ) ); 253 254 ::rtl::OUString sMessage( _rMessage ); 255 if ( !!_rArgument1 ) 256 lcl_replaceParameter( sMessage, sPH1, *_rArgument1 ); 257 258 if ( !!_rArgument2 ) 259 lcl_replaceParameter( sMessage, sPH2, *_rArgument2 ); 260 261 if ( !!_rArgument3 ) 262 lcl_replaceParameter( sMessage, sPH3, *_rArgument3 ); 263 264 if ( !!_rArgument4 ) 265 lcl_replaceParameter( sMessage, sPH4, *_rArgument4 ); 266 267 if ( !!_rArgument5 ) 268 lcl_replaceParameter( sMessage, sPH5, *_rArgument5 ); 269 270 if ( !!_rArgument6 ) 271 lcl_replaceParameter( sMessage, sPH6, *_rArgument6 ); 272 273 try 274 { 275 Reference< XLogger > xLogger( m_pImpl->getLogger() ); 276 OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" ); 277 if ( _pSourceClass && _pSourceMethod ) 278 { 279 xLogger->logp( 280 _nLogLevel, 281 ::rtl::OUString::createFromAscii( _pSourceClass ), 282 ::rtl::OUString::createFromAscii( _pSourceMethod ), 283 sMessage 284 ); 285 } 286 else 287 { 288 xLogger->log( _nLogLevel, sMessage ); 289 } 290 } 291 catch( const Exception& e ) 292 { 293 (void)e; 294 OSL_ENSURE( false, "EventLogger::impl_log: caught an exception!" ); 295 } 296 297 return false; 298 } 299 300 //==================================================================== 301 //= ResourceBasedEventLogger_Data 302 //==================================================================== 303 struct ResourceBasedEventLogger_Data 304 { 305 /// the base name of the resource bundle 306 ::rtl::OUString sBundleBaseName; 307 /// did we already attempt to load the bundle? 308 bool bBundleLoaded; 309 /// the lazily loaded bundle 310 Reference< XResourceBundle > xBundle; 311 312 ResourceBasedEventLogger_Data() 313 :sBundleBaseName() 314 ,bBundleLoaded( false ) 315 ,xBundle() 316 { 317 } 318 }; 319 320 //-------------------------------------------------------------------- 321 bool lcl_loadBundle_nothrow( const ComponentContext& _rContext, ResourceBasedEventLogger_Data& _rLoggerData ) 322 { 323 if ( _rLoggerData.bBundleLoaded ) 324 return _rLoggerData.xBundle.is(); 325 326 // no matter what happens below, don't attempt creation ever again 327 _rLoggerData.bBundleLoaded = true; 328 329 try 330 { 331 Reference< XResourceBundleLoader > xLoader( _rContext.getSingleton( "com.sun.star.resource.OfficeResourceLoader" ), UNO_QUERY_THROW ); 332 _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW ); 333 } 334 catch( const Exception& e ) 335 { 336 (void)e; 337 OSL_ENSURE( false, "lcl_loadBundle_nothrow: caught an exception!" ); 338 } 339 340 return _rLoggerData.xBundle.is(); 341 } 342 343 //-------------------------------------------------------------------- 344 ::rtl::OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID ) 345 { 346 OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" ); 347 ::rtl::OUString sMessage; 348 try 349 { 350 ::rtl::OUStringBuffer aBuffer; 351 aBuffer.appendAscii( "string:" ); 352 aBuffer.append( _nMessageResID ); 353 OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage ); 354 } 355 catch( const Exception& e ) 356 { 357 (void)e; 358 OSL_ENSURE( false, "lcl_loadString_nothrow: caught an exception!" ); 359 } 360 return sMessage; 361 } 362 363 //==================================================================== 364 //= ResourceBasedEventLogger 365 //==================================================================== 366 //-------------------------------------------------------------------- 367 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rResourceBundleBaseName, 368 const ::rtl::OUString& _rLoggerName ) 369 :EventLogger( _rxContext, _rLoggerName ) 370 ,m_pData( new ResourceBasedEventLogger_Data ) 371 { 372 m_pData->sBundleBaseName = _rResourceBundleBaseName; 373 } 374 375 //-------------------------------------------------------------------- 376 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName, 377 const sal_Char* _pAsciiLoggerName ) 378 :EventLogger( _rxContext, _pAsciiLoggerName ) 379 ,m_pData( new ResourceBasedEventLogger_Data ) 380 { 381 m_pData->sBundleBaseName = ::rtl::OUString::createFromAscii( _pResourceBundleBaseName ); 382 } 383 384 //-------------------------------------------------------------------- 385 ::rtl::OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const 386 { 387 ::rtl::OUString sMessage; 388 if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) ) 389 sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID ); 390 if ( sMessage.getLength() == 0 ) 391 { 392 ::rtl::OUStringBuffer aBuffer; 393 aBuffer.appendAscii( "<invalid event resource: '" ); 394 aBuffer.append( m_pData->sBundleBaseName ); 395 aBuffer.appendAscii( ":" ); 396 aBuffer.append( _nMessageResID ); 397 aBuffer.appendAscii( "'>" ); 398 sMessage = aBuffer.makeStringAndClear(); 399 } 400 return sMessage; 401 } 402 403 //........................................................................ 404 } // namespace comphelper 405 //........................................................................ 406