Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / logging / consolehandler.cxx
blobd1455baa317828e8076a1f83303ab78416efed9f
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 .
20 #include <sal/config.h>
22 #include "methodguard.hxx"
23 #include "loghandler.hxx"
25 #include <com/sun/star/logging/XConsoleHandler.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/logging/LogLevel.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/beans/NamedValue.hpp>
31 #include <cppuhelper/compbase.hxx>
32 #include <cppuhelper/basemutex.hxx>
33 #include <cppuhelper/supportsservice.hxx>
35 #include <stdio.h>
37 namespace logging
39 using ::com::sun::star::logging::XConsoleHandler;
40 using ::com::sun::star::lang::XServiceInfo;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::XComponentContext;
43 using ::com::sun::star::logging::XLogFormatter;
44 using ::com::sun::star::uno::Sequence;
45 using ::com::sun::star::logging::LogRecord;
46 using ::com::sun::star::uno::XInterface;
47 using ::com::sun::star::lang::IllegalArgumentException;
48 using ::com::sun::star::beans::NamedValue;
50 typedef ::cppu::WeakComponentImplHelper < XConsoleHandler
51 , XServiceInfo
52 > ConsoleHandler_Base;
54 namespace {
56 class ConsoleHandler :public ::cppu::BaseMutex
57 ,public ConsoleHandler_Base
59 private:
60 LogHandlerHelper m_aHandlerHelper;
61 sal_Int32 m_nThreshold;
63 public:
64 ConsoleHandler(const Reference<XComponentContext> &context,
65 const css::uno::Sequence<css::uno::Any> &arguments);
66 virtual ~ConsoleHandler() override;
68 private:
69 // XConsoleHandler
70 virtual ::sal_Int32 SAL_CALL getThreshold() override;
71 virtual void SAL_CALL setThreshold( ::sal_Int32 _threshold ) override;
73 // XLogHandler
74 virtual OUString SAL_CALL getEncoding() override;
75 virtual void SAL_CALL setEncoding( const OUString& _encoding ) override;
76 virtual Reference< XLogFormatter > SAL_CALL getFormatter() override;
77 virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) override;
78 virtual ::sal_Int32 SAL_CALL getLevel() override;
79 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) override;
80 virtual void SAL_CALL flush( ) override;
81 virtual sal_Bool SAL_CALL publish( const LogRecord& Record ) override;
83 // XServiceInfo
84 virtual OUString SAL_CALL getImplementationName() override;
85 virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) override;
86 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
88 // OComponentHelper
89 virtual void SAL_CALL disposing() override;
91 public:
92 typedef ComponentMethodGuard< ConsoleHandler > MethodGuard;
93 void enterMethod( MethodGuard::Access );
94 void leaveMethod( MethodGuard::Access );
99 ConsoleHandler::ConsoleHandler(const Reference<XComponentContext> &context,
100 const css::uno::Sequence<css::uno::Any> &arguments)
101 :ConsoleHandler_Base( m_aMutex )
102 ,m_aHandlerHelper( context, m_aMutex, rBHelper )
103 ,m_nThreshold( css::logging::LogLevel::SEVERE )
105 ::osl::MutexGuard aGuard( m_aMutex );
107 if ( !arguments.hasElements() )
108 { // create() - nothing to init
109 m_aHandlerHelper.setIsInitialized();
110 return;
113 if ( arguments.getLength() != 1 )
114 throw IllegalArgumentException( OUString(), *this, 1 );
116 Sequence< NamedValue > aSettings;
117 if ( !( arguments[0] >>= aSettings ) )
118 throw IllegalArgumentException( OUString(), *this, 1 );
120 // createWithSettings( [in] sequence< css::beans::NamedValue > Settings )
121 ::comphelper::NamedValueCollection aTypedSettings( aSettings );
122 m_aHandlerHelper.initFromSettings( aTypedSettings );
124 aTypedSettings.get_ensureType( "Threshold", m_nThreshold );
126 m_aHandlerHelper.setIsInitialized();
129 ConsoleHandler::~ConsoleHandler()
131 if ( !rBHelper.bDisposed )
133 acquire();
134 dispose();
139 void SAL_CALL ConsoleHandler::disposing()
141 m_aHandlerHelper.setFormatter( nullptr );
145 void ConsoleHandler::enterMethod( MethodGuard::Access )
147 m_aHandlerHelper.enterMethod();
151 void ConsoleHandler::leaveMethod( MethodGuard::Access )
153 m_aMutex.release();
157 ::sal_Int32 SAL_CALL ConsoleHandler::getThreshold()
159 MethodGuard aGuard( *this );
160 return m_nThreshold;
164 void SAL_CALL ConsoleHandler::setThreshold( ::sal_Int32 _threshold )
166 MethodGuard aGuard( *this );
167 m_nThreshold = _threshold;
171 OUString SAL_CALL ConsoleHandler::getEncoding()
173 MethodGuard aGuard( *this );
174 OUString sEncoding;
175 OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
176 return sEncoding;
180 void SAL_CALL ConsoleHandler::setEncoding( const OUString& _rEncoding )
182 MethodGuard aGuard( *this );
183 OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
187 Reference< XLogFormatter > SAL_CALL ConsoleHandler::getFormatter()
189 MethodGuard aGuard( *this );
190 return m_aHandlerHelper.getFormatter();
194 void SAL_CALL ConsoleHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter )
196 MethodGuard aGuard( *this );
197 m_aHandlerHelper.setFormatter( _rxFormatter );
201 ::sal_Int32 SAL_CALL ConsoleHandler::getLevel()
203 MethodGuard aGuard( *this );
204 return m_aHandlerHelper.getLevel();
208 void SAL_CALL ConsoleHandler::setLevel( ::sal_Int32 _nLevel )
210 MethodGuard aGuard( *this );
211 m_aHandlerHelper.setLevel( _nLevel );
215 void SAL_CALL ConsoleHandler::flush( )
217 MethodGuard aGuard( *this );
218 fflush( stdout );
219 fflush( stderr );
223 sal_Bool SAL_CALL ConsoleHandler::publish( const LogRecord& _rRecord )
225 MethodGuard aGuard( *this );
227 OString sEntry;
228 if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
229 return false;
231 if ( _rRecord.Level >= m_nThreshold )
232 fprintf( stderr, "%s\n", sEntry.getStr() );
233 else
234 fprintf( stdout, "%s\n", sEntry.getStr() );
236 return true;
239 OUString SAL_CALL ConsoleHandler::getImplementationName()
241 return "com.sun.star.comp.extensions.ConsoleHandler";
244 sal_Bool SAL_CALL ConsoleHandler::supportsService( const OUString& _rServiceName )
246 return cppu::supportsService(this, _rServiceName);
249 Sequence< OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames()
251 return { "com.sun.star.logging.ConsoleHandler" };
254 } // namespace logging
256 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
257 com_sun_star_comp_extensions_ConsoleHandler(
258 css::uno::XComponentContext *context,
259 css::uno::Sequence<css::uno::Any> const &arguments)
261 return cppu::acquire(new logging::ConsoleHandler(context, arguments));
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */