1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove 25*b1cdbd2cSJim Jagielski #include "precompiled_extensions.hxx" 26*b1cdbd2cSJim Jagielski 27*b1cdbd2cSJim Jagielski #ifndef EXTENSIONS_LOGHANDLER_HXX 28*b1cdbd2cSJim Jagielski #include "loghandler.hxx" 29*b1cdbd2cSJim Jagielski #endif 30*b1cdbd2cSJim Jagielski 31*b1cdbd2cSJim Jagielski /** === begin UNO includes === **/ 32*b1cdbd2cSJim Jagielski #include <com/sun/star/logging/LogLevel.hpp> 33*b1cdbd2cSJim Jagielski #include <com/sun/star/lang/IllegalArgumentException.hpp> 34*b1cdbd2cSJim Jagielski #include <com/sun/star/lang/DisposedException.hpp> 35*b1cdbd2cSJim Jagielski #include <com/sun/star/logging/PlainTextFormatter.hpp> 36*b1cdbd2cSJim Jagielski /** === end UNO includes === **/ 37*b1cdbd2cSJim Jagielski 38*b1cdbd2cSJim Jagielski #include <tools/diagnose_ex.h> 39*b1cdbd2cSJim Jagielski #include <comphelper/componentcontext.hxx> 40*b1cdbd2cSJim Jagielski #include <rtl/tencinfo.h> 41*b1cdbd2cSJim Jagielski 42*b1cdbd2cSJim Jagielski //........................................................................ 43*b1cdbd2cSJim Jagielski namespace logging 44*b1cdbd2cSJim Jagielski { 45*b1cdbd2cSJim Jagielski //........................................................................ 46*b1cdbd2cSJim Jagielski 47*b1cdbd2cSJim Jagielski /** === begin UNO using === **/ 48*b1cdbd2cSJim Jagielski using ::com::sun::star::uno::Reference; 49*b1cdbd2cSJim Jagielski using ::com::sun::star::uno::XComponentContext; 50*b1cdbd2cSJim Jagielski using ::com::sun::star::uno::Any; 51*b1cdbd2cSJim Jagielski using ::com::sun::star::logging::LogRecord; 52*b1cdbd2cSJim Jagielski using ::com::sun::star::uno::UNO_QUERY_THROW; 53*b1cdbd2cSJim Jagielski using ::com::sun::star::logging::XLogFormatter; 54*b1cdbd2cSJim Jagielski using ::com::sun::star::uno::Exception; 55*b1cdbd2cSJim Jagielski using ::com::sun::star::lang::IllegalArgumentException; 56*b1cdbd2cSJim Jagielski using ::com::sun::star::lang::DisposedException; 57*b1cdbd2cSJim Jagielski using ::com::sun::star::logging::PlainTextFormatter; 58*b1cdbd2cSJim Jagielski /** === end UNO using === **/ 59*b1cdbd2cSJim Jagielski namespace LogLevel = ::com::sun::star::logging::LogLevel; 60*b1cdbd2cSJim Jagielski 61*b1cdbd2cSJim Jagielski //==================================================================== 62*b1cdbd2cSJim Jagielski //= LogHandlerHelper 63*b1cdbd2cSJim Jagielski //==================================================================== 64*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- LogHandlerHelper(const Reference<XComponentContext> & _rxContext,::osl::Mutex & _rMutex,::cppu::OBroadcastHelper & _rBHelper)65*b1cdbd2cSJim Jagielski LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper ) 66*b1cdbd2cSJim Jagielski :m_eEncoding( RTL_TEXTENCODING_UTF8 ) 67*b1cdbd2cSJim Jagielski ,m_nLevel( LogLevel::SEVERE ) 68*b1cdbd2cSJim Jagielski ,m_xFormatter( NULL ) 69*b1cdbd2cSJim Jagielski ,m_xContext( _rxContext ) 70*b1cdbd2cSJim Jagielski ,m_rMutex( _rMutex ) 71*b1cdbd2cSJim Jagielski ,m_rBHelper( _rBHelper ) 72*b1cdbd2cSJim Jagielski ,m_bInitialized( false ) 73*b1cdbd2cSJim Jagielski { 74*b1cdbd2cSJim Jagielski } 75*b1cdbd2cSJim Jagielski 76*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- initFromSettings(const::comphelper::NamedValueCollection & _rSettings)77*b1cdbd2cSJim Jagielski void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings ) 78*b1cdbd2cSJim Jagielski { 79*b1cdbd2cSJim Jagielski ::rtl::OUString sEncoding; 80*b1cdbd2cSJim Jagielski if ( _rSettings.get_ensureType( "Encoding", sEncoding ) ) 81*b1cdbd2cSJim Jagielski { 82*b1cdbd2cSJim Jagielski if ( !setEncoding( sEncoding ) ) 83*b1cdbd2cSJim Jagielski throw IllegalArgumentException(); 84*b1cdbd2cSJim Jagielski } 85*b1cdbd2cSJim Jagielski 86*b1cdbd2cSJim Jagielski _rSettings.get_ensureType( "Formatter", m_xFormatter ); 87*b1cdbd2cSJim Jagielski _rSettings.get_ensureType( "Level", m_nLevel ); 88*b1cdbd2cSJim Jagielski } 89*b1cdbd2cSJim Jagielski 90*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- enterMethod()91*b1cdbd2cSJim Jagielski void LogHandlerHelper::enterMethod() 92*b1cdbd2cSJim Jagielski { 93*b1cdbd2cSJim Jagielski m_rMutex.acquire(); 94*b1cdbd2cSJim Jagielski 95*b1cdbd2cSJim Jagielski if ( !getIsInitialized() ) 96*b1cdbd2cSJim Jagielski throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component not initialized" ) ), NULL ); 97*b1cdbd2cSJim Jagielski 98*b1cdbd2cSJim Jagielski if ( m_rBHelper.bDisposed ) 99*b1cdbd2cSJim Jagielski throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component already disposed" ) ), NULL ); 100*b1cdbd2cSJim Jagielski 101*b1cdbd2cSJim Jagielski // fallback settings, in case they weren't passed at construction time 102*b1cdbd2cSJim Jagielski if ( !getFormatter().is() ) 103*b1cdbd2cSJim Jagielski { 104*b1cdbd2cSJim Jagielski try 105*b1cdbd2cSJim Jagielski { 106*b1cdbd2cSJim Jagielski Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW ); 107*b1cdbd2cSJim Jagielski setFormatter( xFormatter ); 108*b1cdbd2cSJim Jagielski } 109*b1cdbd2cSJim Jagielski catch( const Exception& ) 110*b1cdbd2cSJim Jagielski { 111*b1cdbd2cSJim Jagielski DBG_UNHANDLED_EXCEPTION(); 112*b1cdbd2cSJim Jagielski } 113*b1cdbd2cSJim Jagielski } 114*b1cdbd2cSJim Jagielski } 115*b1cdbd2cSJim Jagielski 116*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- getEncoding(::rtl::OUString & _out_rEncoding) const117*b1cdbd2cSJim Jagielski bool LogHandlerHelper::getEncoding( ::rtl::OUString& _out_rEncoding ) const 118*b1cdbd2cSJim Jagielski { 119*b1cdbd2cSJim Jagielski const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding ); 120*b1cdbd2cSJim Jagielski if ( pMimeCharset ) 121*b1cdbd2cSJim Jagielski { 122*b1cdbd2cSJim Jagielski _out_rEncoding = ::rtl::OUString::createFromAscii( pMimeCharset ); 123*b1cdbd2cSJim Jagielski return true; 124*b1cdbd2cSJim Jagielski } 125*b1cdbd2cSJim Jagielski _out_rEncoding = ::rtl::OUString(); 126*b1cdbd2cSJim Jagielski return false; 127*b1cdbd2cSJim Jagielski } 128*b1cdbd2cSJim Jagielski 129*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- setEncoding(const::rtl::OUString & _rEncoding)130*b1cdbd2cSJim Jagielski bool LogHandlerHelper::setEncoding( const ::rtl::OUString& _rEncoding ) 131*b1cdbd2cSJim Jagielski { 132*b1cdbd2cSJim Jagielski ::rtl::OString sAsciiEncoding( ::rtl::OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) ); 133*b1cdbd2cSJim Jagielski rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() ); 134*b1cdbd2cSJim Jagielski if ( eEncoding != RTL_TEXTENCODING_DONTKNOW ) 135*b1cdbd2cSJim Jagielski { 136*b1cdbd2cSJim Jagielski m_eEncoding = eEncoding; 137*b1cdbd2cSJim Jagielski return true; 138*b1cdbd2cSJim Jagielski } 139*b1cdbd2cSJim Jagielski return false; 140*b1cdbd2cSJim Jagielski } 141*b1cdbd2cSJim Jagielski 142*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- formatForPublishing(const LogRecord & _rRecord,::rtl::OString & _out_rEntry) const143*b1cdbd2cSJim Jagielski bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, ::rtl::OString& _out_rEntry ) const 144*b1cdbd2cSJim Jagielski { 145*b1cdbd2cSJim Jagielski if ( _rRecord.Level < getLevel() ) 146*b1cdbd2cSJim Jagielski // not to be published due to low level 147*b1cdbd2cSJim Jagielski return false; 148*b1cdbd2cSJim Jagielski 149*b1cdbd2cSJim Jagielski try 150*b1cdbd2cSJim Jagielski { 151*b1cdbd2cSJim Jagielski Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW ); 152*b1cdbd2cSJim Jagielski ::rtl::OUString sEntry( xFormatter->format( _rRecord ) ); 153*b1cdbd2cSJim Jagielski _out_rEntry = ::rtl::OUStringToOString( sEntry, getTextEncoding() ); 154*b1cdbd2cSJim Jagielski return true; 155*b1cdbd2cSJim Jagielski } 156*b1cdbd2cSJim Jagielski catch( const Exception& ) 157*b1cdbd2cSJim Jagielski { 158*b1cdbd2cSJim Jagielski DBG_UNHANDLED_EXCEPTION(); 159*b1cdbd2cSJim Jagielski } 160*b1cdbd2cSJim Jagielski return false; 161*b1cdbd2cSJim Jagielski } 162*b1cdbd2cSJim Jagielski 163*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- getEncodedHead(::rtl::OString & _out_rHead) const164*b1cdbd2cSJim Jagielski bool LogHandlerHelper::getEncodedHead( ::rtl::OString& _out_rHead ) const 165*b1cdbd2cSJim Jagielski { 166*b1cdbd2cSJim Jagielski try 167*b1cdbd2cSJim Jagielski { 168*b1cdbd2cSJim Jagielski Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW ); 169*b1cdbd2cSJim Jagielski ::rtl::OUString sHead( xFormatter->getHead() ); 170*b1cdbd2cSJim Jagielski _out_rHead = ::rtl::OUStringToOString( sHead, getTextEncoding() ); 171*b1cdbd2cSJim Jagielski return true; 172*b1cdbd2cSJim Jagielski } 173*b1cdbd2cSJim Jagielski catch( const Exception& ) 174*b1cdbd2cSJim Jagielski { 175*b1cdbd2cSJim Jagielski DBG_UNHANDLED_EXCEPTION(); 176*b1cdbd2cSJim Jagielski } 177*b1cdbd2cSJim Jagielski return false; 178*b1cdbd2cSJim Jagielski } 179*b1cdbd2cSJim Jagielski 180*b1cdbd2cSJim Jagielski //-------------------------------------------------------------------- getEncodedTail(::rtl::OString & _out_rTail) const181*b1cdbd2cSJim Jagielski bool LogHandlerHelper::getEncodedTail( ::rtl::OString& _out_rTail ) const 182*b1cdbd2cSJim Jagielski { 183*b1cdbd2cSJim Jagielski try 184*b1cdbd2cSJim Jagielski { 185*b1cdbd2cSJim Jagielski Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW ); 186*b1cdbd2cSJim Jagielski ::rtl::OUString sTail( xFormatter->getTail() ); 187*b1cdbd2cSJim Jagielski _out_rTail = ::rtl::OUStringToOString( sTail, getTextEncoding() ); 188*b1cdbd2cSJim Jagielski return true; 189*b1cdbd2cSJim Jagielski } 190*b1cdbd2cSJim Jagielski catch( const Exception& ) 191*b1cdbd2cSJim Jagielski { 192*b1cdbd2cSJim Jagielski DBG_UNHANDLED_EXCEPTION(); 193*b1cdbd2cSJim Jagielski } 194*b1cdbd2cSJim Jagielski return false; 195*b1cdbd2cSJim Jagielski } 196*b1cdbd2cSJim Jagielski 197*b1cdbd2cSJim Jagielski //........................................................................ 198*b1cdbd2cSJim Jagielski } // namespace logging 199*b1cdbd2cSJim Jagielski //........................................................................ 200*b1cdbd2cSJim Jagielski 201