Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / extensions / source / logging / loghandler.cxx
bloba398bd053acdcd2ee1059b8995f350cad5d7aa69
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 <comphelper/diagnose_ex.hxx>
29 #include <rtl/tencinfo.h>
32 namespace logging
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XComponentContext;
38 using ::com::sun::star::logging::LogRecord;
39 using ::com::sun::star::logging::XLogFormatter;
40 using ::com::sun::star::uno::Exception;
41 using ::com::sun::star::lang::IllegalArgumentException;
42 using ::com::sun::star::lang::DisposedException;
43 using ::com::sun::star::logging::PlainTextFormatter;
45 namespace LogLevel = ::com::sun::star::logging::LogLevel;
47 LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
48 :m_eEncoding( RTL_TEXTENCODING_UTF8 )
49 ,m_nLevel( LogLevel::SEVERE )
50 ,m_xContext( _rxContext )
51 ,m_rMutex( _rMutex )
52 ,m_rBHelper( _rBHelper )
53 ,m_bInitialized( false )
58 void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
60 OUString sEncoding;
61 if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
63 if ( !setEncoding( sEncoding ) )
64 throw IllegalArgumentException();
67 _rSettings.get_ensureType( "Formatter", m_xFormatter );
68 _rSettings.get_ensureType( "Level", m_nLevel );
72 void LogHandlerHelper::enterMethod()
74 m_rMutex.acquire();
76 if ( !m_bInitialized )
77 throw DisposedException("component not initialized" );
79 if ( m_rBHelper.bDisposed )
80 throw DisposedException("component already disposed" );
82 // fallback settings, in case they weren't passed at construction time
83 if ( !getFormatter().is() )
85 try
87 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), css::uno::UNO_SET_THROW );
88 setFormatter( xFormatter );
90 catch( const Exception& )
92 DBG_UNHANDLED_EXCEPTION("extensions.logging");
98 bool LogHandlerHelper::getEncoding( OUString& _out_rEncoding ) const
100 const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
101 if ( pMimeCharset )
103 _out_rEncoding = OUString::createFromAscii( pMimeCharset );
104 return true;
106 _out_rEncoding.clear();
107 return false;
111 bool LogHandlerHelper::setEncoding( std::u16string_view _rEncoding )
113 OString sAsciiEncoding( OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
114 rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
115 if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
117 m_eEncoding = eEncoding;
118 return true;
120 return false;
124 bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, OString& _out_rEntry ) const
126 if ( _rRecord.Level < getLevel() )
127 // not to be published due to low level
128 return false;
132 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
133 OUString sEntry( xFormatter->format( _rRecord ) );
134 _out_rEntry = OUStringToOString( sEntry, getTextEncoding() );
135 return true;
137 catch( const Exception& )
139 DBG_UNHANDLED_EXCEPTION("extensions.logging");
141 return false;
145 bool LogHandlerHelper::getEncodedHead( OString& _out_rHead ) const
149 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
150 OUString sHead( xFormatter->getHead() );
151 _out_rHead = OUStringToOString( sHead, getTextEncoding() );
152 return true;
154 catch( const Exception& )
156 DBG_UNHANDLED_EXCEPTION("extensions.logging");
158 return false;
162 bool LogHandlerHelper::getEncodedTail( OString& _out_rTail ) const
166 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
167 OUString sTail( xFormatter->getTail() );
168 _out_rTail = OUStringToOString( sTail, getTextEncoding() );
169 return true;
171 catch( const Exception& )
173 DBG_UNHANDLED_EXCEPTION("extensions.logging");
175 return false;
179 } // namespace logging
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */