fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / comphelper / source / misc / logging.cxx
blob98f38d878a9b40aa60642c0fba8e5445ada98f2f
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 <rtl/ustrbuf.hxx>
31 //........................................................................
32 namespace comphelper
34 //........................................................................
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XComponentContext;
38 using ::com::sun::star::logging::XLoggerPool;
39 using ::com::sun::star::logging::LoggerPool;
40 using ::com::sun::star::logging::XLogger;
41 using ::com::sun::star::uno::UNO_QUERY_THROW;
42 using ::com::sun::star::uno::Exception;
43 using ::com::sun::star::logging::XLogHandler;
44 using ::com::sun::star::resource::XResourceBundle;
45 using ::com::sun::star::resource::XResourceBundleLoader;
47 namespace LogLevel = ::com::sun::star::logging::LogLevel;
49 //====================================================================
50 //= EventLogger_Impl - declaration
51 //====================================================================
52 class EventLogger_Impl
54 private:
55 Reference< XComponentContext > m_aContext;
56 OUString m_sLoggerName;
57 Reference< XLogger > m_xLogger;
59 public:
60 EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
61 :m_aContext( _rxContext )
62 ,m_sLoggerName( _rLoggerName )
64 impl_createLogger_nothrow();
67 inline bool isValid() const { return m_xLogger.is(); }
68 inline const OUString& getName() const { return m_sLoggerName; }
69 inline const Reference< XLogger >& getLogger() const { return m_xLogger; }
70 inline Reference< XComponentContext > getContext() const { return m_aContext; }
72 private:
73 void impl_createLogger_nothrow();
76 //====================================================================
77 //= EventLogger_Impl - implementation
78 //====================================================================
79 //--------------------------------------------------------------------
80 void EventLogger_Impl::impl_createLogger_nothrow()
82 try
84 Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext ) );
85 if ( !m_sLoggerName.isEmpty() )
86 m_xLogger = xPool->getNamedLogger( m_sLoggerName );
87 else
88 m_xLogger = xPool->getDefaultLogger();
90 catch( const Exception& e )
92 (void)e;
93 OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
97 //====================================================================
98 //= EventLogger
99 //====================================================================
100 //--------------------------------------------------------------------
101 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName )
102 :m_pImpl( new EventLogger_Impl( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
106 //--------------------------------------------------------------------
107 EventLogger::~EventLogger()
111 //--------------------------------------------------------------------
112 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
114 if ( !m_pImpl->isValid() )
115 return false;
119 return m_pImpl->getLogger()->isLoggable( _nLogLevel );
121 catch( const Exception& e )
123 (void)e;
124 OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
127 return false;
130 //--------------------------------------------------------------------
131 namespace
133 void lcl_replaceParameter( OUString& _inout_Message, const OUString& _rPlaceHolder, const OUString& _rReplacement )
135 sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder );
136 OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
137 if ( nPlaceholderPosition < 0 )
138 return;
140 _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement );
144 //--------------------------------------------------------------------
145 bool EventLogger::impl_log( const sal_Int32 _nLogLevel,
146 const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage,
147 const OptionalString& _rArgument1, const OptionalString& _rArgument2,
148 const OptionalString& _rArgument3, const OptionalString& _rArgument4,
149 const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
151 // (if OUString had an indexOfAscii, we could save those ugly statics ...)
152 static OUString sPH1( "$1$" );
153 static OUString sPH2( "$2$" );
154 static OUString sPH3( "$3$" );
155 static OUString sPH4( "$4$" );
156 static OUString sPH5( "$5$" );
157 static OUString sPH6( "$6$" );
159 OUString sMessage( _rMessage );
160 if ( !!_rArgument1 )
161 lcl_replaceParameter( sMessage, sPH1, *_rArgument1 );
163 if ( !!_rArgument2 )
164 lcl_replaceParameter( sMessage, sPH2, *_rArgument2 );
166 if ( !!_rArgument3 )
167 lcl_replaceParameter( sMessage, sPH3, *_rArgument3 );
169 if ( !!_rArgument4 )
170 lcl_replaceParameter( sMessage, sPH4, *_rArgument4 );
172 if ( !!_rArgument5 )
173 lcl_replaceParameter( sMessage, sPH5, *_rArgument5 );
175 if ( !!_rArgument6 )
176 lcl_replaceParameter( sMessage, sPH6, *_rArgument6 );
180 Reference< XLogger > xLogger( m_pImpl->getLogger() );
181 OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
182 if ( _pSourceClass && _pSourceMethod )
184 xLogger->logp(
185 _nLogLevel,
186 OUString::createFromAscii( _pSourceClass ),
187 OUString::createFromAscii( _pSourceMethod ),
188 sMessage
191 else
193 xLogger->log( _nLogLevel, sMessage );
196 catch( const Exception& e )
198 (void)e;
199 OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
202 return false;
205 //====================================================================
206 //= ResourceBasedEventLogger_Data
207 //====================================================================
208 struct ResourceBasedEventLogger_Data
210 /// the base name of the resource bundle
211 OUString sBundleBaseName;
212 /// did we already attempt to load the bundle?
213 bool bBundleLoaded;
214 /// the lazily loaded bundle
215 Reference< XResourceBundle > xBundle;
217 ResourceBasedEventLogger_Data()
218 :sBundleBaseName()
219 ,bBundleLoaded( false )
220 ,xBundle()
225 //--------------------------------------------------------------------
226 bool lcl_loadBundle_nothrow( Reference< XComponentContext > const & _rContext, ResourceBasedEventLogger_Data& _rLoggerData )
228 if ( _rLoggerData.bBundleLoaded )
229 return _rLoggerData.xBundle.is();
231 // no matter what happens below, don't attempt creation ever again
232 _rLoggerData.bBundleLoaded = true;
236 Reference< XResourceBundleLoader > xLoader(
237 com::sun::star::resource::OfficeResourceLoader::get(
238 _rContext ) );
239 _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW );
241 catch( const Exception& e )
243 (void)e;
244 OSL_FAIL( "lcl_loadBundle_nothrow: caught an exception!" );
247 return _rLoggerData.xBundle.is();
250 //--------------------------------------------------------------------
251 OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID )
253 OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" );
254 OUString sMessage;
257 OUStringBuffer aBuffer;
258 aBuffer.appendAscii( "string:" );
259 aBuffer.append( _nMessageResID );
260 OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage );
262 catch( const Exception& e )
264 (void)e;
265 OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" );
267 return sMessage;
270 //====================================================================
271 //= ResourceBasedEventLogger
272 //====================================================================
273 //--------------------------------------------------------------------
274 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName,
275 const sal_Char* _pAsciiLoggerName )
276 :EventLogger( _rxContext, _pAsciiLoggerName )
277 ,m_pData( new ResourceBasedEventLogger_Data )
279 m_pData->sBundleBaseName = OUString::createFromAscii( _pResourceBundleBaseName );
282 //--------------------------------------------------------------------
283 OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const
285 OUString sMessage;
286 if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) )
287 sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID );
288 if ( sMessage.isEmpty() )
290 OUStringBuffer aBuffer;
291 aBuffer.appendAscii( "<invalid event resource: '" );
292 aBuffer.append( m_pData->sBundleBaseName );
293 aBuffer.appendAscii( ":" );
294 aBuffer.append( _nMessageResID );
295 aBuffer.appendAscii( "'>" );
296 sMessage = aBuffer.makeStringAndClear();
298 return sMessage;
301 //........................................................................
302 } // namespace comphelper
303 //........................................................................
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */