Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "log_module.hxx"
22 : #include "logrecord.hxx"
23 : #include "loggerconfig.hxx"
24 :
25 : #include <com/sun/star/logging/XLogger.hpp>
26 : #include <com/sun/star/logging/LogLevel.hpp>
27 : #include <com/sun/star/uno/XComponentContext.hpp>
28 : #include <com/sun/star/lang/XServiceInfo.hpp>
29 : #include <com/sun/star/logging/XLoggerPool.hpp>
30 :
31 : #include <boost/bind.hpp>
32 : #include <cppuhelper/basemutex.hxx>
33 : #include <cppuhelper/interfacecontainer.hxx>
34 : #include <cppuhelper/implbase2.hxx>
35 : #include <cppuhelper/supportsservice.hxx>
36 : #include <cppuhelper/weakref.hxx>
37 : #include <map>
38 :
39 :
40 : namespace logging
41 : {
42 :
43 : using ::com::sun::star::logging::XLogger;
44 : using ::com::sun::star::uno::Reference;
45 : using ::com::sun::star::uno::XComponentContext;
46 : using ::com::sun::star::lang::XServiceInfo;
47 : using ::com::sun::star::uno::RuntimeException;
48 : using ::com::sun::star::uno::Sequence;
49 : using ::com::sun::star::uno::XInterface;
50 : using ::com::sun::star::uno::UNO_QUERY_THROW;
51 : using ::com::sun::star::uno::Any;
52 : using ::com::sun::star::uno::Exception;
53 : using ::com::sun::star::uno::WeakReference;
54 : using ::com::sun::star::logging::XLogHandler;
55 : using ::com::sun::star::logging::XLoggerPool;
56 : using ::com::sun::star::logging::LogRecord;
57 :
58 : namespace LogLevel = ::com::sun::star::logging::LogLevel;
59 :
60 : typedef ::cppu::WeakImplHelper2 < XLogger
61 : , XServiceInfo
62 : > EventLogger_Base;
63 : class EventLogger :public ::cppu::BaseMutex
64 : ,public EventLogger_Base
65 : {
66 : private:
67 : ::cppu::OInterfaceContainerHelper m_aHandlers;
68 : oslInterlockedCount m_nEventNumber;
69 :
70 : // <attributes>
71 : sal_Int32 m_nLogLevel;
72 : OUString m_sName;
73 : // </attributes>
74 :
75 : public:
76 : EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName );
77 :
78 : // XServiceInfo
79 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
80 : virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
81 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
82 :
83 : // XLogger
84 : virtual OUString SAL_CALL getName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 : virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 : virtual void SAL_CALL log( ::sal_Int32 Level, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
91 : virtual void SAL_CALL logp( ::sal_Int32 Level, const OUString& SourceClass, const OUString& SourceMethod, const OUString& Message ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
92 :
93 : protected:
94 : virtual ~EventLogger();
95 :
96 : private:
97 : /** logs the given log record
98 : */
99 : void impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
100 :
101 : /** non-threadsafe impl-version of isLoggable
102 : */
103 : bool impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
104 : };
105 :
106 : typedef ::cppu::WeakImplHelper2 < XLoggerPool
107 : , XServiceInfo
108 : > LoggerPool_Base;
109 : /** administrates a pool of XLogger instances, where a logger is keyed by its name,
110 : and subsequent requests for a logger with the same name return the same instance.
111 : */
112 0 : class LoggerPool : public LoggerPool_Base
113 : {
114 : private:
115 : typedef ::std::map< OUString, WeakReference< XLogger > > ImplPool;
116 :
117 : private:
118 : ::osl::Mutex m_aMutex;
119 : Reference<XComponentContext> m_xContext;
120 : ImplPool m_aImpl;
121 :
122 : public:
123 : LoggerPool( const Reference< XComponentContext >& _rxContext );
124 :
125 : // XServiceInfo
126 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
127 : virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
128 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
129 :
130 : // helper for factories
131 : static Sequence< OUString > getSupportedServiceNames_static();
132 : static OUString getImplementationName_static();
133 : static OUString getSingletonName_static();
134 : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
135 :
136 : // XLoggerPool
137 : virtual Reference< XLogger > SAL_CALL getNamedLogger( const OUString& Name ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
138 : virtual Reference< XLogger > SAL_CALL getDefaultLogger( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
139 : };
140 :
141 0 : EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const OUString& _rName )
142 : :m_aHandlers( m_aMutex )
143 : ,m_nEventNumber( 0 )
144 : ,m_nLogLevel( LogLevel::OFF )
145 0 : ,m_sName( _rName )
146 : {
147 0 : osl_atomic_increment( &m_refCount );
148 : {
149 0 : initializeLoggerFromConfiguration( _rxContext, this );
150 : }
151 0 : osl_atomic_decrement( &m_refCount );
152 0 : }
153 :
154 0 : EventLogger::~EventLogger()
155 : {
156 0 : }
157 :
158 0 : bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
159 : {
160 0 : if ( _nLevel < m_nLogLevel )
161 0 : return false;
162 :
163 0 : if ( !m_aHandlers.getLength() )
164 0 : return false;
165 :
166 0 : return true;
167 : }
168 :
169 0 : void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
170 : {
171 0 : ::osl::MutexGuard aGuard( m_aMutex );
172 :
173 0 : if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
174 0 : return;
175 :
176 : m_aHandlers.forEach< XLogHandler >(
177 0 : ::boost::bind( &XLogHandler::publish, _1, ::boost::cref( _rRecord ) ) );
178 : m_aHandlers.forEach< XLogHandler >(
179 0 : ::boost::bind( &XLogHandler::flush, _1 ) );
180 : }
181 :
182 0 : OUString SAL_CALL EventLogger::getName() throw (RuntimeException, std::exception)
183 : {
184 0 : return m_sName;
185 : }
186 :
187 0 : ::sal_Int32 SAL_CALL EventLogger::getLevel() throw (RuntimeException, std::exception)
188 : {
189 0 : ::osl::MutexGuard aGuard( m_aMutex );
190 0 : return m_nLogLevel;
191 : }
192 :
193 0 : void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception)
194 : {
195 0 : ::osl::MutexGuard aGuard( m_aMutex );
196 0 : m_nLogLevel = _level;
197 0 : }
198 :
199 0 : void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
200 : {
201 0 : if ( _rxLogHandler.is() )
202 0 : m_aHandlers.addInterface( _rxLogHandler );
203 0 : }
204 :
205 0 : void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler ) throw (RuntimeException, std::exception)
206 : {
207 0 : if ( _rxLogHandler.is() )
208 0 : m_aHandlers.removeInterface( _rxLogHandler );
209 0 : }
210 :
211 0 : sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception)
212 : {
213 0 : ::osl::MutexGuard aGuard( m_aMutex );
214 0 : return impl_nts_isLoggable_nothrow( _nLevel );
215 : }
216 :
217 0 : void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const OUString& _rMessage ) throw (RuntimeException, std::exception)
218 : {
219 : impl_ts_logEvent_nothrow( createLogRecord(
220 : m_sName,
221 : _rMessage,
222 : _nLevel,
223 0 : osl_atomic_increment( &m_nEventNumber )
224 0 : ) );
225 0 : }
226 :
227 0 : void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const OUString& _rSourceClass, const OUString& _rSourceMethod, const OUString& _rMessage ) throw (RuntimeException, std::exception)
228 : {
229 : impl_ts_logEvent_nothrow( createLogRecord(
230 : m_sName,
231 : _rSourceClass,
232 : _rSourceMethod,
233 : _rMessage,
234 : _nLevel,
235 0 : osl_atomic_increment( &m_nEventNumber )
236 0 : ) );
237 0 : }
238 :
239 0 : OUString SAL_CALL EventLogger::getImplementationName() throw(RuntimeException, std::exception)
240 : {
241 0 : return OUString( "com.sun.star.comp.extensions.EventLogger" );
242 : }
243 :
244 0 : sal_Bool EventLogger::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
245 : {
246 0 : return cppu::supportsService(this, _rServiceName);
247 : }
248 :
249 0 : Sequence< OUString > SAL_CALL EventLogger::getSupportedServiceNames() throw(RuntimeException, std::exception)
250 : {
251 0 : Sequence< OUString > aServiceNames(1);
252 0 : aServiceNames[0] = "com.sun.star.logging.Logger";
253 0 : return aServiceNames;
254 : }
255 :
256 0 : LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
257 0 : :m_xContext( _rxContext )
258 : {
259 0 : }
260 :
261 0 : OUString SAL_CALL LoggerPool::getImplementationName() throw(RuntimeException, std::exception)
262 : {
263 0 : return getImplementationName_static();
264 : }
265 :
266 0 : sal_Bool SAL_CALL LoggerPool::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
267 : {
268 0 : return cppu::supportsService(this, _rServiceName);
269 : }
270 :
271 0 : Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames() throw(RuntimeException, std::exception)
272 : {
273 0 : return getSupportedServiceNames_static();
274 : }
275 :
276 0 : OUString SAL_CALL LoggerPool::getImplementationName_static()
277 : {
278 0 : return OUString( "com.sun.star.comp.extensions.LoggerPool" );
279 : }
280 :
281 0 : Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames_static()
282 : {
283 0 : Sequence< OUString > aServiceNames(1);
284 0 : aServiceNames[0] = getSingletonName_static();
285 0 : return aServiceNames;
286 : }
287 :
288 0 : OUString LoggerPool::getSingletonName_static()
289 : {
290 0 : return OUString( "com.sun.star.logging.LoggerPool" );
291 : }
292 :
293 0 : Reference< XInterface > SAL_CALL LoggerPool::Create( const Reference< XComponentContext >& _rxContext )
294 : {
295 0 : return *( new LoggerPool( _rxContext ) );
296 : }
297 :
298 0 : Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const OUString& _rName ) throw (RuntimeException, std::exception)
299 : {
300 0 : ::osl::MutexGuard aGuard( m_aMutex );
301 :
302 0 : WeakReference< XLogger >& rLogger( m_aImpl[ _rName ] );
303 0 : Reference< XLogger > xLogger( (Reference< XLogger >)rLogger );
304 0 : if ( !xLogger.is() )
305 : {
306 : // never requested before, or already dead
307 0 : xLogger = new EventLogger( m_xContext, _rName );
308 0 : rLogger = xLogger;
309 : }
310 :
311 0 : return xLogger;
312 : }
313 :
314 0 : Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger( ) throw (RuntimeException, std::exception)
315 : {
316 0 : return getNamedLogger( OUString( "org.openoffice.logging.DefaultLogger" ) );
317 : }
318 :
319 0 : void createRegistryInfo_LoggerPool()
320 : {
321 0 : static OSingletonRegistration< LoggerPool > aAutoRegistration;
322 0 : }
323 :
324 0 : } // namespace logging
325 :
326 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|