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