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