bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / logging / logger.cxx
blobd185ee652f49a2f3f545c49b8e826d1d08d54ae4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "log_module.hxx"
22 #include "log_services.hxx"
23 #include "logrecord.hxx"
24 #include "loggerconfig.hxx"
26 #include <com/sun/star/logging/XLogger.hpp>
27 #include <com/sun/star/logging/LogLevel.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/logging/XLoggerPool.hpp>
32 #include <boost/bind.hpp>
33 #include <cppuhelper/basemutex.hxx>
34 #include <cppuhelper/interfacecontainer.hxx>
35 #include <cppuhelper/implbase2.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <cppuhelper/weakref.hxx>
38 #include <map>
41 namespace logging
44 using ::com::sun::star::logging::XLogger;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::XComponentContext;
47 using ::com::sun::star::lang::XServiceInfo;
48 using ::com::sun::star::uno::RuntimeException;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::XInterface;
51 using ::com::sun::star::uno::UNO_QUERY_THROW;
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::uno::WeakReference;
55 using ::com::sun::star::logging::XLogHandler;
56 using ::com::sun::star::logging::XLoggerPool;
57 using ::com::sun::star::logging::LogRecord;
59 namespace LogLevel = ::com::sun::star::logging::LogLevel;
61 typedef ::cppu::WeakImplHelper2 < XLogger
62 , XServiceInfo
63 > EventLogger_Base;
64 class EventLogger :public ::cppu::BaseMutex
65 ,public EventLogger_Base
67 private:
68 ::cppu::OInterfaceContainerHelper m_aHandlers;
69 oslInterlockedCount m_nEventNumber;
71 // <attributes>
72 sal_Int32 m_nLogLevel;
73 OUString m_sName;
74 // </attributes>
76 public:
77 EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName );
79 // XServiceInfo
80 virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
81 virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
82 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
84 // XLogger
85 virtual OUString SAL_CALL getName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
89 virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 virtual sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
91 virtual void SAL_CALL log( ::sal_Int32 Level, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
92 virtual void SAL_CALL logp( ::sal_Int32 Level, const OUString& SourceClass, const OUString& SourceMethod, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
94 protected:
95 virtual ~EventLogger();
97 private:
98 /** logs the given log record
100 void impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
102 /** non-threadsafe impl-version of isLoggable
104 bool impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
107 typedef ::cppu::WeakImplHelper2 < XLoggerPool
108 , XServiceInfo
109 > LoggerPool_Base;
110 /** administrates a pool of XLogger instances, where a logger is keyed by its name,
111 and subsequent requests for a logger with the same name return the same instance.
113 class LoggerPool : public LoggerPool_Base
115 private:
116 typedef ::std::map< OUString, WeakReference< XLogger > > ImplPool;
118 private:
119 ::osl::Mutex m_aMutex;
120 Reference<XComponentContext> m_xContext;
121 ImplPool m_aImpl;
123 public:
124 LoggerPool( const Reference< XComponentContext >& _rxContext );
126 // XServiceInfo
127 virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
128 virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
129 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
131 // helper for factories
132 static Sequence< OUString > getSupportedServiceNames_static();
133 static OUString getImplementationName_static();
134 static OUString getSingletonName_static();
135 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
137 // XLoggerPool
138 virtual Reference< XLogger > SAL_CALL getNamedLogger( const OUString& Name ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
139 virtual Reference< XLogger > SAL_CALL getDefaultLogger( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
142 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName )
143 :m_aHandlers( m_aMutex )
144 ,m_nEventNumber( 0 )
145 ,m_nLogLevel( LogLevel::OFF )
146 ,m_sName( _rName )
148 osl_atomic_increment( &m_refCount );
150 initializeLoggerFromConfiguration( _rxContext, this );
152 osl_atomic_decrement( &m_refCount );
155 EventLogger::~EventLogger()
159 bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
161 if ( _nLevel < m_nLogLevel )
162 return false;
164 if ( !m_aHandlers.getLength() )
165 return false;
167 return true;
170 void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
172 ::osl::MutexGuard aGuard( m_aMutex );
174 if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
175 return;
177 m_aHandlers.forEach< XLogHandler >(
178 ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
179 m_aHandlers.forEach< XLogHandler >(
180 ::boost::bind( &XLogHandler::flush, _1 ) );
183 OUString SAL_CALL EventLogger::getName() throw (RuntimeException, std::exception)
185 return m_sName;
188 ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException, std::exception)
190 ::osl::MutexGuard aGuard( m_aMutex );
191 return m_nLogLevel;
194 void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception)
196 ::osl::MutexGuard aGuard( m_aMutex );
197 m_nLogLevel = _level;
200 void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
202 if ( _rxLogHandler.is() )
203 m_aHandlers.addInterface( _rxLogHandler );
206 void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
208 if ( _rxLogHandler.is() )
209 m_aHandlers.removeInterface( _rxLogHandler );
212 sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception)
214 ::osl::MutexGuard aGuard( m_aMutex );
215 return impl_nts_isLoggable_nothrow( _nLevel );
218 void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const OUString& _rMessage ) throw (RuntimeException, std::exception)
220 impl_ts_logEvent_nothrow( createLogRecord(
221 m_sName,
222 _rMessage,
223 _nLevel,
224 osl_atomic_increment( &m_nEventNumber )
225 ) );
228 void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const OUString& _rSourceClass, const OUString& _rSourceMethod, const OUString& _rMessage ) throw (RuntimeException, std::exception)
230 impl_ts_logEvent_nothrow( createLogRecord(
231 m_sName,
232 _rSourceClass,
233 _rSourceMethod,
234 _rMessage,
235 _nLevel,
236 osl_atomic_increment( &m_nEventNumber )
237 ) );
240 OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException, std::exception)
242 return OUString( "com.sun.star.comp.extensions.EventLogger" );
245 sal_Bool EventLogger::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
247 return cppu::supportsService(this, _rServiceName);
250 Sequence< OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException, std::exception)
252 Sequence< OUString > aServiceNames(1);
253 aServiceNames[0] = "com.sun.star.logging.Logger";
254 return aServiceNames;
257 LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
258 :m_xContext( _rxContext )
262 OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException, std::exception)
264 return getImplementationName_static();
267 sal_Bool SAL_CALL LoggerPool::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
269 return cppu::supportsService(this, _rServiceName);
272 Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException, std::exception)
274 return getSupportedServiceNames_static();
277 OUString SAL_CALL LoggerPool::getImplementationName_static()
279 return OUString( "com.sun.star.comp.extensions.LoggerPool" );
282 Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
284 Sequence< OUString > aServiceNames(1);
285 aServiceNames[0] = getSingletonName_static();
286 return aServiceNames;
289 OUString LoggerPool::getSingletonName_static()
291 return OUString( "com.sun.star.logging.LoggerPool" );
294 Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
296 return *( new LoggerPool( _rxContext ) );
299 Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const OUString& _rName ) throw (RuntimeException, std::exception)
301 ::osl::MutexGuard aGuard( m_aMutex );
303 WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
304 Reference< XLogger > xLogger( rLogger );
305 if ( !xLogger.is() )
307 // never requested before, or already dead
308 xLogger = new EventLogger( m_xContext, _rName );
309 rLogger = xLogger;
312 return xLogger;
315 Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger( ) throw (RuntimeException, std::exception)
317 return getNamedLogger( OUString( "org.openoffice.logging.DefaultLogger" ) );
320 void createRegistryInfo_LoggerPool()
322 static OSingletonRegistration< LoggerPool > aAutoRegistration;
325 } // namespace logging
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */