1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include <comphelper/logging.hxx>
32 #include <comphelper/componentcontext.hxx>
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 === **/
41 #include <rtl/ustrbuf.hxx>
43 //........................................................................
46 //........................................................................
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
;
62 //====================================================================
63 //= EventLogger_Impl - declaration
64 //====================================================================
65 class EventLogger_Impl
68 ::comphelper::ComponentContext m_aContext
;
69 ::rtl::OUString m_sLoggerName
;
70 Reference
< XLogger
> m_xLogger
;
73 EventLogger_Impl( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rLoggerName
)
74 :m_aContext( _rxContext
)
75 ,m_sLoggerName( _rLoggerName
)
77 impl_createLogger_nothrow();
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
; }
86 void impl_createLogger_nothrow();
89 //====================================================================
90 //= EventLogger_Impl - implementation
91 //====================================================================
92 //--------------------------------------------------------------------
93 void EventLogger_Impl::impl_createLogger_nothrow()
97 Reference
< XLoggerPool
> xPool( LoggerPool::get( m_aContext
.getUNOContext() ), UNO_QUERY_THROW
);
98 if ( m_sLoggerName
.getLength() )
99 m_xLogger
= xPool
->getNamedLogger( m_sLoggerName
);
101 m_xLogger
= xPool
->getDefaultLogger();
103 catch( const Exception
& e
)
106 OSL_ENSURE( false, "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
110 //====================================================================
112 //====================================================================
113 //--------------------------------------------------------------------
114 EventLogger::EventLogger( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rLoggerName
)
115 :m_pImpl( new EventLogger_Impl( _rxContext
, _rLoggerName
) )
119 //--------------------------------------------------------------------
120 EventLogger::EventLogger( const Reference
< XComponentContext
>& _rxContext
, const sal_Char
* _pAsciiLoggerName
)
121 :m_pImpl( new EventLogger_Impl( _rxContext
, ::rtl::OUString::createFromAscii( _pAsciiLoggerName
) ) )
125 //--------------------------------------------------------------------
126 EventLogger::~EventLogger()
130 //--------------------------------------------------------------------
131 const ::rtl::OUString
& EventLogger::getName() const
133 return m_pImpl
->getName();
136 //--------------------------------------------------------------------
137 sal_Int32
EventLogger::getLogLevel() const
141 if ( m_pImpl
->isValid() )
142 return m_pImpl
->getLogger()->getLevel();
144 catch( const Exception
& e
)
147 OSL_ENSURE( false, "EventLogger::getLogLevel: caught an exception!" );
150 return LogLevel::OFF
;
153 //--------------------------------------------------------------------
154 void EventLogger::setLogLevel( const sal_Int32 _nLogLevel
) const
158 if ( m_pImpl
->isValid() )
159 m_pImpl
->getLogger()->setLevel( _nLogLevel
);
161 catch( const Exception
& e
)
164 OSL_ENSURE( false, "EventLogger::setLogLevel: caught an exception!" );
168 //--------------------------------------------------------------------
169 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel
) const
171 if ( !m_pImpl
->isValid() )
176 return m_pImpl
->getLogger()->isLoggable( _nLogLevel
);
178 catch( const Exception
& e
)
181 OSL_ENSURE( false, "EventLogger::isLoggable: caught an exception!" );
187 //--------------------------------------------------------------------
188 bool EventLogger::addLogHandler( const Reference
< XLogHandler
>& _rxLogHandler
)
192 if ( m_pImpl
->isValid() )
194 m_pImpl
->getLogger()->addLogHandler( _rxLogHandler
);
198 catch( const Exception
& e
)
201 OSL_ENSURE( false, "EventLogger::addLogHandler: caught an exception!" );
206 //--------------------------------------------------------------------
207 bool EventLogger::removeLogHandler( const Reference
< XLogHandler
>& _rxLogHandler
)
211 if ( m_pImpl
->isValid() )
213 m_pImpl
->getLogger()->removeLogHandler( _rxLogHandler
);
217 catch( const Exception
& e
)
220 OSL_ENSURE( false, "EventLogger::removeLogHandler: caught an exception!" );
225 //--------------------------------------------------------------------
228 void lcl_replaceParameter( ::rtl::OUString
& _inout_Message
, const ::rtl::OUString
& _rPlaceHolder
, const ::rtl::OUString
& _rReplacement
)
230 sal_Int32 nPlaceholderPosition
= _inout_Message
.indexOf( _rPlaceHolder
);
231 OSL_ENSURE( nPlaceholderPosition
>= 0, "lcl_replaceParameter: placeholder not found!" );
232 if ( nPlaceholderPosition
< 0 )
235 _inout_Message
= _inout_Message
.replaceAt( nPlaceholderPosition
, _rPlaceHolder
.getLength(), _rReplacement
);
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
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$" ) ) );
254 ::rtl::OUString
sMessage( _rMessage
);
256 lcl_replaceParameter( sMessage
, sPH1
, *_rArgument1
);
259 lcl_replaceParameter( sMessage
, sPH2
, *_rArgument2
);
262 lcl_replaceParameter( sMessage
, sPH3
, *_rArgument3
);
265 lcl_replaceParameter( sMessage
, sPH4
, *_rArgument4
);
268 lcl_replaceParameter( sMessage
, sPH5
, *_rArgument5
);
271 lcl_replaceParameter( sMessage
, sPH6
, *_rArgument6
);
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
)
281 ::rtl::OUString::createFromAscii( _pSourceClass
),
282 ::rtl::OUString::createFromAscii( _pSourceMethod
),
288 xLogger
->log( _nLogLevel
, sMessage
);
291 catch( const Exception
& e
)
294 OSL_ENSURE( false, "EventLogger::impl_log: caught an exception!" );
300 //====================================================================
301 //= ResourceBasedEventLogger_Data
302 //====================================================================
303 struct ResourceBasedEventLogger_Data
305 /// the base name of the resource bundle
306 ::rtl::OUString sBundleBaseName
;
307 /// did we already attempt to load the bundle?
309 /// the lazily loaded bundle
310 Reference
< XResourceBundle
> xBundle
;
312 ResourceBasedEventLogger_Data()
314 ,bBundleLoaded( false )
320 //--------------------------------------------------------------------
321 bool lcl_loadBundle_nothrow( const ComponentContext
& _rContext
, ResourceBasedEventLogger_Data
& _rLoggerData
)
323 if ( _rLoggerData
.bBundleLoaded
)
324 return _rLoggerData
.xBundle
.is();
326 // no matter what happens below, don't attempt creation ever again
327 _rLoggerData
.bBundleLoaded
= true;
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
);
334 catch( const Exception
& e
)
337 OSL_ENSURE( false, "lcl_loadBundle_nothrow: caught an exception!" );
340 return _rLoggerData
.xBundle
.is();
343 //--------------------------------------------------------------------
344 ::rtl::OUString
lcl_loadString_nothrow( const Reference
< XResourceBundle
>& _rxBundle
, const sal_Int32 _nMessageResID
)
346 OSL_PRECOND( _rxBundle
.is(), "lcl_loadString_nothrow: this will crash!" );
347 ::rtl::OUString sMessage
;
350 ::rtl::OUStringBuffer aBuffer
;
351 aBuffer
.appendAscii( "string:" );
352 aBuffer
.append( _nMessageResID
);
353 OSL_VERIFY( _rxBundle
->getDirectElement( aBuffer
.makeStringAndClear() ) >>= sMessage
);
355 catch( const Exception
& e
)
358 OSL_ENSURE( false, "lcl_loadString_nothrow: caught an exception!" );
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
)
372 m_pData
->sBundleBaseName
= _rResourceBundleBaseName
;
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
)
381 m_pData
->sBundleBaseName
= ::rtl::OUString::createFromAscii( _pResourceBundleBaseName
);
384 //--------------------------------------------------------------------
385 ::rtl::OUString
ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID
) const
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 )
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();
403 //........................................................................
404 } // namespace comphelper
405 //........................................................................