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