bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / logging / loghandler.cxx
bloba2022b9d724f1a2c7f38ac77d9f21197251e85c7
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 <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::uno::Any;
39 using ::com::sun::star::logging::LogRecord;
40 using ::com::sun::star::uno::UNO_QUERY_THROW;
41 using ::com::sun::star::logging::XLogFormatter;
42 using ::com::sun::star::uno::Exception;
43 using ::com::sun::star::lang::IllegalArgumentException;
44 using ::com::sun::star::lang::DisposedException;
45 using ::com::sun::star::logging::PlainTextFormatter;
47 namespace LogLevel = ::com::sun::star::logging::LogLevel;
49 LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
50 :m_eEncoding( RTL_TEXTENCODING_UTF8 )
51 ,m_nLevel( LogLevel::SEVERE )
52 ,m_xFormatter( NULL )
53 ,m_xContext( _rxContext )
54 ,m_rMutex( _rMutex )
55 ,m_rBHelper( _rBHelper )
56 ,m_bInitialized( false )
61 void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
63 OUString sEncoding;
64 if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
66 if ( !setEncoding( sEncoding ) )
67 throw IllegalArgumentException();
70 _rSettings.get_ensureType( "Formatter", m_xFormatter );
71 _rSettings.get_ensureType( "Level", m_nLevel );
75 void LogHandlerHelper::enterMethod()
77 m_rMutex.acquire();
79 if ( !getIsInitialized() )
80 throw DisposedException("component not initialized" );
82 if ( m_rBHelper.bDisposed )
83 throw DisposedException("component already disposed" );
85 // fallback settings, in case they weren't passed at construction time
86 if ( !getFormatter().is() )
88 try
90 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW );
91 setFormatter( xFormatter );
93 catch( const Exception& )
95 DBG_UNHANDLED_EXCEPTION();
101 bool LogHandlerHelper::getEncoding( OUString& _out_rEncoding ) const
103 const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
104 if ( pMimeCharset )
106 _out_rEncoding = OUString::createFromAscii( pMimeCharset );
107 return true;
109 _out_rEncoding.clear();
110 return false;
114 bool LogHandlerHelper::setEncoding( const OUString& _rEncoding )
116 OString sAsciiEncoding( OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
117 rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
118 if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
120 m_eEncoding = eEncoding;
121 return true;
123 return false;
127 bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, OString& _out_rEntry ) const
129 if ( _rRecord.Level < getLevel() )
130 // not to be published due to low level
131 return false;
135 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
136 OUString sEntry( xFormatter->format( _rRecord ) );
137 _out_rEntry = OUStringToOString( sEntry, getTextEncoding() );
138 return true;
140 catch( const Exception& )
142 DBG_UNHANDLED_EXCEPTION();
144 return false;
148 bool LogHandlerHelper::getEncodedHead( OString& _out_rHead ) const
152 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
153 OUString sHead( xFormatter->getHead() );
154 _out_rHead = OUStringToOString( sHead, getTextEncoding() );
155 return true;
157 catch( const Exception& )
159 DBG_UNHANDLED_EXCEPTION();
161 return false;
165 bool LogHandlerHelper::getEncodedTail( OString& _out_rTail ) const
169 Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
170 OUString sTail( xFormatter->getTail() );
171 _out_rTail = OUStringToOString( sTail, getTextEncoding() );
172 return true;
174 catch( const Exception& )
176 DBG_UNHANDLED_EXCEPTION();
178 return false;
182 } // namespace logging
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */