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