merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / logging / logger.cxx
blob599f481a24e59e30d83b608bde7751f7e96c2b65
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: logger.cxx,v $
10 * $Revision: 1.3 $
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_extensions.hxx"
34 #include "log_module.hxx"
35 #include "logrecord.hxx"
36 #include "loggerconfig.hxx"
38 /** === begin UNO includes === **/
39 #include <com/sun/star/logging/XLogger.hpp>
40 #include <com/sun/star/logging/LogLevel.hpp>
41 #include <com/sun/star/uno/XComponentContext.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/logging/XLoggerPool.hpp>
44 /** === end UNO includes === **/
46 #include <tools/diagnose_ex.h>
48 #include <comphelper/componentcontext.hxx>
50 #include <cppuhelper/basemutex.hxx>
51 #include <cppuhelper/interfacecontainer.hxx>
52 #include <cppuhelper/implbase2.hxx>
53 #include <cppuhelper/weakref.hxx>
55 #include <boost/bind.hpp>
57 #include <map>
59 //........................................................................
60 namespace logging
62 //........................................................................
64 /** === begin UNO using === **/
65 using ::com::sun::star::logging::XLogger;
66 using ::com::sun::star::uno::Reference;
67 using ::com::sun::star::uno::XComponentContext;
68 using ::com::sun::star::lang::XServiceInfo;
69 using ::com::sun::star::uno::RuntimeException;
70 using ::com::sun::star::uno::Sequence;
71 using ::com::sun::star::uno::XInterface;
72 using ::com::sun::star::uno::UNO_QUERY_THROW;
73 using ::com::sun::star::uno::Any;
74 using ::com::sun::star::uno::Exception;
75 using ::com::sun::star::uno::WeakReference;
76 using ::com::sun::star::logging::XLogHandler;
77 using ::com::sun::star::logging::XLoggerPool;
78 using ::com::sun::star::logging::LogRecord;
79 /** === end UNO using === **/
80 namespace LogLevel = ::com::sun::star::logging::LogLevel;
82 //====================================================================
83 //= helper
84 //====================================================================
85 namespace
87 sal_Bool lcl_supportsService_nothrow( XServiceInfo& _rSI, const ::rtl::OUString& _rServiceName )
89 const Sequence< ::rtl::OUString > aServiceNames( _rSI.getSupportedServiceNames() );
90 for ( const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
91 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
92 ++pServiceNames
94 if ( _rServiceName == *pServiceNames )
95 return sal_True;
96 return sal_False;
100 //====================================================================
101 //= EventLogger - declaration
102 //====================================================================
103 typedef ::cppu::WeakImplHelper2 < XLogger
104 , XServiceInfo
105 > EventLogger_Base;
106 class EventLogger :public ::cppu::BaseMutex
107 ,public EventLogger_Base
109 private:
110 ::comphelper::ComponentContext m_aContext;
111 ::cppu::OInterfaceContainerHelper m_aHandlers;
112 oslInterlockedCount m_nEventNumber;
114 // <attributes>
115 sal_Int32 m_nLogLevel;
116 ::rtl::OUString m_sName;
117 // </attributes>
119 public:
120 EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName );
122 // XServiceInfo
123 virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
124 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
125 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
127 // XLogger
128 virtual ::rtl::OUString SAL_CALL getName() throw (RuntimeException);
129 virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
130 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
131 virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
132 virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException);
133 virtual ::sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException);
134 virtual void SAL_CALL log( ::sal_Int32 Level, const ::rtl::OUString& Message ) throw (RuntimeException);
135 virtual void SAL_CALL logp( ::sal_Int32 Level, const ::rtl::OUString& SourceClass, const ::rtl::OUString& SourceMethod, const ::rtl::OUString& Message ) throw (RuntimeException);
137 protected:
138 ~EventLogger();
140 private:
141 /** logs the given log record
143 void impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
145 /** non-threadsafe impl-version of isLoggable
147 bool impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
150 //====================================================================
151 //= LoggerPool - declaration
152 //====================================================================
153 typedef ::cppu::WeakImplHelper2 < XLoggerPool
154 , XServiceInfo
155 > LoggerPool_Base;
156 /** administrates a pool of XLogger instances, where a logger is keyed by its name,
157 and subsequent requests for a logger with the same name return the same instance.
159 class LoggerPool : public LoggerPool_Base
161 private:
162 typedef ::std::map< ::rtl::OUString, WeakReference< XLogger > > ImplPool;
164 private:
165 ::osl::Mutex m_aMutex;
166 ::comphelper::ComponentContext m_aContext;
167 ImplPool m_aImpl;
169 public:
170 LoggerPool( const Reference< XComponentContext >& _rxContext );
172 // XServiceInfo
173 virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
174 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
175 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
177 // helper for factories
178 static Sequence< ::rtl::OUString > getSupportedServiceNames_static();
179 static ::rtl::OUString getImplementationName_static();
180 static ::rtl::OUString getSingletonName_static();
181 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
183 // XLoggerPool
184 virtual Reference< XLogger > SAL_CALL getNamedLogger( const ::rtl::OUString& Name ) throw (RuntimeException);
185 virtual Reference< XLogger > SAL_CALL getDefaultLogger( ) throw (RuntimeException);
188 //====================================================================
189 //= EventLogger - implementation
190 //====================================================================
191 //--------------------------------------------------------------------
192 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rName )
193 :m_aContext( _rxContext )
194 ,m_aHandlers( m_aMutex )
195 ,m_nEventNumber( 0 )
196 ,m_nLogLevel( LogLevel::OFF )
197 ,m_sName( _rName )
199 osl_incrementInterlockedCount( &m_refCount );
201 initializeLoggerFromConfiguration( m_aContext, this );
203 osl_decrementInterlockedCount( &m_refCount );
206 //--------------------------------------------------------------------
207 EventLogger::~EventLogger()
211 //--------------------------------------------------------------------
212 bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
214 if ( _nLevel < m_nLogLevel )
215 return false;
217 if ( !m_aHandlers.getLength() )
218 return false;
220 return true;
223 //--------------------------------------------------------------------
224 void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
226 ::osl::MutexGuard aGuard( m_aMutex );
228 if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
229 return;
231 m_aHandlers.forEach< XLogHandler >(
232 ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
235 //--------------------------------------------------------------------
236 ::rtl::OUString SAL_CALL EventLogger::getName() throw (RuntimeException)
238 return m_sName;
241 //--------------------------------------------------------------------
242 ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException)
244 ::osl::MutexGuard aGuard( m_aMutex );
245 return m_nLogLevel;
248 //--------------------------------------------------------------------
249 void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException)
251 ::osl::MutexGuard aGuard( m_aMutex );
252 m_nLogLevel = _level;
255 //--------------------------------------------------------------------
256 void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
258 if ( _rxLogHandler.is() )
259 m_aHandlers.addInterface( _rxLogHandler );
262 //--------------------------------------------------------------------
263 void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException)
265 if ( _rxLogHandler.is() )
266 m_aHandlers.removeInterface( _rxLogHandler );
269 //--------------------------------------------------------------------
270 ::sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException)
272 ::osl::MutexGuard aGuard( m_aMutex );
273 return impl_nts_isLoggable_nothrow( _nLevel );
276 //--------------------------------------------------------------------
277 void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
279 impl_ts_logEvent_nothrow( createLogRecord(
280 m_sName,
281 _rMessage,
282 _nLevel,
283 osl_incrementInterlockedCount( &m_nEventNumber )
284 ) );
287 //--------------------------------------------------------------------
288 void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const ::rtl::OUString& _rSourceClass, const ::rtl::OUString& _rSourceMethod, const ::rtl::OUString& _rMessage ) throw (RuntimeException)
290 impl_ts_logEvent_nothrow( createLogRecord(
291 m_sName,
292 _rSourceClass,
293 _rSourceMethod,
294 _rMessage,
295 _nLevel,
296 osl_incrementInterlockedCount( &m_nEventNumber )
297 ) );
300 //--------------------------------------------------------------------
301 ::rtl::OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException)
303 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.EventLogger" ) );
306 //--------------------------------------------------------------------
307 ::sal_Bool EventLogger::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
309 return lcl_supportsService_nothrow( *this, _rServiceName );
312 //--------------------------------------------------------------------
313 Sequence< ::rtl::OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException)
315 Sequence< ::rtl::OUString > aServiceNames(1);
316 aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.Logger" ) );
317 return aServiceNames;
320 //====================================================================
321 //= LoggerPool - implementation
322 //====================================================================
323 //--------------------------------------------------------------------
324 LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
325 :m_aContext( _rxContext )
329 //--------------------------------------------------------------------
330 ::rtl::OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException)
332 return getImplementationName_static();
335 //--------------------------------------------------------------------
336 ::sal_Bool SAL_CALL LoggerPool::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
338 return lcl_supportsService_nothrow( *this, _rServiceName );
341 //--------------------------------------------------------------------
342 Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException)
344 return getSupportedServiceNames_static();
347 //--------------------------------------------------------------------
348 ::rtl::OUString SAL_CALL LoggerPool::getImplementationName_static()
350 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.LoggerPool" ) );
353 //--------------------------------------------------------------------
354 Sequence< ::rtl::OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
356 Sequence< ::rtl::OUString > aServiceNames(1);
357 aServiceNames[0] = getSingletonName_static();
358 return aServiceNames;
361 //--------------------------------------------------------------------
362 ::rtl::OUString LoggerPool::getSingletonName_static()
364 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.LoggerPool" ) );
367 //--------------------------------------------------------------------
368 Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
370 return *( new LoggerPool( _rxContext ) );
373 //--------------------------------------------------------------------
374 Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const ::rtl::OUString& _rName ) throw (RuntimeException)
376 ::osl::MutexGuard aGuard( m_aMutex );
378 WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
379 Reference< XLogger > xLogger( (Reference< XLogger >)rLogger );
380 if ( !xLogger.is() )
382 // never requested before, or already dead
383 xLogger = new EventLogger( m_aContext.getUNOContext(), _rName );
384 rLogger = xLogger;
387 return xLogger;
390 //--------------------------------------------------------------------
391 Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger( ) throw (RuntimeException)
393 return getNamedLogger( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.logging.DefaultLogger" ) ) );
396 //--------------------------------------------------------------------
397 void createRegistryInfo_LoggerPool()
399 static OSingletonRegistration< LoggerPool > aAutoRegistration;
402 //........................................................................
403 } // namespace logging
404 //........................................................................