masterfix DEV300: #i10000# build fix
[LibreOffice.git] / extensions / source / logging / logger.cxx
blob7472259373568f45a4abd6dea6929cffe477a5c2
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_extensions.hxx"
31 #include "log_module.hxx"
32 #include "logrecord.hxx"
33 #include "loggerconfig.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/logging/XLogger.hpp>
37 #include <com/sun/star/logging/LogLevel.hpp>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/logging/XLoggerPool.hpp>
41 /** === end UNO includes === **/
43 #include <tools/diagnose_ex.h>
45 #include <comphelper/componentcontext.hxx>
47 #include <cppuhelper/basemutex.hxx>
48 #include <cppuhelper/interfacecontainer.hxx>
49 #include <cppuhelper/implbase2.hxx>
50 #include <cppuhelper/weakref.hxx>
52 #include <boost/bind.hpp>
54 #include <map>
56 //........................................................................
57 namespace logging
59 //........................................................................
61 /** === begin UNO using === **/
62 using ::com::sun::star::logging::XLogger;
63 using ::com::sun::star::uno::Reference;
64 using ::com::sun::star::uno::XComponentContext;
65 using ::com::sun::star::lang::XServiceInfo;
66 using ::com::sun::star::uno::RuntimeException;
67 using ::com::sun::star::uno::Sequence;
68 using ::com::sun::star::uno::XInterface;
69 using ::com::sun::star::uno::UNO_QUERY_THROW;
70 using ::com::sun::star::uno::Any;
71 using ::com::sun::star::uno::Exception;
72 using ::com::sun::star::uno::WeakReference;
73 using ::com::sun::star::logging::XLogHandler;
74 using ::com::sun::star::logging::XLoggerPool;
75 using ::com::sun::star::logging::LogRecord;
76 /** === end UNO using === **/
77 namespace LogLevel = ::com::sun::star::logging::LogLevel;
79 //====================================================================
80 //= helper
81 //====================================================================
82 namespace
84 sal_Bool lcl_supportsService_nothrow( XServiceInfo& _rSI, const ::rtl::OUString& _rServiceName )
86 const Sequence< ::rtl::OUString > aServiceNames( _rSI.getSupportedServiceNames() );
87 for ( const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
88 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
89 ++pServiceNames
91 if ( _rServiceName == *pServiceNames )
92 return sal_True;
93 return sal_False;
97 //====================================================================
98 //= EventLogger - declaration
99 //====================================================================
100 typedef ::cppu::WeakImplHelper2 < XLogger
101 , XServiceInfo
102 > EventLogger_Base;
103 class EventLogger :public ::cppu::BaseMutex
104 ,public EventLogger_Base
106 private:
107 ::comphelper::ComponentContext m_aContext;
108 ::cppu::OInterfaceContainerHelper m_aHandlers;
109 oslInterlockedCount m_nEventNumber;
111 // <attributes>
112 sal_Int32 m_nLogLevel;
113 ::rtl::OUString m_sName;
114 // </attributes>
116 public:
117 EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName );
119 // XServiceInfo
120 virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
121 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
122 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
124 // XLogger
125 virtual ::rtl::OUString SAL_CALL getName() throw (RuntimeException);
126 virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
127 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
128 virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
129 virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
130 virtual ::sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException);
131 virtual void SAL_CALL log( ::sal_Int32 Level, const ::rtl::OUString& Message ) throw (RuntimeException);
132 virtual void SAL_CALL logp( ::sal_Int32 Level, const ::rtl::OUString& SourceClass, const ::rtl::OUString& SourceMethod, const ::rtl::OUString& Message ) throw (RuntimeException);
134 protected:
135 ~EventLogger();
137 private:
138 /** logs the given log record
140 void impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
142 /** non-threadsafe impl-version of isLoggable
144 bool impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
147 //====================================================================
148 //= LoggerPool - declaration
149 //====================================================================
150 typedef ::cppu::WeakImplHelper2 < XLoggerPool
151 , XServiceInfo
152 > LoggerPool_Base;
153 /** administrates a pool of XLogger instances, where a logger is keyed by its name,
154 and subsequent requests for a logger with the same name return the same instance.
156 class LoggerPool : public LoggerPool_Base
158 private:
159 typedef ::std::map< ::rtl::OUString, WeakReference< XLogger > > ImplPool;
161 private:
162 ::osl::Mutex m_aMutex;
163 ::comphelper::ComponentContext m_aContext;
164 ImplPool m_aImpl;
166 public:
167 LoggerPool( const Reference< XComponentContext >& _rxContext );
169 // XServiceInfo
170 virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
171 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
172 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
174 // helper for factories
175 static Sequence< ::rtl::OUString > getSupportedServiceNames_static();
176 static ::rtl::OUString getImplementationName_static();
177 static ::rtl::OUString getSingletonName_static();
178 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
180 // XLoggerPool
181 virtual Reference< XLogger > SAL_CALL getNamedLogger( const ::rtl::OUString& Name ) throw (RuntimeException);
182 virtual Reference< XLogger > SAL_CALL getDefaultLogger( ) throw (RuntimeException);
185 //====================================================================
186 //= EventLogger - implementation
187 //====================================================================
188 //--------------------------------------------------------------------
189 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName )
190 :m_aContext( _rxContext )
191 ,m_aHandlers( m_aMutex )
192 ,m_nEventNumber( 0 )
193 ,m_nLogLevel( LogLevel::OFF )
194 ,m_sName( _rName )
196 osl_incrementInterlockedCount( &m_refCount );
198 initializeLoggerFromConfiguration( m_aContext, this );
200 osl_decrementInterlockedCount( &m_refCount );
203 //--------------------------------------------------------------------
204 EventLogger::~EventLogger()
208 //--------------------------------------------------------------------
209 bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
211 if ( _nLevel < m_nLogLevel )
212 return false;
214 if ( !m_aHandlers.getLength() )
215 return false;
217 return true;
220 //--------------------------------------------------------------------
221 void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
223 ::osl::MutexGuard aGuard( m_aMutex );
225 if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
226 return;
228 m_aHandlers.forEach< XLogHandler >(
229 ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
230 m_aHandlers.forEach< XLogHandler >(
231 ::boost::bind( &XLogHandler::flush, _1 ) );
234 //--------------------------------------------------------------------
235 ::rtl::OUString SAL_CALL EventLogger::getName() throw (RuntimeException)
237 return m_sName;
240 //--------------------------------------------------------------------
241 ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException)
243 ::osl::MutexGuard aGuard( m_aMutex );
244 return m_nLogLevel;
247 //--------------------------------------------------------------------
248 void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException)
250 ::osl::MutexGuard aGuard( m_aMutex );
251 m_nLogLevel = _level;
254 //--------------------------------------------------------------------
255 void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
257 if ( _rxLogHandler.is() )
258 m_aHandlers.addInterface( _rxLogHandler );
261 //--------------------------------------------------------------------
262 void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
264 if ( _rxLogHandler.is() )
265 m_aHandlers.removeInterface( _rxLogHandler );
268 //--------------------------------------------------------------------
269 ::sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException)
271 ::osl::MutexGuard aGuard( m_aMutex );
272 return impl_nts_isLoggable_nothrow( _nLevel );
275 //--------------------------------------------------------------------
276 void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
278 impl_ts_logEvent_nothrow( createLogRecord(
279 m_sName,
280 _rMessage,
281 _nLevel,
282 osl_incrementInterlockedCount( &m_nEventNumber )
283 ) );
286 //--------------------------------------------------------------------
287 void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const ::rtl::OUString& _rSourceClass, const ::rtl::OUString& _rSourceMethod, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
289 impl_ts_logEvent_nothrow( createLogRecord(
290 m_sName,
291 _rSourceClass,
292 _rSourceMethod,
293 _rMessage,
294 _nLevel,
295 osl_incrementInterlockedCount( &m_nEventNumber )
296 ) );
299 //--------------------------------------------------------------------
300 ::rtl::OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException)
302 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.EventLogger" ) );
305 //--------------------------------------------------------------------
306 ::sal_Bool EventLogger::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
308 return lcl_supportsService_nothrow( *this, _rServiceName );
311 //--------------------------------------------------------------------
312 Sequence< ::rtl::OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException)
314 Sequence< ::rtl::OUString > aServiceNames(1);
315 aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.Logger" ) );
316 return aServiceNames;
319 //====================================================================
320 //= LoggerPool - implementation
321 //====================================================================
322 //--------------------------------------------------------------------
323 LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
324 :m_aContext( _rxContext )
328 //--------------------------------------------------------------------
329 ::rtl::OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException)
331 return getImplementationName_static();
334 //--------------------------------------------------------------------
335 ::sal_Bool SAL_CALL LoggerPool::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
337 return lcl_supportsService_nothrow( *this, _rServiceName );
340 //--------------------------------------------------------------------
341 Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException)
343 return getSupportedServiceNames_static();
346 //--------------------------------------------------------------------
347 ::rtl::OUString SAL_CALL LoggerPool::getImplementationName_static()
349 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.LoggerPool" ) );
352 //--------------------------------------------------------------------
353 Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
355 Sequence< ::rtl::OUString > aServiceNames(1);
356 aServiceNames[0] = getSingletonName_static();
357 return aServiceNames;
360 //--------------------------------------------------------------------
361 ::rtl::OUString LoggerPool::getSingletonName_static()
363 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.LoggerPool" ) );
366 //--------------------------------------------------------------------
367 Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
369 return *( new LoggerPool( _rxContext ) );
372 //--------------------------------------------------------------------
373 Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const ::rtl::OUString& _rName ) throw (RuntimeException)
375 ::osl::MutexGuard aGuard( m_aMutex );
377 WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
378 Reference< XLogger > xLogger( (Reference< XLogger >)rLogger );
379 if ( !xLogger.is() )
381 // never requested before, or already dead
382 xLogger = new EventLogger( m_aContext.getUNOContext(), _rName );
383 rLogger = xLogger;
386 return xLogger;
389 //--------------------------------------------------------------------
390 Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger( ) throw (RuntimeException)
392 return getNamedLogger( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.logging.DefaultLogger" ) ) );
395 //--------------------------------------------------------------------
396 void createRegistryInfo_LoggerPool()
398 static OSingletonRegistration< LoggerPool > aAutoRegistration;
401 //........................................................................
402 } // namespace logging
403 //........................................................................