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