1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 //........................................................................
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
55 Reference
< XComponentContext
> m_aContext
;
56 OUString m_sLoggerName
;
57 Reference
< XLogger
> m_xLogger
;
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
; }
73 void impl_createLogger_nothrow();
76 //====================================================================
77 //= EventLogger_Impl - implementation
78 //====================================================================
79 //--------------------------------------------------------------------
80 void EventLogger_Impl::impl_createLogger_nothrow()
84 Reference
< XLoggerPool
> xPool( LoggerPool::get( m_aContext
) );
85 if ( !m_sLoggerName
.isEmpty() )
86 m_xLogger
= xPool
->getNamedLogger( m_sLoggerName
);
88 m_xLogger
= xPool
->getDefaultLogger();
90 catch( const Exception
& e
)
93 OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
97 //====================================================================
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() )
119 return m_pImpl
->getLogger()->isLoggable( _nLogLevel
);
121 catch( const Exception
& e
)
124 OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
130 //--------------------------------------------------------------------
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 )
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
);
161 lcl_replaceParameter( sMessage
, sPH1
, *_rArgument1
);
164 lcl_replaceParameter( sMessage
, sPH2
, *_rArgument2
);
167 lcl_replaceParameter( sMessage
, sPH3
, *_rArgument3
);
170 lcl_replaceParameter( sMessage
, sPH4
, *_rArgument4
);
173 lcl_replaceParameter( sMessage
, sPH5
, *_rArgument5
);
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
)
186 OUString::createFromAscii( _pSourceClass
),
187 OUString::createFromAscii( _pSourceMethod
),
193 xLogger
->log( _nLogLevel
, sMessage
);
196 catch( const Exception
& e
)
199 OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
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?
214 /// the lazily loaded bundle
215 Reference
< XResourceBundle
> xBundle
;
217 ResourceBasedEventLogger_Data()
219 ,bBundleLoaded( false )
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(
239 _rLoggerData
.xBundle
= Reference
< XResourceBundle
>( xLoader
->loadBundle_Default( _rLoggerData
.sBundleBaseName
), UNO_QUERY_THROW
);
241 catch( const Exception
& 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!" );
257 OUStringBuffer aBuffer
;
258 aBuffer
.appendAscii( "string:" );
259 aBuffer
.append( _nMessageResID
);
260 OSL_VERIFY( _rxBundle
->getDirectElement( aBuffer
.makeStringAndClear() ) >>= sMessage
);
262 catch( const Exception
& e
)
265 OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" );
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
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();
301 //........................................................................
302 } // namespace comphelper
303 //........................................................................
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */