12a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52a97ec55SAndrew Rist  * distributed with this work for additional information
62a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
92a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
102a97ec55SAndrew Rist  *
112a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122a97ec55SAndrew Rist  *
132a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142a97ec55SAndrew Rist  * software distributed under the License is distributed on an
152a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
172a97ec55SAndrew Rist  * specific language governing permissions and limitations
182a97ec55SAndrew Rist  * under the License.
192a97ec55SAndrew Rist  *
202a97ec55SAndrew Rist  *************************************************************/
212a97ec55SAndrew Rist 
222a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "log_module.hxx"
28cdf0e10cSrcweir #include "methodguard.hxx"
29cdf0e10cSrcweir #include "loghandler.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /** === begin UNO includes === **/
32cdf0e10cSrcweir #include <com/sun/star/logging/XConsoleHandler.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <com/sun/star/logging/LogLevel.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
38cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
39cdf0e10cSrcweir /** === end UNO includes === **/
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <tools/diagnose_ex.h>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <cppuhelper/compbase3.hxx>
46cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <stdio.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir //........................................................................
51cdf0e10cSrcweir namespace logging
52cdf0e10cSrcweir {
53cdf0e10cSrcweir //........................................................................
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	/** === begin UNO using === **/
56cdf0e10cSrcweir     using ::com::sun::star::logging::XConsoleHandler;
57cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
58cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
59cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
60cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
61cdf0e10cSrcweir     using ::com::sun::star::logging::XLogFormatter;
62cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
63cdf0e10cSrcweir     using ::com::sun::star::logging::LogRecord;
64cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
65cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
66cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
67cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
68cdf0e10cSrcweir     using ::com::sun::star::lang::XInitialization;
69cdf0e10cSrcweir     using ::com::sun::star::ucb::AlreadyInitializedException;
70cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
71cdf0e10cSrcweir     using ::com::sun::star::beans::NamedValue;
72cdf0e10cSrcweir 	/** === end UNO using === **/
73cdf0e10cSrcweir     namespace LogLevel = ::com::sun::star::logging::LogLevel;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	//====================================================================
76cdf0e10cSrcweir 	//= ConsoleHandler - declaration
77cdf0e10cSrcweir 	//====================================================================
78cdf0e10cSrcweir 	//--------------------------------------------------------------------
79cdf0e10cSrcweir     typedef ::cppu::WeakComponentImplHelper3    <   XConsoleHandler
80cdf0e10cSrcweir                                                 ,   XServiceInfo
81cdf0e10cSrcweir                                                 ,   XInitialization
82cdf0e10cSrcweir                                                 >   ConsoleHandler_Base;
83cdf0e10cSrcweir     class ConsoleHandler    :public ::cppu::BaseMutex
84cdf0e10cSrcweir                             ,public ConsoleHandler_Base
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir     private:
87cdf0e10cSrcweir         ::comphelper::ComponentContext  m_aContext;
88cdf0e10cSrcweir         LogHandlerHelper                m_aHandlerHelper;
89cdf0e10cSrcweir         sal_Int32                       m_nThreshold;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     protected:
92cdf0e10cSrcweir         ConsoleHandler( const Reference< XComponentContext >& _rxContext );
93cdf0e10cSrcweir         virtual ~ConsoleHandler();
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // XConsoleHandler
96cdf0e10cSrcweir         virtual ::sal_Int32 SAL_CALL getThreshold() throw (RuntimeException);
97cdf0e10cSrcweir         virtual void SAL_CALL setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         // XLogHandler
100cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getEncoding() throw (RuntimeException);
101cdf0e10cSrcweir         virtual void SAL_CALL setEncoding( const ::rtl::OUString& _encoding ) throw (RuntimeException);
102cdf0e10cSrcweir         virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException);
103cdf0e10cSrcweir         virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException);
104cdf0e10cSrcweir         virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
105cdf0e10cSrcweir         virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
106cdf0e10cSrcweir         virtual void SAL_CALL flush(  ) throw (RuntimeException);
107cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         // XInitialization
110cdf0e10cSrcweir         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);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         // XServiceInfo
113cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
114cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
115cdf0e10cSrcweir         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         // OComponentHelper
118cdf0e10cSrcweir         virtual void SAL_CALL disposing();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     public:
121cdf0e10cSrcweir         // XServiceInfo - static version
122cdf0e10cSrcweir 		static ::rtl::OUString SAL_CALL getImplementationName_static();
123cdf0e10cSrcweir         static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
124cdf0e10cSrcweir         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     public:
127cdf0e10cSrcweir         typedef ComponentMethodGuard< ConsoleHandler > MethodGuard;
128cdf0e10cSrcweir         void    enterMethod( MethodGuard::Access );
129cdf0e10cSrcweir         void    leaveMethod( MethodGuard::Access );
130cdf0e10cSrcweir 	};
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     //====================================================================
133cdf0e10cSrcweir 	//= ConsoleHandler - implementation
134cdf0e10cSrcweir 	//====================================================================
135cdf0e10cSrcweir 	//--------------------------------------------------------------------
ConsoleHandler(const Reference<XComponentContext> & _rxContext)136cdf0e10cSrcweir     ConsoleHandler::ConsoleHandler( const Reference< XComponentContext >& _rxContext )
137cdf0e10cSrcweir         :ConsoleHandler_Base( m_aMutex )
138cdf0e10cSrcweir         ,m_aContext( _rxContext )
139cdf0e10cSrcweir         ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
140cdf0e10cSrcweir         ,m_nThreshold( LogLevel::SEVERE )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     //--------------------------------------------------------------------
~ConsoleHandler()145cdf0e10cSrcweir     ConsoleHandler::~ConsoleHandler()
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         if ( !rBHelper.bDisposed )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             acquire();
150cdf0e10cSrcweir             dispose();
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     //--------------------------------------------------------------------
disposing()155cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::disposing()
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         m_aHandlerHelper.setFormatter( NULL );
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     //--------------------------------------------------------------------
enterMethod(MethodGuard::Access)161cdf0e10cSrcweir     void ConsoleHandler::enterMethod( MethodGuard::Access )
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         m_aHandlerHelper.enterMethod();
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     //--------------------------------------------------------------------
leaveMethod(MethodGuard::Access)167cdf0e10cSrcweir     void ConsoleHandler::leaveMethod( MethodGuard::Access )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         m_aMutex.release();
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     //--------------------------------------------------------------------
getThreshold()173cdf0e10cSrcweir     ::sal_Int32 SAL_CALL ConsoleHandler::getThreshold() throw (RuntimeException)
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         MethodGuard aGuard( *this );
176cdf0e10cSrcweir         return m_nThreshold;
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     //--------------------------------------------------------------------
setThreshold(::sal_Int32 _threshold)180cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::setThreshold( ::sal_Int32 _threshold ) throw (RuntimeException)
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         MethodGuard aGuard( *this );
183cdf0e10cSrcweir         m_nThreshold = _threshold;
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     //--------------------------------------------------------------------
getEncoding()187cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ConsoleHandler::getEncoding() throw (RuntimeException)
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         MethodGuard aGuard( *this );
190cdf0e10cSrcweir         ::rtl::OUString sEncoding;
191cdf0e10cSrcweir         OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
192cdf0e10cSrcweir         return sEncoding;
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     //--------------------------------------------------------------------
setEncoding(const::rtl::OUString & _rEncoding)196cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::setEncoding( const ::rtl::OUString& _rEncoding ) throw (RuntimeException)
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         MethodGuard aGuard( *this );
199cdf0e10cSrcweir         OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     //--------------------------------------------------------------------
getFormatter()203cdf0e10cSrcweir     Reference< XLogFormatter > SAL_CALL ConsoleHandler::getFormatter() throw (RuntimeException)
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         MethodGuard aGuard( *this );
206cdf0e10cSrcweir         return m_aHandlerHelper.getFormatter();
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     //--------------------------------------------------------------------
setFormatter(const Reference<XLogFormatter> & _rxFormatter)210cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException)
211cdf0e10cSrcweir     {
212cdf0e10cSrcweir         MethodGuard aGuard( *this );
213cdf0e10cSrcweir         m_aHandlerHelper.setFormatter( _rxFormatter );
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     //--------------------------------------------------------------------
getLevel()217cdf0e10cSrcweir     ::sal_Int32 SAL_CALL ConsoleHandler::getLevel() throw (RuntimeException)
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         MethodGuard aGuard( *this );
220cdf0e10cSrcweir         return m_aHandlerHelper.getLevel();
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     //--------------------------------------------------------------------
setLevel(::sal_Int32 _nLevel)224cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException)
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         MethodGuard aGuard( *this );
227cdf0e10cSrcweir         m_aHandlerHelper.setLevel( _nLevel );
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     //--------------------------------------------------------------------
flush()231cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::flush(  ) throw (RuntimeException)
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         MethodGuard aGuard( *this );
234cdf0e10cSrcweir         fflush( stdout );
235cdf0e10cSrcweir         fflush( stderr );
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     //--------------------------------------------------------------------
publish(const LogRecord & _rRecord)239cdf0e10cSrcweir     ::sal_Bool SAL_CALL ConsoleHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         MethodGuard aGuard( *this );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         ::rtl::OString sEntry;
244cdf0e10cSrcweir         if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
245cdf0e10cSrcweir             return sal_False;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir         if ( _rRecord.Level >= m_nThreshold )
248*851abcd9Struckman             fprintf( stderr, "%s", sEntry.getStr() );
249cdf0e10cSrcweir         else
250*851abcd9Struckman             fprintf( stdout, "%s", sEntry.getStr() );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         return sal_True;
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     //--------------------------------------------------------------------
initialize(const Sequence<Any> & _rArguments)256cdf0e10cSrcweir     void SAL_CALL ConsoleHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         if ( m_aHandlerHelper.getIsInitialized() )
261cdf0e10cSrcweir             throw AlreadyInitializedException();
262cdf0e10cSrcweir 
263cdf0e10cSrcweir         if ( _rArguments.getLength() == 0 )
264cdf0e10cSrcweir         {   // create() - nothing to init
265cdf0e10cSrcweir             m_aHandlerHelper.setIsInitialized();
266cdf0e10cSrcweir             return;
267cdf0e10cSrcweir         }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         if ( _rArguments.getLength() != 1 )
270cdf0e10cSrcweir             throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir         Sequence< NamedValue > aSettings;
273cdf0e10cSrcweir         if ( !( _rArguments[0] >>= aSettings ) )
274cdf0e10cSrcweir             throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir         // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
277cdf0e10cSrcweir         ::comphelper::NamedValueCollection aTypedSettings( aSettings );
278cdf0e10cSrcweir         m_aHandlerHelper.initFromSettings( aTypedSettings );
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         aTypedSettings.get_ensureType( "Threshold", m_nThreshold );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir         m_aHandlerHelper.setIsInitialized();
283cdf0e10cSrcweir     }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     //--------------------------------------------------------------------
getImplementationName()286cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ConsoleHandler::getImplementationName() throw(RuntimeException)
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir         return getImplementationName_static();
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)292cdf0e10cSrcweir     ::sal_Bool SAL_CALL ConsoleHandler::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
295cdf0e10cSrcweir         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
296cdf0e10cSrcweir                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
297cdf0e10cSrcweir                 ++pServiceNames
298cdf0e10cSrcweir             )
299cdf0e10cSrcweir             if ( _rServiceName == *pServiceNames )
300cdf0e10cSrcweir                 return sal_True;
301cdf0e10cSrcweir         return sal_False;
302cdf0e10cSrcweir     }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     //--------------------------------------------------------------------
getSupportedServiceNames()305cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames() throw(RuntimeException)
306cdf0e10cSrcweir     {
307cdf0e10cSrcweir         return getSupportedServiceNames_static();
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     //--------------------------------------------------------------------
getImplementationName_static()311cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ConsoleHandler::getImplementationName_static()
312cdf0e10cSrcweir     {
313cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.ConsoleHandler" ) );
314cdf0e10cSrcweir     }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     //--------------------------------------------------------------------
getSupportedServiceNames_static()317cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames_static()
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServiceNames(1);
320cdf0e10cSrcweir         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.ConsoleHandler" ) );
321cdf0e10cSrcweir         return aServiceNames;
322cdf0e10cSrcweir     }
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     //--------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)325cdf0e10cSrcweir     Reference< XInterface > ConsoleHandler::Create( const Reference< XComponentContext >& _rxContext )
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         return *( new ConsoleHandler( _rxContext ) );
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     //--------------------------------------------------------------------
createRegistryInfo_ConsoleHandler()331cdf0e10cSrcweir     void createRegistryInfo_ConsoleHandler()
332cdf0e10cSrcweir     {
333cdf0e10cSrcweir         static OAutoRegistration< ConsoleHandler > aAutoRegistration;
334cdf0e10cSrcweir     }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir //........................................................................
337cdf0e10cSrcweir } // namespace logging
338cdf0e10cSrcweir //........................................................................
339