bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / logging / loghandler.cxx
blob7acb576c8b5e1b76df5de569722d0bad3e18871b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "loghandler.hxx"
23 #include <com/sun/star/logging/LogLevel.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/logging/PlainTextFormatter.hpp>
28 #include <tools/diagnose_ex.h>
29 #include <comphelper/componentcontext.hxx>
30 #include <rtl/tencinfo.h>
32 //........................................................................
33 namespace logging
35 //........................................................................
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::XComponentContext;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::logging::LogRecord;
41 using ::com::sun::star::uno::UNO_QUERY_THROW;
42 using ::com::sun::star::logging::XLogFormatter;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::lang::IllegalArgumentException;
45 using ::com::sun::star::lang::DisposedException;
46 using ::com::sun::star::logging::PlainTextFormatter;
48 namespace LogLevel = ::com::sun::star::logging::LogLevel;
50 //====================================================================
51 //= LogHandlerHelper
52 //====================================================================
53 //--------------------------------------------------------------------
54 LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
55 :m_eEncoding( RTL_TEXTENCODING_UTF8 )
56 ,m_nLevel( LogLevel::SEVERE )
57 ,m_xFormatter( NULL )
58 ,m_xContext( _rxContext )
59 ,m_rMutex( _rMutex )
60 ,m_rBHelper( _rBHelper )
61 ,m_bInitialized( false )
65 //--------------------------------------------------------------------
66 void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
68 OUString sEncoding;
69 if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
71 if ( !setEncoding( sEncoding ) )
72 throw IllegalArgumentException();
75 _rSettings.get_ensureType( "Formatter", m_xFormatter );
76 _rSettings.get_ensureType( "Level", m_nLevel );
79 //--------------------------------------------------------------------
80 void LogHandlerHelper::enterMethod()
82 m_rMutex.acquire();
84 if ( !getIsInitialized() )
85 throw DisposedException( OUString( "component not initialized" ), NULL );
87 if ( m_rBHelper.bDisposed )
88 throw DisposedException( OUString( "component already disposed" ), NULL );
90 // fallback settings, in case they weren't passed at construction time
91 if ( !getFormatter().is() )
93 try
95 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW );
96 setFormatter( xFormatter );
98 catch( const Exception& )
100 DBG_UNHANDLED_EXCEPTION();
105 //--------------------------------------------------------------------
106 bool LogHandlerHelper::getEncoding( OUString& _out_rEncoding ) const
108 const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
109 if ( pMimeCharset )
111 _out_rEncoding = OUString::createFromAscii( pMimeCharset );
112 return true;
114 _out_rEncoding = OUString();
115 return false;
118 //--------------------------------------------------------------------
119 bool LogHandlerHelper::setEncoding( const OUString& _rEncoding )
121 OString sAsciiEncoding( OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
122 rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
123 if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
125 m_eEncoding = eEncoding;
126 return true;
128 return false;
131 //--------------------------------------------------------------------
132 bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, OString& _out_rEntry ) const
134 if ( _rRecord.Level < getLevel() )
135 // not to be published due to low level
136 return false;
140 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
141 OUString sEntry( xFormatter->format( _rRecord ) );
142 _out_rEntry = OUStringToOString( sEntry, getTextEncoding() );
143 return true;
145 catch( const Exception& )
147 DBG_UNHANDLED_EXCEPTION();
149 return false;
152 //--------------------------------------------------------------------
153 bool LogHandlerHelper::getEncodedHead( OString& _out_rHead ) const
157 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
158 OUString sHead( xFormatter->getHead() );
159 _out_rHead = OUStringToOString( sHead, getTextEncoding() );
160 return true;
162 catch( const Exception& )
164 DBG_UNHANDLED_EXCEPTION();
166 return false;
169 //--------------------------------------------------------------------
170 bool LogHandlerHelper::getEncodedTail( OString& _out_rTail ) const
174 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
175 OUString sTail( xFormatter->getTail() );
176 _out_rTail = OUStringToOString( sTail, getTextEncoding() );
177 return true;
179 catch( const Exception& )
181 DBG_UNHANDLED_EXCEPTION();
183 return false;
186 //........................................................................
187 } // namespace logging
188 //........................................................................
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */