1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: consolehandler.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #include "log_module.hxx"
35 #include "methodguard.hxx"
36 #include "loghandler.hxx"
38 /** === begin UNO includes === **/
39 #include <com/sun/star/logging/XConsoleHandler.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/logging/LogLevel.hpp>
42 #include <com/sun/star/lang/XInitialization.hpp>
43 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
44 #include <com/sun/star/lang/IllegalArgumentException.hpp>
45 #include <com/sun/star/beans/NamedValue.hpp>
46 /** === end UNO includes === **/
48 #include <tools/diagnose_ex.h>
50 #include <comphelper/componentcontext.hxx>
52 #include <cppuhelper/compbase3.hxx>
53 #include <cppuhelper/basemutex.hxx>
57 //........................................................................
60 //........................................................................
62 /** === begin UNO using === **/
63 using ::com::sun::star::logging::XConsoleHandler
;
64 using ::com::sun::star::lang::XServiceInfo
;
65 using ::com::sun::star::uno::Reference
;
66 using ::com::sun::star::uno::XComponentContext
;
67 using ::com::sun::star::uno::RuntimeException
;
68 using ::com::sun::star::logging::XLogFormatter
;
69 using ::com::sun::star::uno::Sequence
;
70 using ::com::sun::star::logging::LogRecord
;
71 using ::com::sun::star::uno::UNO_QUERY_THROW
;
72 using ::com::sun::star::uno::Exception
;
73 using ::com::sun::star::uno::Any
;
74 using ::com::sun::star::uno::XInterface
;
75 using ::com::sun::star::lang::XInitialization
;
76 using ::com::sun::star::ucb::AlreadyInitializedException
;
77 using ::com::sun::star::lang::IllegalArgumentException
;
78 using ::com::sun::star::beans::NamedValue
;
79 /** === end UNO using === **/
80 namespace LogLevel
= ::com::sun::star::logging::LogLevel
;
82 //====================================================================
83 //= ConsoleHandler - declaration
84 //====================================================================
85 //--------------------------------------------------------------------
86 typedef ::cppu::WeakComponentImplHelper3
< XConsoleHandler
89 > ConsoleHandler_Base
;
90 class ConsoleHandler
:public ::cppu::BaseMutex
91 ,public ConsoleHandler_Base
94 ::comphelper::ComponentContext m_aContext
;
95 LogHandlerHelper m_aHandlerHelper
;
96 sal_Int32 m_nThreshold
;
99 ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
);
100 virtual ~ConsoleHandler();
103 virtual ::sal_Int32 SAL_CALL
getThreshold() throw (RuntimeException
);
104 virtual void SAL_CALL
setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
);
107 virtual ::rtl::OUString SAL_CALL
getEncoding() throw (RuntimeException
);
108 virtual void SAL_CALL
setEncoding( const ::rtl::OUString
& _encoding
) throw (RuntimeException
);
109 virtual Reference
< XLogFormatter
> SAL_CALL
getFormatter() throw (RuntimeException
);
110 virtual void SAL_CALL
setFormatter( const Reference
< XLogFormatter
>& _formatter
) throw (RuntimeException
);
111 virtual ::sal_Int32 SAL_CALL
getLevel() throw (RuntimeException
);
112 virtual void SAL_CALL
setLevel( ::sal_Int32 _level
) throw (RuntimeException
);
113 virtual void SAL_CALL
flush( ) throw (RuntimeException
);
114 virtual ::sal_Bool SAL_CALL
publish( const LogRecord
& Record
) throw (RuntimeException
);
117 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
);
120 virtual ::rtl::OUString SAL_CALL
getImplementationName() throw(RuntimeException
);
121 virtual ::sal_Bool SAL_CALL
supportsService( const ::rtl::OUString
& _rServiceName
) throw(RuntimeException
);
122 virtual Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames() throw(RuntimeException
);
125 virtual void SAL_CALL
disposing();
128 // XServiceInfo - static version
129 static ::rtl::OUString SAL_CALL
getImplementationName_static();
130 static Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames_static();
131 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
134 typedef ComponentMethodGuard
< ConsoleHandler
> MethodGuard
;
135 void enterMethod( MethodGuard::Access
);
136 void leaveMethod( MethodGuard::Access
);
139 //====================================================================
140 //= ConsoleHandler - implementation
141 //====================================================================
142 //--------------------------------------------------------------------
143 ConsoleHandler::ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
)
144 :ConsoleHandler_Base( m_aMutex
)
145 ,m_aContext( _rxContext
)
146 ,m_aHandlerHelper( _rxContext
, m_aMutex
, rBHelper
)
147 ,m_nThreshold( LogLevel::SEVERE
)
151 //--------------------------------------------------------------------
152 ConsoleHandler::~ConsoleHandler()
154 if ( !rBHelper
.bDisposed
)
161 //--------------------------------------------------------------------
162 void SAL_CALL
ConsoleHandler::disposing()
164 m_aHandlerHelper
.setFormatter( NULL
);
167 //--------------------------------------------------------------------
168 void ConsoleHandler::enterMethod( MethodGuard::Access
)
170 m_aHandlerHelper
.enterMethod();
173 //--------------------------------------------------------------------
174 void ConsoleHandler::leaveMethod( MethodGuard::Access
)
179 //--------------------------------------------------------------------
180 ::sal_Int32 SAL_CALL
ConsoleHandler::getThreshold() throw (RuntimeException
)
182 MethodGuard
aGuard( *this );
186 //--------------------------------------------------------------------
187 void SAL_CALL
ConsoleHandler::setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
)
189 MethodGuard
aGuard( *this );
190 m_nThreshold
= _threshold
;
193 //--------------------------------------------------------------------
194 ::rtl::OUString SAL_CALL
ConsoleHandler::getEncoding() throw (RuntimeException
)
196 MethodGuard
aGuard( *this );
197 ::rtl::OUString sEncoding
;
198 OSL_VERIFY( m_aHandlerHelper
.getEncoding( sEncoding
) );
202 //--------------------------------------------------------------------
203 void SAL_CALL
ConsoleHandler::setEncoding( const ::rtl::OUString
& _rEncoding
) throw (RuntimeException
)
205 MethodGuard
aGuard( *this );
206 OSL_VERIFY( m_aHandlerHelper
.setEncoding( _rEncoding
) );
209 //--------------------------------------------------------------------
210 Reference
< XLogFormatter
> SAL_CALL
ConsoleHandler::getFormatter() throw (RuntimeException
)
212 MethodGuard
aGuard( *this );
213 return m_aHandlerHelper
.getFormatter();
216 //--------------------------------------------------------------------
217 void SAL_CALL
ConsoleHandler::setFormatter( const Reference
< XLogFormatter
>& _rxFormatter
) throw (RuntimeException
)
219 MethodGuard
aGuard( *this );
220 m_aHandlerHelper
.setFormatter( _rxFormatter
);
223 //--------------------------------------------------------------------
224 ::sal_Int32 SAL_CALL
ConsoleHandler::getLevel() throw (RuntimeException
)
226 MethodGuard
aGuard( *this );
227 return m_aHandlerHelper
.getLevel();
230 //--------------------------------------------------------------------
231 void SAL_CALL
ConsoleHandler::setLevel( ::sal_Int32 _nLevel
) throw (RuntimeException
)
233 MethodGuard
aGuard( *this );
234 m_aHandlerHelper
.setLevel( _nLevel
);
237 //--------------------------------------------------------------------
238 void SAL_CALL
ConsoleHandler::flush( ) throw (RuntimeException
)
240 MethodGuard
aGuard( *this );
245 //--------------------------------------------------------------------
246 ::sal_Bool SAL_CALL
ConsoleHandler::publish( const LogRecord
& _rRecord
) throw (RuntimeException
)
248 MethodGuard
aGuard( *this );
250 ::rtl::OString sEntry
;
251 if ( !m_aHandlerHelper
.formatForPublishing( _rRecord
, sEntry
) )
254 if ( _rRecord
.Level
>= m_nThreshold
)
255 fprintf( stderr
, "%s", sEntry
.getStr() );
257 fprintf( stdout
, "%s", sEntry
.getStr() );
262 //--------------------------------------------------------------------
263 void SAL_CALL
ConsoleHandler::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
)
265 ::osl::MutexGuard
aGuard( m_aMutex
);
267 if ( m_aHandlerHelper
.getIsInitialized() )
268 throw AlreadyInitializedException();
270 if ( _rArguments
.getLength() == 0 )
271 { // create() - nothing to init
272 m_aHandlerHelper
.setIsInitialized();
276 if ( _rArguments
.getLength() != 1 )
277 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
279 Sequence
< NamedValue
> aSettings
;
280 if ( !( _rArguments
[0] >>= aSettings
) )
281 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
283 // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
284 ::comphelper::NamedValueCollection
aTypedSettings( aSettings
);
285 m_aHandlerHelper
.initFromSettings( aTypedSettings
);
287 aTypedSettings
.get_ensureType( "Threshold", m_nThreshold
);
289 m_aHandlerHelper
.setIsInitialized();
292 //--------------------------------------------------------------------
293 ::rtl::OUString SAL_CALL
ConsoleHandler::getImplementationName() throw(RuntimeException
)
295 return getImplementationName_static();
298 //--------------------------------------------------------------------
299 ::sal_Bool SAL_CALL
ConsoleHandler::supportsService( const ::rtl::OUString
& _rServiceName
) throw(RuntimeException
)
301 const Sequence
< ::rtl::OUString
> aServiceNames( getSupportedServiceNames() );
302 for ( const ::rtl::OUString
* pServiceNames
= aServiceNames
.getConstArray();
303 pServiceNames
!= aServiceNames
.getConstArray() + aServiceNames
.getLength();
306 if ( _rServiceName
== *pServiceNames
)
311 //--------------------------------------------------------------------
312 Sequence
< ::rtl::OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames() throw(RuntimeException
)
314 return getSupportedServiceNames_static();
317 //--------------------------------------------------------------------
318 ::rtl::OUString SAL_CALL
ConsoleHandler::getImplementationName_static()
320 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.ConsoleHandler" ) );
323 //--------------------------------------------------------------------
324 Sequence
< ::rtl::OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames_static()
326 Sequence
< ::rtl::OUString
> aServiceNames(1);
327 aServiceNames
[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.ConsoleHandler" ) );
328 return aServiceNames
;
331 //--------------------------------------------------------------------
332 Reference
< XInterface
> ConsoleHandler::Create( const Reference
< XComponentContext
>& _rxContext
)
334 return *( new ConsoleHandler( _rxContext
) );
337 //--------------------------------------------------------------------
338 void createRegistryInfo_ConsoleHandler()
340 static OAutoRegistration
< ConsoleHandler
> aAutoRegistration
;
343 //........................................................................
344 } // namespace logging
345 //........................................................................