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