Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "log_module.hxx"
31 : : #include "methodguard.hxx"
32 : : #include "loghandler.hxx"
33 : :
34 : : #include <com/sun/star/logging/XConsoleHandler.hpp>
35 : : #include <com/sun/star/lang/XServiceInfo.hpp>
36 : : #include <com/sun/star/logging/LogLevel.hpp>
37 : : #include <com/sun/star/lang/XInitialization.hpp>
38 : : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
39 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
40 : : #include <com/sun/star/beans/NamedValue.hpp>
41 : :
42 : : #include <tools/diagnose_ex.h>
43 : :
44 : : #include <comphelper/componentcontext.hxx>
45 : :
46 : : #include <cppuhelper/compbase3.hxx>
47 : : #include <cppuhelper/basemutex.hxx>
48 : :
49 : : #include <stdio.h>
50 : :
51 : : //........................................................................
52 : : namespace logging
53 : : {
54 : : //........................................................................
55 : :
56 : : /** === begin UNO using === **/
57 : : using ::com::sun::star::logging::XConsoleHandler;
58 : : using ::com::sun::star::lang::XServiceInfo;
59 : : using ::com::sun::star::uno::Reference;
60 : : using ::com::sun::star::uno::XComponentContext;
61 : : using ::com::sun::star::uno::RuntimeException;
62 : : using ::com::sun::star::logging::XLogFormatter;
63 : : using ::com::sun::star::uno::Sequence;
64 : : using ::com::sun::star::logging::LogRecord;
65 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
66 : : using ::com::sun::star::uno::Exception;
67 : : using ::com::sun::star::uno::Any;
68 : : using ::com::sun::star::uno::XInterface;
69 : : using ::com::sun::star::lang::XInitialization;
70 : : using ::com::sun::star::ucb::AlreadyInitializedException;
71 : : using ::com::sun::star::lang::IllegalArgumentException;
72 : : using ::com::sun::star::beans::NamedValue;
73 : : /** === end UNO using === **/
74 : : namespace LogLevel = ::com::sun::star::logging::LogLevel;
75 : :
76 : : //====================================================================
77 : : //= ConsoleHandler - declaration
78 : : //====================================================================
79 : : //--------------------------------------------------------------------
80 : : typedef ::cppu::WeakComponentImplHelper3 < XConsoleHandler
81 : : , XServiceInfo
82 : : , XInitialization
83 : : > ConsoleHandler_Base;
84 : : class ConsoleHandler :public ::cppu::BaseMutex
85 : : ,public ConsoleHandler_Base
86 : : {
87 : : private:
88 : : ::comphelper::ComponentContext m_aContext;
89 : : LogHandlerHelper m_aHandlerHelper;
90 : : sal_Int32 m_nThreshold;
91 : :
92 : : protected:
93 : : ConsoleHandler( const Reference< XComponentContext >& _rxContext );
94 : : virtual ~ConsoleHandler();
95 : :
96 : : // XConsoleHandler
97 : : virtual ::sal_Int32 SAL_CALL getThreshold() throw (RuntimeException);
98 : : virtual void SAL_CALL setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException);
99 : :
100 : : // XLogHandler
101 : : virtual ::rtl::OUString SAL_CALL getEncoding() throw (RuntimeException);
102 : : virtual void SAL_CALL setEncoding( const ::rtl::OUString& _encoding ) throw (RuntimeException);
103 : : virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException);
104 : : virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException);
105 : : virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
106 : : virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
107 : : virtual void SAL_CALL flush( ) throw (RuntimeException);
108 : : virtual ::sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException);
109 : :
110 : : // XInitialization
111 : : 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);
112 : :
113 : : // XServiceInfo
114 : : virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
115 : : virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
116 : : virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
117 : :
118 : : // OComponentHelper
119 : : virtual void SAL_CALL disposing();
120 : :
121 : : public:
122 : : // XServiceInfo - static version
123 : : static ::rtl::OUString SAL_CALL getImplementationName_static();
124 : : static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
125 : : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
126 : :
127 : : public:
128 : : typedef ComponentMethodGuard< ConsoleHandler > MethodGuard;
129 : : void enterMethod( MethodGuard::Access );
130 : : void leaveMethod( MethodGuard::Access );
131 : : };
132 : :
133 : : //====================================================================
134 : : //= ConsoleHandler - implementation
135 : : //====================================================================
136 : : //--------------------------------------------------------------------
137 : 0 : ConsoleHandler::ConsoleHandler( const Reference< XComponentContext >& _rxContext )
138 : : :ConsoleHandler_Base( m_aMutex )
139 : : ,m_aContext( _rxContext )
140 : : ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
141 [ # # ][ # # ]: 0 : ,m_nThreshold( LogLevel::SEVERE )
142 : : {
143 : 0 : }
144 : :
145 : : //--------------------------------------------------------------------
146 [ # # ][ # # ]: 0 : ConsoleHandler::~ConsoleHandler()
[ # # ]
147 : : {
148 [ # # ]: 0 : if ( !rBHelper.bDisposed )
149 : : {
150 : 0 : acquire();
151 [ # # ]: 0 : dispose();
152 : : }
153 [ # # ]: 0 : }
154 : :
155 : : //--------------------------------------------------------------------
156 : 0 : void SAL_CALL ConsoleHandler::disposing()
157 : : {
158 [ # # ]: 0 : m_aHandlerHelper.setFormatter( NULL );
159 : 0 : }
160 : :
161 : : //--------------------------------------------------------------------
162 : 0 : void ConsoleHandler::enterMethod( MethodGuard::Access )
163 : : {
164 : 0 : m_aHandlerHelper.enterMethod();
165 : 0 : }
166 : :
167 : : //--------------------------------------------------------------------
168 : 0 : void ConsoleHandler::leaveMethod( MethodGuard::Access )
169 : : {
170 : 0 : m_aMutex.release();
171 : 0 : }
172 : :
173 : : //--------------------------------------------------------------------
174 : 0 : ::sal_Int32 SAL_CALL ConsoleHandler::getThreshold() throw (RuntimeException)
175 : : {
176 [ # # ]: 0 : MethodGuard aGuard( *this );
177 [ # # ]: 0 : return m_nThreshold;
178 : : }
179 : :
180 : : //--------------------------------------------------------------------
181 : 0 : void SAL_CALL ConsoleHandler::setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException)
182 : : {
183 [ # # ]: 0 : MethodGuard aGuard( *this );
184 [ # # ]: 0 : m_nThreshold = _threshold;
185 : 0 : }
186 : :
187 : : //--------------------------------------------------------------------
188 : 0 : ::rtl::OUString SAL_CALL ConsoleHandler::getEncoding() throw (RuntimeException)
189 : : {
190 [ # # ]: 0 : MethodGuard aGuard( *this );
191 : 0 : ::rtl::OUString sEncoding;
192 [ # # ]: 0 : OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
193 [ # # ]: 0 : return sEncoding;
194 : : }
195 : :
196 : : //--------------------------------------------------------------------
197 : 0 : void SAL_CALL ConsoleHandler::setEncoding( const ::rtl::OUString& _rEncoding ) throw (RuntimeException)
198 : : {
199 [ # # ]: 0 : MethodGuard aGuard( *this );
200 [ # # ][ # # ]: 0 : OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
201 : 0 : }
202 : :
203 : : //--------------------------------------------------------------------
204 : 0 : Reference< XLogFormatter > SAL_CALL ConsoleHandler::getFormatter() throw (RuntimeException)
205 : : {
206 [ # # ]: 0 : MethodGuard aGuard( *this );
207 [ # # ][ # # ]: 0 : return m_aHandlerHelper.getFormatter();
208 : : }
209 : :
210 : : //--------------------------------------------------------------------
211 : 0 : void SAL_CALL ConsoleHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException)
212 : : {
213 [ # # ]: 0 : MethodGuard aGuard( *this );
214 [ # # ][ # # ]: 0 : m_aHandlerHelper.setFormatter( _rxFormatter );
215 : 0 : }
216 : :
217 : : //--------------------------------------------------------------------
218 : 0 : ::sal_Int32 SAL_CALL ConsoleHandler::getLevel() throw (RuntimeException)
219 : : {
220 [ # # ]: 0 : MethodGuard aGuard( *this );
221 [ # # ]: 0 : return m_aHandlerHelper.getLevel();
222 : : }
223 : :
224 : : //--------------------------------------------------------------------
225 : 0 : void SAL_CALL ConsoleHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException)
226 : : {
227 [ # # ]: 0 : MethodGuard aGuard( *this );
228 [ # # ]: 0 : m_aHandlerHelper.setLevel( _nLevel );
229 : 0 : }
230 : :
231 : : //--------------------------------------------------------------------
232 : 0 : void SAL_CALL ConsoleHandler::flush( ) throw (RuntimeException)
233 : : {
234 [ # # ]: 0 : MethodGuard aGuard( *this );
235 [ # # ]: 0 : fflush( stdout );
236 [ # # ][ # # ]: 0 : fflush( stderr );
237 : 0 : }
238 : :
239 : : //--------------------------------------------------------------------
240 : 0 : ::sal_Bool SAL_CALL ConsoleHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException)
241 : : {
242 [ # # ]: 0 : MethodGuard aGuard( *this );
243 : :
244 : 0 : ::rtl::OString sEntry;
245 [ # # ][ # # ]: 0 : if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
246 : 0 : return sal_False;
247 : :
248 [ # # ]: 0 : if ( _rRecord.Level >= m_nThreshold )
249 [ # # ]: 0 : fprintf( stderr, "%s\n", sEntry.getStr() );
250 : : else
251 [ # # ]: 0 : fprintf( stdout, "%s\n", sEntry.getStr() );
252 : :
253 [ # # ]: 0 : return sal_True;
254 : : }
255 : :
256 : : //--------------------------------------------------------------------
257 : 0 : void SAL_CALL ConsoleHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
258 : : {
259 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
260 : :
261 [ # # ]: 0 : if ( m_aHandlerHelper.getIsInitialized() )
262 [ # # ]: 0 : throw AlreadyInitializedException();
263 : :
264 [ # # ]: 0 : if ( _rArguments.getLength() == 0 )
265 : : { // create() - nothing to init
266 : 0 : m_aHandlerHelper.setIsInitialized();
267 : 0 : return;
268 : : }
269 : :
270 [ # # ]: 0 : if ( _rArguments.getLength() != 1 )
271 [ # # ][ # # ]: 0 : throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
272 : :
273 [ # # ]: 0 : Sequence< NamedValue > aSettings;
274 [ # # ][ # # ]: 0 : if ( !( _rArguments[0] >>= aSettings ) )
275 [ # # ][ # # ]: 0 : throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
276 : :
277 : : // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
278 [ # # ]: 0 : ::comphelper::NamedValueCollection aTypedSettings( aSettings );
279 [ # # ]: 0 : m_aHandlerHelper.initFromSettings( aTypedSettings );
280 : :
281 [ # # ]: 0 : aTypedSettings.get_ensureType( "Threshold", m_nThreshold );
282 : :
283 [ # # ][ # # ]: 0 : m_aHandlerHelper.setIsInitialized();
[ # # ][ # # ]
284 : : }
285 : :
286 : : //--------------------------------------------------------------------
287 : 0 : ::rtl::OUString SAL_CALL ConsoleHandler::getImplementationName() throw(RuntimeException)
288 : : {
289 : 0 : return getImplementationName_static();
290 : : }
291 : :
292 : : //--------------------------------------------------------------------
293 : 0 : ::sal_Bool SAL_CALL ConsoleHandler::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
294 : : {
295 [ # # ]: 0 : const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
296 [ # # ]: 0 : for ( const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
297 : 0 : pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
298 : : ++pServiceNames
299 : : )
300 [ # # ]: 0 : if ( _rServiceName == *pServiceNames )
301 : 0 : return sal_True;
302 [ # # ]: 0 : return sal_False;
303 : : }
304 : :
305 : : //--------------------------------------------------------------------
306 : 0 : Sequence< ::rtl::OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames() throw(RuntimeException)
307 : : {
308 : 0 : return getSupportedServiceNames_static();
309 : : }
310 : :
311 : : //--------------------------------------------------------------------
312 : 9 : ::rtl::OUString SAL_CALL ConsoleHandler::getImplementationName_static()
313 : : {
314 : 9 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.ConsoleHandler" ) );
315 : : }
316 : :
317 : : //--------------------------------------------------------------------
318 : 9 : Sequence< ::rtl::OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames_static()
319 : : {
320 : 9 : Sequence< ::rtl::OUString > aServiceNames(1);
321 [ + - ][ + - ]: 9 : aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.ConsoleHandler" ) );
322 : 9 : return aServiceNames;
323 : : }
324 : :
325 : : //--------------------------------------------------------------------
326 : 0 : Reference< XInterface > ConsoleHandler::Create( const Reference< XComponentContext >& _rxContext )
327 : : {
328 [ # # ]: 0 : return *( new ConsoleHandler( _rxContext ) );
329 : : }
330 : :
331 : : //--------------------------------------------------------------------
332 : 27 : void createRegistryInfo_ConsoleHandler()
333 : : {
334 [ + + ][ + - ]: 27 : static OAutoRegistration< ConsoleHandler > aAutoRegistration;
[ + - ][ # # ]
335 : 27 : }
336 : :
337 : : //........................................................................
338 : : } // namespace logging
339 : : //........................................................................
340 : :
341 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|