1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: logging.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include <comphelper/logging.hxx>
35 #include <comphelper/componentcontext.hxx>
37 /** === begin UNO includes === **/
38 #include <com/sun/star/logging/LoggerPool.hpp>
39 #include <com/sun/star/logging/LogLevel.hpp>
40 #include <com/sun/star/resource/XResourceBundle.hpp>
41 #include <com/sun/star/resource/XResourceBundleLoader.hpp>
42 /** === end UNO includes === **/
44 #include <rtl/ustrbuf.hxx>
46 //........................................................................
49 //........................................................................
51 /** === begin UNO using === **/
52 using ::com::sun::star::uno::Reference
;
53 using ::com::sun::star::uno::XComponentContext
;
54 using ::com::sun::star::logging::XLoggerPool
;
55 using ::com::sun::star::logging::LoggerPool
;
56 using ::com::sun::star::logging::XLogger
;
57 using ::com::sun::star::uno::UNO_QUERY_THROW
;
58 using ::com::sun::star::uno::Exception
;
59 using ::com::sun::star::logging::XLogHandler
;
60 using ::com::sun::star::resource::XResourceBundle
;
61 using ::com::sun::star::resource::XResourceBundleLoader
;
62 /** === end UNO using === **/
63 namespace LogLevel
= ::com::sun::star::logging::LogLevel
;
65 //====================================================================
66 //= EventLogger_Impl - declaration
67 //====================================================================
68 class EventLogger_Impl
71 ::comphelper::ComponentContext m_aContext
;
72 ::rtl::OUString m_sLoggerName
;
73 Reference
< XLogger
> m_xLogger
;
76 EventLogger_Impl( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rLoggerName
)
77 :m_aContext( _rxContext
)
78 ,m_sLoggerName( _rLoggerName
)
80 impl_createLogger_nothrow();
83 inline bool isValid() const { return m_xLogger
.is(); }
84 inline const ::rtl::OUString
& getName() const { return m_sLoggerName
; }
85 inline const Reference
< XLogger
>& getLogger() const { return m_xLogger
; }
86 inline const ::comphelper::ComponentContext
& getContext() const { return m_aContext
; }
89 void impl_createLogger_nothrow();
92 //====================================================================
93 //= EventLogger_Impl - implementation
94 //====================================================================
95 //--------------------------------------------------------------------
96 void EventLogger_Impl::impl_createLogger_nothrow()
100 Reference
< XLoggerPool
> xPool( LoggerPool::get( m_aContext
.getUNOContext() ), UNO_QUERY_THROW
);
101 if ( m_sLoggerName
.getLength() )
102 m_xLogger
= xPool
->getNamedLogger( m_sLoggerName
);
104 m_xLogger
= xPool
->getDefaultLogger();
106 catch( const Exception
& e
)
109 OSL_ENSURE( false, "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
113 //====================================================================
115 //====================================================================
116 //--------------------------------------------------------------------
117 EventLogger::EventLogger( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rLoggerName
)
118 :m_pImpl( new EventLogger_Impl( _rxContext
, _rLoggerName
) )
122 //--------------------------------------------------------------------
123 EventLogger::EventLogger( const Reference
< XComponentContext
>& _rxContext
, const sal_Char
* _pAsciiLoggerName
)
124 :m_pImpl( new EventLogger_Impl( _rxContext
, ::rtl::OUString::createFromAscii( _pAsciiLoggerName
) ) )
128 //--------------------------------------------------------------------
129 EventLogger::~EventLogger()
133 //--------------------------------------------------------------------
134 const ::rtl::OUString
& EventLogger::getName() const
136 return m_pImpl
->getName();
139 //--------------------------------------------------------------------
140 sal_Int32
EventLogger::getLogLevel() const
144 if ( m_pImpl
->isValid() )
145 return m_pImpl
->getLogger()->getLevel();
147 catch( const Exception
& e
)
150 OSL_ENSURE( false, "EventLogger::getLogLevel: caught an exception!" );
153 return LogLevel::OFF
;
156 //--------------------------------------------------------------------
157 void EventLogger::setLogLevel( const sal_Int32 _nLogLevel
) const
161 if ( m_pImpl
->isValid() )
162 m_pImpl
->getLogger()->setLevel( _nLogLevel
);
164 catch( const Exception
& e
)
167 OSL_ENSURE( false, "EventLogger::setLogLevel: caught an exception!" );
171 //--------------------------------------------------------------------
172 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel
) const
174 if ( !m_pImpl
->isValid() )
179 return m_pImpl
->getLogger()->isLoggable( _nLogLevel
);
181 catch( const Exception
& e
)
184 OSL_ENSURE( false, "EventLogger::isLoggable: caught an exception!" );
190 //--------------------------------------------------------------------
191 bool EventLogger::addLogHandler( const Reference
< XLogHandler
>& _rxLogHandler
)
195 if ( m_pImpl
->isValid() )
197 m_pImpl
->getLogger()->addLogHandler( _rxLogHandler
);
201 catch( const Exception
& e
)
204 OSL_ENSURE( false, "EventLogger::addLogHandler: caught an exception!" );
209 //--------------------------------------------------------------------
210 bool EventLogger::removeLogHandler( const Reference
< XLogHandler
>& _rxLogHandler
)
214 if ( m_pImpl
->isValid() )
216 m_pImpl
->getLogger()->removeLogHandler( _rxLogHandler
);
220 catch( const Exception
& e
)
223 OSL_ENSURE( false, "EventLogger::removeLogHandler: caught an exception!" );
228 //--------------------------------------------------------------------
231 void lcl_replaceParameter( ::rtl::OUString
& _inout_Message
, const ::rtl::OUString
& _rPlaceHolder
, const ::rtl::OUString
& _rReplacement
)
233 sal_Int32 nPlaceholderPosition
= _inout_Message
.indexOf( _rPlaceHolder
);
234 OSL_ENSURE( nPlaceholderPosition
>= 0, "lcl_replaceParameter: placeholder not found!" );
235 if ( nPlaceholderPosition
< 0 )
238 _inout_Message
= _inout_Message
.replaceAt( nPlaceholderPosition
, _rPlaceHolder
.getLength(), _rReplacement
);
242 //--------------------------------------------------------------------
243 bool EventLogger::impl_log( const sal_Int32 _nLogLevel
,
244 const sal_Char
* _pSourceClass
, const sal_Char
* _pSourceMethod
, const ::rtl::OUString
& _rMessage
,
245 const OptionalString
& _rArgument1
, const OptionalString
& _rArgument2
,
246 const OptionalString
& _rArgument3
, const OptionalString
& _rArgument4
,
247 const OptionalString
& _rArgument5
, const OptionalString
& _rArgument6
) const
249 // (if ::rtl::OUString had an indexOfAscii, we could save those ugly statics ...)
250 static ::rtl::OUString
sPH1( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$1$" ) ) );
251 static ::rtl::OUString
sPH2( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$2$" ) ) );
252 static ::rtl::OUString
sPH3( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$3$" ) ) );
253 static ::rtl::OUString
sPH4( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$4$" ) ) );
254 static ::rtl::OUString
sPH5( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$5$" ) ) );
255 static ::rtl::OUString
sPH6( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$6$" ) ) );
257 ::rtl::OUString
sMessage( _rMessage
);
259 lcl_replaceParameter( sMessage
, sPH1
, *_rArgument1
);
262 lcl_replaceParameter( sMessage
, sPH2
, *_rArgument2
);
265 lcl_replaceParameter( sMessage
, sPH3
, *_rArgument3
);
268 lcl_replaceParameter( sMessage
, sPH4
, *_rArgument4
);
271 lcl_replaceParameter( sMessage
, sPH5
, *_rArgument5
);
274 lcl_replaceParameter( sMessage
, sPH6
, *_rArgument6
);
278 Reference
< XLogger
> xLogger( m_pImpl
->getLogger() );
279 OSL_PRECOND( xLogger
.is(), "EventLogger::impl_log: should never be called without a logger!" );
280 if ( _pSourceClass
&& _pSourceMethod
)
284 ::rtl::OUString::createFromAscii( _pSourceClass
),
285 ::rtl::OUString::createFromAscii( _pSourceMethod
),
291 xLogger
->log( _nLogLevel
, sMessage
);
294 catch( const Exception
& e
)
297 OSL_ENSURE( false, "EventLogger::impl_log: caught an exception!" );
303 //====================================================================
304 //= ResourceBasedEventLogger_Data
305 //====================================================================
306 struct ResourceBasedEventLogger_Data
308 /// the base name of the resource bundle
309 ::rtl::OUString sBundleBaseName
;
310 /// did we already attempt to load the bundle?
312 /// the lazily loaded bundle
313 Reference
< XResourceBundle
> xBundle
;
315 ResourceBasedEventLogger_Data()
317 ,bBundleLoaded( false )
323 //--------------------------------------------------------------------
324 bool lcl_loadBundle_nothrow( const ComponentContext
& _rContext
, ResourceBasedEventLogger_Data
& _rLoggerData
)
326 if ( _rLoggerData
.bBundleLoaded
)
327 return _rLoggerData
.xBundle
.is();
329 // no matter what happens below, don't attempt creation ever again
330 _rLoggerData
.bBundleLoaded
= true;
334 Reference
< XResourceBundleLoader
> xLoader( _rContext
.getSingleton( "com.sun.star.resource.OfficeResourceLoader" ), UNO_QUERY_THROW
);
335 _rLoggerData
.xBundle
= Reference
< XResourceBundle
>( xLoader
->loadBundle_Default( _rLoggerData
.sBundleBaseName
), UNO_QUERY_THROW
);
337 catch( const Exception
& e
)
340 OSL_ENSURE( false, "lcl_loadBundle_nothrow: caught an exception!" );
343 return _rLoggerData
.xBundle
.is();
346 //--------------------------------------------------------------------
347 ::rtl::OUString
lcl_loadString_nothrow( const Reference
< XResourceBundle
>& _rxBundle
, const sal_Int32 _nMessageResID
)
349 OSL_PRECOND( _rxBundle
.is(), "lcl_loadString_nothrow: this will crash!" );
350 ::rtl::OUString sMessage
;
353 ::rtl::OUStringBuffer aBuffer
;
354 aBuffer
.appendAscii( "string:" );
355 aBuffer
.append( _nMessageResID
);
356 OSL_VERIFY( _rxBundle
->getDirectElement( aBuffer
.makeStringAndClear() ) >>= sMessage
);
358 catch( const Exception
& e
)
361 OSL_ENSURE( false, "lcl_loadString_nothrow: caught an exception!" );
366 //====================================================================
367 //= ResourceBasedEventLogger
368 //====================================================================
369 //--------------------------------------------------------------------
370 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rResourceBundleBaseName
,
371 const ::rtl::OUString
& _rLoggerName
)
372 :EventLogger( _rxContext
, _rLoggerName
)
373 ,m_pData( new ResourceBasedEventLogger_Data
)
375 m_pData
->sBundleBaseName
= _rResourceBundleBaseName
;
378 //--------------------------------------------------------------------
379 ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference
< XComponentContext
>& _rxContext
, const sal_Char
* _pResourceBundleBaseName
,
380 const sal_Char
* _pAsciiLoggerName
)
381 :EventLogger( _rxContext
, _pAsciiLoggerName
)
382 ,m_pData( new ResourceBasedEventLogger_Data
)
384 m_pData
->sBundleBaseName
= ::rtl::OUString::createFromAscii( _pResourceBundleBaseName
);
387 //--------------------------------------------------------------------
388 ::rtl::OUString
ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID
) const
390 ::rtl::OUString sMessage
;
391 if ( lcl_loadBundle_nothrow( m_pImpl
->getContext(), *m_pData
) )
392 sMessage
= lcl_loadString_nothrow( m_pData
->xBundle
, _nMessageResID
);
393 if ( sMessage
.getLength() == 0 )
395 ::rtl::OUStringBuffer aBuffer
;
396 aBuffer
.appendAscii( "<invalid event resource: '" );
397 aBuffer
.append( m_pData
->sBundleBaseName
);
398 aBuffer
.appendAscii( ":" );
399 aBuffer
.append( _nMessageResID
);
400 aBuffer
.appendAscii( "'>" );
401 sMessage
= aBuffer
.makeStringAndClear();
406 //........................................................................
407 } // namespace comphelper
408 //........................................................................