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