update dev300-m58
[ooovba.git] / extensions / source / logging / loghandler.cxx
blob1d66122b73073384321500a7d0bae7b592b0cb84
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: loghandler.cxx,v $
10 * $Revision: 1.3 $
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 #ifndef EXTENSIONS_LOGHANDLER_HXX
35 #include "loghandler.hxx"
36 #endif
38 /** === begin UNO includes === **/
39 #include <com/sun/star/logging/LogLevel.hpp>
40 #include <com/sun/star/lang/IllegalArgumentException.hpp>
41 #include <com/sun/star/lang/DisposedException.hpp>
42 #include <com/sun/star/logging/PlainTextFormatter.hpp>
43 /** === end UNO includes === **/
45 #include <tools/diagnose_ex.h>
46 #include <comphelper/componentcontext.hxx>
47 #include <rtl/tencinfo.h>
49 //........................................................................
50 namespace logging
52 //........................................................................
54 /** === begin UNO using === **/
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::uno::XComponentContext;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::logging::LogRecord;
59 using ::com::sun::star::uno::UNO_QUERY_THROW;
60 using ::com::sun::star::logging::XLogFormatter;
61 using ::com::sun::star::uno::Exception;
62 using ::com::sun::star::lang::IllegalArgumentException;
63 using ::com::sun::star::lang::DisposedException;
64 using ::com::sun::star::logging::PlainTextFormatter;
65 /** === end UNO using === **/
66 namespace LogLevel = ::com::sun::star::logging::LogLevel;
68 //====================================================================
69 //= LogHandlerHelper
70 //====================================================================
71 //--------------------------------------------------------------------
72 LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
73 :m_eEncoding( RTL_TEXTENCODING_UTF8 )
74 ,m_nLevel( LogLevel::SEVERE )
75 ,m_xFormatter( NULL )
76 ,m_xContext( _rxContext )
77 ,m_rMutex( _rMutex )
78 ,m_rBHelper( _rBHelper )
79 ,m_bInitialized( false )
83 //--------------------------------------------------------------------
84 void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
86 ::rtl::OUString sEncoding;
87 if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
89 if ( !setEncoding( sEncoding ) )
90 throw IllegalArgumentException();
93 _rSettings.get_ensureType( "Formatter", m_xFormatter );
94 _rSettings.get_ensureType( "Level", m_nLevel );
97 //--------------------------------------------------------------------
98 void LogHandlerHelper::enterMethod()
100 m_rMutex.acquire();
102 if ( !getIsInitialized() )
103 throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component not initialized" ) ), NULL );
105 if ( m_rBHelper.bDisposed )
106 throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component already disposed" ) ), NULL );
108 // fallback settings, in case they weren't passed at construction time
109 if ( !getFormatter().is() )
113 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW );
114 setFormatter( xFormatter );
116 catch( const Exception& )
118 DBG_UNHANDLED_EXCEPTION();
123 //--------------------------------------------------------------------
124 bool LogHandlerHelper::getEncoding( ::rtl::OUString& _out_rEncoding ) const
126 const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
127 if ( pMimeCharset )
129 _out_rEncoding = ::rtl::OUString::createFromAscii( pMimeCharset );
130 return true;
132 _out_rEncoding = ::rtl::OUString();
133 return false;
136 //--------------------------------------------------------------------
137 bool LogHandlerHelper::setEncoding( const ::rtl::OUString& _rEncoding )
139 ::rtl::OString sAsciiEncoding( ::rtl::OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
140 rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
141 if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
143 m_eEncoding = eEncoding;
144 return true;
146 return false;
149 //--------------------------------------------------------------------
150 bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, ::rtl::OString& _out_rEntry ) const
152 if ( _rRecord.Level < getLevel() )
153 // not to be published due to low level
154 return false;
158 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
159 ::rtl::OUString sEntry( xFormatter->format( _rRecord ) );
160 _out_rEntry = ::rtl::OUStringToOString( sEntry, getTextEncoding() );
161 return true;
163 catch( const Exception& )
165 DBG_UNHANDLED_EXCEPTION();
167 return false;
170 //--------------------------------------------------------------------
171 bool LogHandlerHelper::getEncodedHead( ::rtl::OString& _out_rHead ) const
175 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
176 ::rtl::OUString sHead( xFormatter->getHead() );
177 _out_rHead = ::rtl::OUStringToOString( sHead, getTextEncoding() );
178 return true;
180 catch( const Exception& )
182 DBG_UNHANDLED_EXCEPTION();
184 return false;
187 //--------------------------------------------------------------------
188 bool LogHandlerHelper::getEncodedTail( ::rtl::OString& _out_rTail ) const
192 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
193 ::rtl::OUString sTail( xFormatter->getTail() );
194 _out_rTail = ::rtl::OUStringToOString( sTail, getTextEncoding() );
195 return true;
197 catch( const Exception& )
199 DBG_UNHANDLED_EXCEPTION();
201 return false;
204 //........................................................................
205 } // namespace logging
206 //........................................................................