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: logger.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_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>
59 //........................................................................
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 //====================================================================
84 //====================================================================
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();
94 if ( _rServiceName
== *pServiceNames
)
100 //====================================================================
101 //= EventLogger - declaration
102 //====================================================================
103 typedef ::cppu::WeakImplHelper2
< XLogger
106 class EventLogger
:public ::cppu::BaseMutex
107 ,public EventLogger_Base
110 ::comphelper::ComponentContext m_aContext
;
111 ::cppu::OInterfaceContainerHelper m_aHandlers
;
112 oslInterlockedCount m_nEventNumber
;
115 sal_Int32 m_nLogLevel
;
116 ::rtl::OUString m_sName
;
120 EventLogger( const Reference
< XComponentContext
>& _rxContext
, const ::rtl::OUString
& _rName
);
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
);
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
);
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
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
162 typedef ::std::map
< ::rtl::OUString
, WeakReference
< XLogger
> > ImplPool
;
165 ::osl::Mutex m_aMutex
;
166 ::comphelper::ComponentContext m_aContext
;
170 LoggerPool( const Reference
< XComponentContext
>& _rxContext
);
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
);
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
)
196 ,m_nLogLevel( LogLevel::OFF
)
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
)
217 if ( !m_aHandlers
.getLength() )
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
) )
231 m_aHandlers
.forEach
< XLogHandler
>(
232 ::boost::bind( &XLogHandler::publish
, _1
, ::boost::cref( _rRecord
) ) );
235 //--------------------------------------------------------------------
236 ::rtl::OUString SAL_CALL
EventLogger::getName() throw (RuntimeException
)
241 //--------------------------------------------------------------------
242 ::sal_Int32 SAL_CALL
EventLogger::getLevel() throw (RuntimeException
)
244 ::osl::MutexGuard
aGuard( m_aMutex
);
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(
283 osl_incrementInterlockedCount( &m_nEventNumber
)
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(
296 osl_incrementInterlockedCount( &m_nEventNumber
)
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
);
382 // never requested before, or already dead
383 xLogger
= new EventLogger( m_aContext
.getUNOContext(), _rName
);
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 //........................................................................