bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / logging.cxx
blob801b5225a5414db9e6f610c5ff5d18048b9e34bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <comphelper/logging.hxx>
23 #include <com/sun/star/logging/LoggerPool.hpp>
24 #include <com/sun/star/logging/LogLevel.hpp>
25 #include <com/sun/star/resource/OfficeResourceLoader.hpp>
26 #include <com/sun/star/resource/XResourceBundle.hpp>
27 #include <com/sun/star/resource/XResourceBundleLoader.hpp>
29 #include <osl/diagnose.h>
30 #include <rtl/ustrbuf.hxx>
33 namespace comphelper
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::XComponentContext;
39 using ::com::sun::star::logging::XLoggerPool;
40 using ::com::sun::star::logging::LoggerPool;
41 using ::com::sun::star::logging::XLogger;
42 using ::com::sun::star::uno::UNO_QUERY_THROW;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::logging::XLogHandler;
45 using ::com::sun::star::resource::XResourceBundle;
46 using ::com::sun::star::resource::XResourceBundleLoader;
48 namespace LogLevel = ::com::sun::star::logging::LogLevel;
50 class EventLogger_Impl
52 private:
53 Reference< XComponentContext > m_aContext;
54 OUString m_sLoggerName;
55 Reference< XLogger > m_xLogger;
57 public:
58 EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
59 :m_aContext( _rxContext )
60 ,m_sLoggerName( _rLoggerName )
62 impl_createLogger_nothrow();
65 inline bool isValid() const { return m_xLogger.is(); }
66 inline const Reference< XLogger >& getLogger() const { return m_xLogger; }
67 inline Reference< XComponentContext > getContext() const { return m_aContext; }
69 private:
70 void impl_createLogger_nothrow();
73 void EventLogger_Impl::impl_createLogger_nothrow()
75 try
77 Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext ) );
78 if ( !m_sLoggerName.isEmpty() )
79 m_xLogger = xPool->getNamedLogger( m_sLoggerName );
80 else
81 m_xLogger = xPool->getDefaultLogger();
83 catch( const Exception& e )
85 (void)e;
86 OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
90 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName )
91 :m_pImpl( new EventLogger_Impl( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
96 EventLogger::~EventLogger()
101 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
103 if ( !m_pImpl->isValid() )
104 return false;
108 return m_pImpl->getLogger()->isLoggable( _nLogLevel );
110 catch( const Exception& e )
112 (void)e;
113 OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
116 return false;
120 namespace
122 void lcl_replaceParameter( OUString& _inout_Message, const char* _rPlaceHolder, const OUString& _rReplacement )
124 sal_Int32 nPlaceholderPosition = _inout_Message.indexOfAsciiL( _rPlaceHolder, strlen(_rPlaceHolder) );
125 OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
126 if ( nPlaceholderPosition < 0 )
127 return;
129 _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, strlen(_rPlaceHolder), _rReplacement );
134 bool EventLogger::impl_log( const sal_Int32 _nLogLevel,
135 const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage,
136 const OptionalString& _rArgument1, const OptionalString& _rArgument2,
137 const OptionalString& _rArgument3, const OptionalString& _rArgument4,
138 const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
140 OUString sMessage( _rMessage );
141 if ( !!_rArgument1 )
142 lcl_replaceParameter( sMessage, "$1$", *_rArgument1 );
144 if ( !!_rArgument2 )
145 lcl_replaceParameter( sMessage, "$2$", *_rArgument2 );
147 if ( !!_rArgument3 )
148 lcl_replaceParameter( sMessage, "$3$", *_rArgument3 );
150 if ( !!_rArgument4 )
151 lcl_replaceParameter( sMessage, "$4$", *_rArgument4 );
153 if ( !!_rArgument5 )
154 lcl_replaceParameter( sMessage, "$5$", *_rArgument5 );
156 if ( !!_rArgument6 )
157 lcl_replaceParameter( sMessage, "$6$", *_rArgument6 );
161 Reference< XLogger > xLogger( m_pImpl->getLogger() );
162 OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
163 if ( _pSourceClass && _pSourceMethod )
165 xLogger->logp(
166 _nLogLevel,
167 OUString::createFromAscii( _pSourceClass ),
168 OUString::createFromAscii( _pSourceMethod ),
169 sMessage
172 else
174 xLogger->log( _nLogLevel, sMessage );
177 catch( const Exception& e )
179 (void)e;
180 OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
183 return false;
186 struct ResourceBasedEventLogger_Data
188 /// the base name of the resource bundle
189 OUString sBundleBaseName;
190 /// did we already attempt to load the bundle?
191 bool bBundleLoaded;
192 /// the lazily loaded bundle
193 Reference< XResourceBundle > xBundle;
195 ResourceBasedEventLogger_Data()
196 :sBundleBaseName()
197 ,bBundleLoaded( false )
198 ,xBundle()
204 bool lcl_loadBundle_nothrow( Reference< XComponentContext > const & _rContext, ResourceBasedEventLogger_Data& _rLoggerData )
206 if ( _rLoggerData.bBundleLoaded )
207 return _rLoggerData.xBundle.is();
209 // no matter what happens below, don't attempt creation ever again
210 _rLoggerData.bBundleLoaded = true;
214 Reference< XResourceBundleLoader > xLoader(
215 com::sun::star::resource::OfficeResourceLoader::get(
216 _rContext ) );
217 _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW );
219 catch( const Exception& e )
221 (void)e;
222 OSL_FAIL( "lcl_loadBundle_nothrow: caught an exception!" );
225 return _rLoggerData.xBundle.is();
229 OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID )
231 OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" );
232 OUString sMessage;
235 OUStringBuffer aBuffer;
236 aBuffer.appendAscii( "string:" );
237 aBuffer.append( _nMessageResID );
238 OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage );
240 catch( const Exception& e )
242 (void)e;
243 OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" );
245 return sMessage;
248 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName,
249 const sal_Char* _pAsciiLoggerName )
250 :EventLogger( _rxContext, _pAsciiLoggerName )
251 ,m_pData( new ResourceBasedEventLogger_Data )
253 m_pData->sBundleBaseName = OUString::createFromAscii( _pResourceBundleBaseName );
257 OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const
259 OUString sMessage;
260 if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) )
261 sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID );
262 if ( sMessage.isEmpty() )
264 OUStringBuffer aBuffer;
265 aBuffer.appendAscii( "<invalid event resource: '" );
266 aBuffer.append( m_pData->sBundleBaseName );
267 aBuffer.appendAscii( ":" );
268 aBuffer.append( _nMessageResID );
269 aBuffer.appendAscii( "'>" );
270 sMessage = aBuffer.makeStringAndClear();
272 return sMessage;
276 } // namespace comphelper
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */