LibreOffice Module extensions (master) 1
consolehandler.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include "methodguard.hxx"
23#include "loghandler.hxx"
24
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>
30
34
35#include <stdio.h>
36
37namespace logging
38{
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;
49
50 typedef ::cppu::WeakComponentImplHelper < XConsoleHandler
51 , XServiceInfo
53
54 namespace {
55
56 class ConsoleHandler :public ::cppu::BaseMutex
58 {
59 private:
60 LogHandlerHelper m_aHandlerHelper;
61 sal_Int32 m_nThreshold;
62
63 public:
64 ConsoleHandler(const Reference<XComponentContext> &context,
65 const css::uno::Sequence<css::uno::Any> &arguments);
66 virtual ~ConsoleHandler() override;
67
68 private:
69 // XConsoleHandler
70 virtual ::sal_Int32 SAL_CALL getThreshold() override;
71 virtual void SAL_CALL setThreshold( ::sal_Int32 _threshold ) override;
72
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;
82
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;
87
88 // OComponentHelper
89 virtual void SAL_CALL disposing() override;
90
91 public:
92 typedef ComponentMethodGuard< ConsoleHandler > MethodGuard;
93 void enterMethod( MethodGuard::Access );
94 void leaveMethod( MethodGuard::Access );
95 };
96
97 }
98
99 ConsoleHandler::ConsoleHandler(const Reference<XComponentContext> &context,
100 const css::uno::Sequence<css::uno::Any> &arguments)
102 ,m_aHandlerHelper( context, m_aMutex, rBHelper )
103 ,m_nThreshold( css::logging::LogLevel::SEVERE )
104 {
105 ::osl::MutexGuard aGuard( m_aMutex );
106
107 if ( !arguments.hasElements() )
108 { // create() - nothing to init
109 m_aHandlerHelper.setIsInitialized();
110 return;
111 }
112
113 if ( arguments.getLength() != 1 )
114 throw IllegalArgumentException( OUString(), *this, 1 );
115
116 Sequence< NamedValue > aSettings;
117 if ( !( arguments[0] >>= aSettings ) )
118 throw IllegalArgumentException( OUString(), *this, 1 );
119
120 // createWithSettings( [in] sequence< css::beans::NamedValue > Settings )
121 ::comphelper::NamedValueCollection aTypedSettings( aSettings );
122 m_aHandlerHelper.initFromSettings( aTypedSettings );
123
124 aTypedSettings.get_ensureType( "Threshold", m_nThreshold );
125
126 m_aHandlerHelper.setIsInitialized();
127 }
128
129 ConsoleHandler::~ConsoleHandler()
130 {
131 if ( !rBHelper.bDisposed )
132 {
133 acquire();
134 dispose();
135 }
136 }
137
138
139 void SAL_CALL ConsoleHandler::disposing()
140 {
141 m_aHandlerHelper.setFormatter( nullptr );
142 }
143
144
145 void ConsoleHandler::enterMethod( MethodGuard::Access )
146 {
147 m_aHandlerHelper.enterMethod();
148 }
149
150
151 void ConsoleHandler::leaveMethod( MethodGuard::Access )
152 {
153 m_aMutex.release();
154 }
155
156
157 ::sal_Int32 SAL_CALL ConsoleHandler::getThreshold()
158 {
159 MethodGuard aGuard( *this );
160 return m_nThreshold;
161 }
162
163
164 void SAL_CALL ConsoleHandler::setThreshold( ::sal_Int32 _threshold )
165 {
166 MethodGuard aGuard( *this );
167 m_nThreshold = _threshold;
168 }
169
170
171 OUString SAL_CALL ConsoleHandler::getEncoding()
172 {
173 MethodGuard aGuard( *this );
174 OUString sEncoding;
175 OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
176 return sEncoding;
177 }
178
179
180 void SAL_CALL ConsoleHandler::setEncoding( const OUString& _rEncoding )
181 {
182 MethodGuard aGuard( *this );
183 OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
184 }
185
186
187 Reference< XLogFormatter > SAL_CALL ConsoleHandler::getFormatter()
188 {
189 MethodGuard aGuard( *this );
190 return m_aHandlerHelper.getFormatter();
191 }
192
193
194 void SAL_CALL ConsoleHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter )
195 {
196 MethodGuard aGuard( *this );
197 m_aHandlerHelper.setFormatter( _rxFormatter );
198 }
199
200
201 ::sal_Int32 SAL_CALL ConsoleHandler::getLevel()
202 {
203 MethodGuard aGuard( *this );
204 return m_aHandlerHelper.getLevel();
205 }
206
207
208 void SAL_CALL ConsoleHandler::setLevel( ::sal_Int32 _nLevel )
209 {
210 MethodGuard aGuard( *this );
211 m_aHandlerHelper.setLevel( _nLevel );
212 }
213
214
215 void SAL_CALL ConsoleHandler::flush( )
216 {
217 MethodGuard aGuard( *this );
218 fflush( stdout );
219 fflush( stderr );
220 }
221
222
223 sal_Bool SAL_CALL ConsoleHandler::publish( const LogRecord& _rRecord )
224 {
225 MethodGuard aGuard( *this );
226
227 OString sEntry;
228 if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
229 return false;
230
231 if ( _rRecord.Level >= m_nThreshold )
232 fprintf( stderr, "%s\n", sEntry.getStr() );
233 else
234 fprintf( stdout, "%s\n", sEntry.getStr() );
235
236 return true;
237 }
238
239 OUString SAL_CALL ConsoleHandler::getImplementationName()
240 {
241 return "com.sun.star.comp.extensions.ConsoleHandler";
242 }
243
244 sal_Bool SAL_CALL ConsoleHandler::supportsService( const OUString& _rServiceName )
245 {
246 return cppu::supportsService(this, _rServiceName);
247 }
248
249 Sequence< OUString > SAL_CALL ConsoleHandler::getSupportedServiceNames()
250 {
251 return { "com.sun.star.logging.ConsoleHandler" };
252 }
253
254} // namespace logging
255
256extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
258 css::uno::XComponentContext *context,
259 css::uno::Sequence<css::uno::Any> const &arguments)
260{
261 return cppu::acquire(new logging::ConsoleHandler(context, arguments));
262}
263
264/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_extensions_ConsoleHandler(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &arguments)
LogHandlerHelper m_aHandlerHelper
sal_Int32 m_nThreshold
::osl::Mutex m_aMutex
Definition: logger.cxx:98
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
::cppu::WeakComponentImplHelper< XConsoleHandler, XServiceInfo > ConsoleHandler_Base
void dispose()
unsigned char sal_Bool