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 "log_services.hxx"
23 : #include "methodguard.hxx"
24 : #include "loghandler.hxx"
25 :
26 : #include <com/sun/star/logging/XConsoleHandler.hpp>
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 : #include <com/sun/star/logging/LogLevel.hpp>
29 : #include <com/sun/star/lang/XInitialization.hpp>
30 : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
31 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 : #include <com/sun/star/beans/NamedValue.hpp>
33 :
34 : #include <cppuhelper/compbase3.hxx>
35 : #include <cppuhelper/basemutex.hxx>
36 : #include <cppuhelper/supportsservice.hxx>
37 :
38 : #include <stdio.h>
39 :
40 :
41 : namespace logging
42 : {
43 :
44 :
45 : using ::com::sun::star::logging::XConsoleHandler;
46 : using ::com::sun::star::lang::XServiceInfo;
47 : using ::com::sun::star::uno::Reference;
48 : using ::com::sun::star::uno::XComponentContext;
49 : using ::com::sun::star::uno::RuntimeException;
50 : using ::com::sun::star::logging::XLogFormatter;
51 : using ::com::sun::star::uno::Sequence;
52 : using ::com::sun::star::logging::LogRecord;
53 : using ::com::sun::star::uno::UNO_QUERY_THROW;
54 : using ::com::sun::star::uno::Exception;
55 : using ::com::sun::star::uno::Any;
56 : using ::com::sun::star::uno::XInterface;
57 : using ::com::sun::star::lang::XInitialization;
58 : using ::com::sun::star::ucb::AlreadyInitializedException;
59 : using ::com::sun::star::lang::IllegalArgumentException;
60 : using ::com::sun::star::beans::NamedValue;
61 :
62 : namespace LogLevel = ::com::sun::star::logging::LogLevel;
63 :
64 :
65 : //= ConsoleHandler - declaration
66 :
67 :
68 : typedef ::cppu::WeakComponentImplHelper3 < XConsoleHandler
69 : , XServiceInfo
70 : , XInitialization
71 : > ConsoleHandler_Base;
72 : class ConsoleHandler :public ::cppu::BaseMutex
73 : ,public ConsoleHandler_Base
74 : {
75 : private:
76 : LogHandlerHelper m_aHandlerHelper;
77 : sal_Int32 m_nThreshold;
78 :
79 : protected:
80 : ConsoleHandler( const Reference< XComponentContext >& _rxContext );
81 : virtual ~ConsoleHandler();
82 :
83 : // XConsoleHandler
84 : virtual ::sal_Int32 SAL_CALL getThreshold() throw (RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual void SAL_CALL setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 :
87 : // XLogHandler
88 : virtual OUString SAL_CALL getEncoding() throw (RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual void SAL_CALL setEncoding( const OUString& _encoding ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 : virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException, std::exception) SAL_OVERRIDE;
91 : virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
92 : virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException, std::exception) SAL_OVERRIDE;
93 : virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
94 : virtual void SAL_CALL flush( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
95 : virtual sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
96 :
97 : // XInitialization
98 : virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
99 :
100 : // XServiceInfo
101 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
102 : virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
103 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
104 :
105 : // OComponentHelper
106 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
107 :
108 : public:
109 : // XServiceInfo - static version
110 : static OUString SAL_CALL getImplementationName_static();
111 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
112 : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
113 :
114 : public:
115 : typedef ComponentMethodGuard< ConsoleHandler > MethodGuard;
116 : void enterMethod( MethodGuard::Access );
117 : void leaveMethod( MethodGuard::Access );
118 : };
119 :
120 :
121 : //= ConsoleHandler - implementation
122 :
123 :
124 2 : ConsoleHandler::ConsoleHandler( const Reference< XComponentContext >& _rxContext )
125 : :ConsoleHandler_Base( m_aMutex )
126 : ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
127 2 : ,m_nThreshold( LogLevel::SEVERE )
128 : {
129 2 : }
130 :
131 :
132 6 : ConsoleHandler::~ConsoleHandler()
133 : {
134 2 : if ( !rBHelper.bDisposed )
135 : {
136 0 : acquire();
137 0 : dispose();
138 : }
139 4 : }
140 :
141 :
142 2 : void SAL_CALL ConsoleHandler::disposing()
143 : {
144 2 : m_aHandlerHelper.setFormatter( NULL );
145 2 : }
146 :
147 :
148 0 : void ConsoleHandler::enterMethod( MethodGuard::Access )
149 : {
150 0 : m_aHandlerHelper.enterMethod();
151 0 : }
152 :
153 :
154 0 : void ConsoleHandler::leaveMethod( MethodGuard::Access )
155 : {
156 0 : m_aMutex.release();
157 0 : }
158 :
159 :
160 0 : ::sal_Int32 SAL_CALL ConsoleHandler::getThreshold() throw (RuntimeException, std::exception)
161 : {
162 0 : MethodGuard aGuard( *this );
163 0 : return m_nThreshold;
164 : }
165 :
166 :
167 0 : void SAL_CALL ConsoleHandler::setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException, std::exception)
168 : {
169 0 : MethodGuard aGuard( *this );
170 0 : m_nThreshold = _threshold;
171 0 : }
172 :
173 :
174 0 : OUString SAL_CALL ConsoleHandler::getEncoding() throw (RuntimeException, std::exception)
175 : {
176 0 : MethodGuard aGuard( *this );
177 0 : OUString sEncoding;
178 0 : OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
179 0 : return sEncoding;
180 : }
181 :
182 :
183 0 : void SAL_CALL ConsoleHandler::setEncoding( const OUString& _rEncoding ) throw (RuntimeException, std::exception)
184 : {
185 0 : MethodGuard aGuard( *this );
186 0 : OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
187 0 : }
188 :
189 :
190 0 : Reference< XLogFormatter > SAL_CALL ConsoleHandler::getFormatter() throw (RuntimeException, std::exception)
191 : {
192 0 : MethodGuard aGuard( *this );
193 0 : return m_aHandlerHelper.getFormatter();
194 : }
195 :
196 :
197 0 : void SAL_CALL ConsoleHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException, std::exception)
198 : {
199 0 : MethodGuard aGuard( *this );
200 0 : m_aHandlerHelper.setFormatter( _rxFormatter );
201 0 : }
202 :
203 :
204 0 : ::sal_Int32 SAL_CALL ConsoleHandler::getLevel() throw (RuntimeException, std::exception)
205 : {
206 0 : MethodGuard aGuard( *this );
207 0 : return m_aHandlerHelper.getLevel();
208 : }
209 :
210 :
211 0 : void SAL_CALL ConsoleHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException, std::exception)
212 : {
213 0 : MethodGuard aGuard( *this );
214 0 : m_aHandlerHelper.setLevel( _nLevel );
215 0 : }
216 :
217 :
218 0 : void SAL_CALL ConsoleHandler::flush( ) throw (RuntimeException, std::exception)
219 : {
220 0 : MethodGuard aGuard( *this );
221 0 : fflush( stdout );
222 0 : fflush( stderr );
223 0 : }
224 :
225 :
226 0 : sal_Bool SAL_CALL ConsoleHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException, std::exception)
227 : {
228 0 : MethodGuard aGuard( *this );
229 :
230 0 : OString sEntry;
231 0 : if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
232 0 : return sal_False;
233 :
234 0 : if ( _rRecord.Level >= m_nThreshold )
235 0 : fprintf( stderr, "%s\n", sEntry.getStr() );
236 : else
237 0 : fprintf( stdout, "%s\n", sEntry.getStr() );
238 :
239 0 : return sal_True;
240 : }
241 :
242 :
243 2 : void SAL_CALL ConsoleHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
244 : {
245 2 : ::osl::MutexGuard aGuard( m_aMutex );
246 :
247 2 : if ( m_aHandlerHelper.getIsInitialized() )
248 0 : throw AlreadyInitializedException();
249 :
250 2 : if ( _rArguments.getLength() == 0 )
251 : { // create() - nothing to init
252 2 : m_aHandlerHelper.setIsInitialized();
253 4 : return;
254 : }
255 :
256 0 : if ( _rArguments.getLength() != 1 )
257 0 : throw IllegalArgumentException( OUString(), *this, 1 );
258 :
259 0 : Sequence< NamedValue > aSettings;
260 0 : if ( !( _rArguments[0] >>= aSettings ) )
261 0 : throw IllegalArgumentException( OUString(), *this, 1 );
262 :
263 : // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
264 0 : ::comphelper::NamedValueCollection aTypedSettings( aSettings );
265 0 : m_aHandlerHelper.initFromSettings( aTypedSettings );
266 :
267 0 : aTypedSettings.get_ensureType( "Threshold", m_nThreshold );
268 :
269 0 : m_aHandlerHelper.setIsInitialized();
270 : }
271 :
272 :
273 0 : OUString SAL_CALL ConsoleHandler::getImplementationName() throw(RuntimeException, std::exception)
274 : {
275 0 : return getImplementationName_static();
276 : }
277 :
278 :
279 0 : sal_Bool SAL_CALL ConsoleHandler::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
280 : {
281 0 : return cppu::supportsService(this, _rServiceName);
282 : }
283 :
284 :
285 0 : Sequence< OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames() throw(RuntimeException, std::exception)
286 : {
287 0 : return getSupportedServiceNames_static();
288 : }
289 :
290 :
291 14 : OUString SAL_CALL ConsoleHandler::getImplementationName_static()
292 : {
293 14 : return OUString( "com.sun.star.comp.extensions.ConsoleHandler" );
294 : }
295 :
296 :
297 14 : Sequence< OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames_static()
298 : {
299 14 : Sequence< OUString > aServiceNames(1);
300 14 : aServiceNames[0] = "com.sun.star.logging.ConsoleHandler";
301 14 : return aServiceNames;
302 : }
303 :
304 :
305 2 : Reference< XInterface > ConsoleHandler::Create( const Reference< XComponentContext >& _rxContext )
306 : {
307 2 : return *( new ConsoleHandler( _rxContext ) );
308 : }
309 :
310 :
311 44 : void createRegistryInfo_ConsoleHandler()
312 : {
313 44 : static OAutoRegistration< ConsoleHandler > aAutoRegistration;
314 44 : }
315 :
316 :
317 : } // namespace logging
318 :
319 :
320 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|