LibreOffice Module extensions (master) 1
logger.cxx
Go to the documentation of this file.
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 "logrecord.hxx"
22#include "loggerconfig.hxx"
23
24#include <com/sun/star/logging/XLogger.hpp>
25#include <com/sun/star/logging/LogLevel.hpp>
26#include <com/sun/star/uno/XComponentContext.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/logging/XLoggerPool.hpp>
29
35#include <map>
36#include <utility>
37
38
39namespace logging
40{
41
42 using ::com::sun::star::logging::XLogger;
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::uno::XComponentContext;
45 using ::com::sun::star::lang::XServiceInfo;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::uno::XInterface;
48 using ::com::sun::star::uno::WeakReference;
49 using ::com::sun::star::logging::XLogHandler;
50 using ::com::sun::star::logging::LogRecord;
51
52 namespace {
53
54 class EventLogger : public cppu::BaseMutex,
55 public cppu::WeakImplHelper<css::logging::XLogger>
56 {
57 private:
59 oslInterlockedCount m_nEventNumber;
60
61 // <attributes>
62 sal_Int32 m_nLogLevel;
63 OUString m_sName;
64 // </attributes>
65
66 public:
67 EventLogger( const Reference< XComponentContext >& _rxContext, OUString _aName );
68
69 // XLogger
70 virtual OUString SAL_CALL getName() override;
71 virtual ::sal_Int32 SAL_CALL getLevel() override;
72 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) override;
73 virtual void SAL_CALL addLogHandler( const Reference< XLogHandler >& LogHandler ) override;
74 virtual void SAL_CALL removeLogHandler( const Reference< XLogHandler >& LogHandler ) override;
75 virtual sal_Bool SAL_CALL isLoggable( ::sal_Int32 _nLevel ) override;
76 virtual void SAL_CALL log( ::sal_Int32 Level, const OUString& Message ) override;
77 virtual void SAL_CALL logp( ::sal_Int32 Level, const OUString& SourceClass, const OUString& SourceMethod, const OUString& Message ) override;
78
79 protected:
80 virtual ~EventLogger() override;
81
82 private:
85 void impl_ts_logEvent_nothrow( const LogRecord& _rRecord );
86
89 bool impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel );
90 };
91
95 class LoggerPool : public cppu::WeakImplHelper<css::logging::XLoggerPool, XServiceInfo>
96 {
97 private:
98 ::osl::Mutex m_aMutex;
99 Reference<XComponentContext> m_xContext;
100 std::map< OUString, WeakReference<XLogger> > m_aLoggerMap;
101
102 public:
103 explicit LoggerPool( const Reference< XComponentContext >& _rxContext );
104
105 // XServiceInfo
106 virtual OUString SAL_CALL getImplementationName() override;
107 virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) override;
108 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
109
110 // XLoggerPool
111 virtual Reference< XLogger > SAL_CALL getNamedLogger( const OUString& Name ) override;
112 virtual Reference< XLogger > SAL_CALL getDefaultLogger( ) override;
113 };
114
115 }
116
117 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, OUString _aName )
119 ,m_nEventNumber( 0 )
120 ,m_nLogLevel( css::logging::LogLevel::OFF )
121 ,m_sName(std::move( _aName ))
122 {
123 osl_atomic_increment( &m_refCount );
124 {
125 initializeLoggerFromConfiguration( _rxContext, this );
126 }
127 osl_atomic_decrement( &m_refCount );
128 }
129
130 EventLogger::~EventLogger()
131 {
132 }
133
134 bool EventLogger::impl_nts_isLoggable_nothrow( ::sal_Int32 _nLevel )
135 {
136 if ( _nLevel < m_nLogLevel )
137 return false;
138
139 if ( !m_aHandlers.getLength() )
140 return false;
141
142 return true;
143 }
144
145 void EventLogger::impl_ts_logEvent_nothrow( const LogRecord& _rRecord )
146 {
147 ::osl::MutexGuard aGuard( m_aMutex );
148
149 if ( !impl_nts_isLoggable_nothrow( _rRecord.Level ) )
150 return;
151
152 m_aHandlers.forEach< XLogHandler >(
153 [&_rRecord] (Reference<XLogHandler> const& rxListener) { rxListener->publish(_rRecord); } );
154 m_aHandlers.forEach< XLogHandler >(
155 [] (Reference<XLogHandler> const& rxListener) { rxListener->flush(); } );
156 }
157
158 OUString SAL_CALL EventLogger::getName()
159 {
160 return m_sName;
161 }
162
163 ::sal_Int32 SAL_CALL EventLogger::getLevel()
164 {
165 ::osl::MutexGuard aGuard( m_aMutex );
166 return m_nLogLevel;
167 }
168
169 void SAL_CALL EventLogger::setLevel( ::sal_Int32 _level )
170 {
171 ::osl::MutexGuard aGuard( m_aMutex );
172 m_nLogLevel = _level;
173 }
174
175 void SAL_CALL EventLogger::addLogHandler( const Reference< XLogHandler >& _rxLogHandler )
176 {
177 if ( _rxLogHandler.is() )
178 m_aHandlers.addInterface( _rxLogHandler );
179 }
180
181 void SAL_CALL EventLogger::removeLogHandler( const Reference< XLogHandler >& _rxLogHandler )
182 {
183 if ( _rxLogHandler.is() )
184 m_aHandlers.removeInterface( _rxLogHandler );
185 }
186
187 sal_Bool SAL_CALL EventLogger::isLoggable( ::sal_Int32 _nLevel )
188 {
189 ::osl::MutexGuard aGuard( m_aMutex );
190 return impl_nts_isLoggable_nothrow( _nLevel );
191 }
192
193 void SAL_CALL EventLogger::log( ::sal_Int32 _nLevel, const OUString& _rMessage )
194 {
195 impl_ts_logEvent_nothrow( createLogRecord(
196 m_sName,
197 _rMessage,
198 _nLevel,
199 osl_atomic_increment( &m_nEventNumber )
200 ) );
201 }
202
203 void SAL_CALL EventLogger::logp( ::sal_Int32 _nLevel, const OUString& _rSourceClass, const OUString& _rSourceMethod, const OUString& _rMessage )
204 {
205 impl_ts_logEvent_nothrow( createLogRecord(
206 m_sName,
207 _rSourceClass,
208 _rSourceMethod,
209 _rMessage,
210 _nLevel,
211 osl_atomic_increment( &m_nEventNumber )
212 ) );
213 }
214
215 LoggerPool::LoggerPool( const Reference< XComponentContext >& _rxContext )
216 :m_xContext( _rxContext )
217 {
218 }
219
220 OUString SAL_CALL LoggerPool::getImplementationName()
221 {
222 return "com.sun.star.comp.extensions.LoggerPool";
223 }
224
225 sal_Bool SAL_CALL LoggerPool::supportsService( const OUString& _rServiceName )
226 {
227 return cppu::supportsService(this, _rServiceName);
228 }
229
230 Sequence< OUString > SAL_CALL LoggerPool::getSupportedServiceNames()
231 {
232 return { "com.sun.star.logging.LoggerPool" };
233 }
234
235 Reference< XLogger > SAL_CALL LoggerPool::getNamedLogger( const OUString& _rName )
236 {
237 ::osl::MutexGuard aGuard( m_aMutex );
238
239 WeakReference< XLogger >& rLogger( m_aLoggerMap[ _rName ] );
240 Reference< XLogger > xLogger( rLogger );
241 if ( !xLogger.is() )
242 {
243 // never requested before, or already dead
244 xLogger = new EventLogger( m_xContext, _rName );
245 rLogger = xLogger;
246 }
247
248 return xLogger;
249 }
250
251 Reference< XLogger > SAL_CALL LoggerPool::getDefaultLogger( )
252 {
253 return getNamedLogger( "org.openoffice.logging.DefaultLogger" );
254 }
255
256} // namespace logging
257
258extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
260 css::uno::XComponentContext *context,
261 css::uno::Sequence<css::uno::Any> const &)
262{
263 return cppu::acquire(new logging::LoggerPool(context));
264}
265
266/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
oslInterlockedCount m_nEventNumber
Definition: logger.cxx:59
comphelper::OInterfaceContainerHelper2 m_aHandlers
Definition: logger.cxx:58
Reference< XComponentContext > m_xContext
Definition: logger.cxx:99
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_extensions_LoggerPool(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: logger.cxx:259
sal_Int32 m_nLogLevel
Definition: logger.cxx:62
OUString m_sName
Definition: logger.cxx:63
::osl::Mutex m_aMutex
Definition: logger.cxx:98
std::map< OUString, WeakReference< XLogger > > m_aLoggerMap
Definition: logger.cxx:100
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
LogRecord createLogRecord(const OUString &_rLoggerName, const OUString &_rClassName, const OUString &_rMethodName, const OUString &_rMessage, sal_Int32 _nLogLevel, oslInterlockedCount _nEventNumber)
Definition: logrecord.cxx:51
void initializeLoggerFromConfiguration(const Reference< XComponentContext > &_rContext, const Reference< XLogger > &_rxLogger)
log
unsigned char sal_Bool