LibreOffice Module extensions (master) 1
filehandler.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#include <sal/log.hxx>
22
23#include "methodguard.hxx"
24#include "loghandler.hxx"
25
26#include <com/sun/star/logging/XLogHandler.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <com/sun/star/util/PathSubstitution.hpp>
30#include <com/sun/star/util/XStringSubstitution.hpp>
31
33
37
38#include <osl/file.hxx>
39
40#include <memory>
41
42namespace logging
43{
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::logging::LogRecord;
46 using ::com::sun::star::logging::XLogFormatter;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::uno::XInterface;
49 using ::com::sun::star::uno::XComponentContext;
50 using ::com::sun::star::logging::XLogHandler;
51 using ::com::sun::star::lang::XServiceInfo;
52 using ::com::sun::star::uno::Exception;
53 using ::com::sun::star::lang::IllegalArgumentException;
54 using ::com::sun::star::util::PathSubstitution;
55 using ::com::sun::star::util::XStringSubstitution;
56 using ::com::sun::star::beans::NamedValue;
57
58 typedef ::cppu::WeakComponentImplHelper < XLogHandler
59 , XServiceInfo
61
62 namespace {
63
64 class FileHandler :public ::cppu::BaseMutex
65 ,public FileHandler_Base
66 {
67 private:
68 enum FileValidity
69 {
73 eValid,
75 eInvalid
76 };
77
78 Reference<XComponentContext> m_xContext;
79 LogHandlerHelper m_aHandlerHelper;
80 OUString m_sFileURL;
81 std::unique_ptr< ::osl::File > m_pFile;
82 FileValidity m_eFileValidity;
83
84 public:
85 FileHandler(const css::uno::Reference<XComponentContext> &context,
86 const css::uno::Sequence<css::uno::Any> &arguments);
87 virtual ~FileHandler() override;
88
89 private:
90 // XLogHandler
91 virtual OUString SAL_CALL getEncoding() override;
92 virtual void SAL_CALL setEncoding( const OUString& _encoding ) override;
93 virtual Reference< XLogFormatter > SAL_CALL getFormatter() override;
94 virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) override;
95 virtual ::sal_Int32 SAL_CALL getLevel() override;
96 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) override;
97 virtual void SAL_CALL flush( ) override;
98 virtual sal_Bool SAL_CALL publish( const LogRecord& Record ) override;
99
100 // XServiceInfo
101 virtual OUString SAL_CALL getImplementationName() override;
102 virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) override;
103 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
104
105 // OComponentHelper
106 virtual void SAL_CALL disposing() override;
107
108 public:
109 typedef ComponentMethodGuard< FileHandler > MethodGuard;
110 void enterMethod( MethodGuard::Access );
111 void leaveMethod( MethodGuard::Access );
112
113 private:
116 bool impl_prepareFile_nothrow();
117
119 void impl_writeString_nothrow( const OString& _rEntry );
120
123 void impl_doStringsubstitution_nothrow( OUString& _inout_rURL );
124 };
125
126 }
127
128 FileHandler::FileHandler(const css::uno::Reference<XComponentContext> &context,
129 const css::uno::Sequence<css::uno::Any> &arguments)
131 ,m_xContext( context )
132 ,m_aHandlerHelper( context, m_aMutex, rBHelper )
134 {
135 ::osl::MutexGuard aGuard( m_aMutex );
136
137 if ( arguments.getLength() != 1 )
138 throw IllegalArgumentException( OUString(), *this, 1 );
139
140 Sequence< NamedValue > aSettings;
141 if ( arguments[0] >>= m_sFileURL )
142 {
143 // create( [in] string URL );
144 impl_doStringsubstitution_nothrow( m_sFileURL );
145 }
146 else if ( arguments[0] >>= aSettings )
147 {
148 // createWithSettings( [in] sequence< css::beans::NamedValue > Settings )
149 ::comphelper::NamedValueCollection aTypedSettings( aSettings );
150 m_aHandlerHelper.initFromSettings( aTypedSettings );
151
152 if ( aTypedSettings.get_ensureType( "FileURL", m_sFileURL ) )
153 impl_doStringsubstitution_nothrow( m_sFileURL );
154 }
155 else
156 throw IllegalArgumentException( OUString(), *this, 1 );
157
158 m_aHandlerHelper.setIsInitialized();
159 }
160
161 FileHandler::~FileHandler()
162 {
163 if ( !rBHelper.bDisposed )
164 {
165 acquire();
166 dispose();
167 }
168 }
169
170
171 bool FileHandler::impl_prepareFile_nothrow()
172 {
173 if ( m_eFileValidity == eUnknown )
174 {
175 m_pFile.reset( new ::osl::File( m_sFileURL ) );
176 // check whether the log file already exists
177 ::osl::DirectoryItem aFileItem;
178 if (osl::FileBase::E_None == ::osl::DirectoryItem::get(m_sFileURL, aFileItem))
179 {
180 ::osl::FileStatus aStatus(osl_FileStatus_Mask_Validate);
181 if (::osl::FileBase::E_None == aFileItem.getFileStatus(aStatus))
182 ::osl::File::remove(m_sFileURL);
183 }
184
185 ::osl::FileBase::RC res = m_pFile->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
186 m_eFileValidity = res == ::osl::FileBase::E_None
187 ? eValid
188 : eInvalid;
189 #if OSL_DEBUG_LEVEL > 0
190 if ( m_eFileValidity == eInvalid )
191 {
192 SAL_WARN( "extensions.logging", "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:"
193 "\nURL: " << m_sFileURL
194 << "\nerror code: " << static_cast<sal_Int32>(res) );
195 }
196 #endif
197 if ( m_eFileValidity == eValid )
198 {
199 OString sHead;
200 if ( m_aHandlerHelper.getEncodedHead( sHead ) )
201 impl_writeString_nothrow( sHead );
202 }
203 }
204
205 return m_eFileValidity == eValid;
206 }
207
208
209 void FileHandler::impl_writeString_nothrow( const OString& _rEntry )
210 {
211 OSL_PRECOND(m_pFile, "FileHandler::impl_writeString_nothrow: no file!");
212
213 sal_uInt64 nBytesToWrite( _rEntry.getLength() );
214 sal_uInt64 nBytesWritten( 0 );
215 ::osl::FileBase::RC res =
216 m_pFile->write( _rEntry.getStr(), nBytesToWrite, nBytesWritten );
217 OSL_ENSURE( ( res == ::osl::FileBase::E_None ) && ( nBytesWritten == nBytesToWrite ),
218 "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
219 }
220
221
222 void FileHandler::impl_doStringsubstitution_nothrow( OUString& _inout_rURL )
223 {
224 try
225 {
226 Reference< XStringSubstitution > xStringSubst(PathSubstitution::create(m_xContext));
227 _inout_rURL = xStringSubst->substituteVariables( _inout_rURL, true );
228 }
229 catch( const Exception& )
230 {
231 DBG_UNHANDLED_EXCEPTION("extensions.logging");
232 }
233 }
234
235
236 void SAL_CALL FileHandler::disposing()
237 {
238 if ( m_eFileValidity == eValid )
239 {
240 OString sTail;
241 if ( m_aHandlerHelper.getEncodedTail( sTail ) )
242 impl_writeString_nothrow( sTail );
243 }
244
245 m_pFile.reset();
246 m_aHandlerHelper.setFormatter( nullptr );
247 }
248
249
250 void FileHandler::enterMethod( MethodGuard::Access )
251 {
252 m_aHandlerHelper.enterMethod();
253 }
254
255
256 void FileHandler::leaveMethod( MethodGuard::Access )
257 {
258 m_aMutex.release();
259 }
260
261
262 OUString SAL_CALL FileHandler::getEncoding()
263 {
264 MethodGuard aGuard( *this );
265 OUString sEncoding;
266 OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
267 return sEncoding;
268 }
269
270
271 void SAL_CALL FileHandler::setEncoding( const OUString& _rEncoding )
272 {
273 MethodGuard aGuard( *this );
274 OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
275 }
276
277
278 Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter()
279 {
280 MethodGuard aGuard( *this );
281 return m_aHandlerHelper.getFormatter();
282 }
283
284
285 void SAL_CALL FileHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter )
286 {
287 MethodGuard aGuard( *this );
288 m_aHandlerHelper.setFormatter( _rxFormatter );
289 }
290
291
292 ::sal_Int32 SAL_CALL FileHandler::getLevel()
293 {
294 MethodGuard aGuard( *this );
295 return m_aHandlerHelper.getLevel();
296 }
297
298
299 void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel )
300 {
301 MethodGuard aGuard( *this );
302 m_aHandlerHelper.setLevel( _nLevel );
303 }
304
305
306 void SAL_CALL FileHandler::flush( )
307 {
308 MethodGuard aGuard( *this );
309 if (!m_pFile)
310 {
311 OSL_PRECOND(false, "FileHandler::flush: no file!");
312 return;
313 }
314 ::osl::FileBase::RC res = m_pFile->sync();
315 OSL_ENSURE(res == ::osl::FileBase::E_None, "FileHandler::flush: Could not sync logfile to filesystem.");
316 }
317
318
319 sal_Bool SAL_CALL FileHandler::publish( const LogRecord& _rRecord )
320 {
321 MethodGuard aGuard( *this );
322
323 if ( !impl_prepareFile_nothrow() )
324 return false;
325
326 OString sEntry;
327 if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
328 return false;
329
330 impl_writeString_nothrow( sEntry );
331 return true;
332 }
333
334 OUString SAL_CALL FileHandler::getImplementationName()
335 {
336 return "com.sun.star.comp.extensions.FileHandler";
337 }
338
339 sal_Bool SAL_CALL FileHandler::supportsService( const OUString& _rServiceName )
340 {
341 return cppu::supportsService(this, _rServiceName);
342 }
343
344 Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames()
345 {
346 return { "com.sun.star.logging.FileHandler" };
347 }
348
349} // namespace logging
350
351extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
353 css::uno::XComponentContext *context,
354 css::uno::Sequence<css::uno::Any> const &arguments)
355{
356 return cppu::acquire(new logging::FileHandler(context, arguments));
357}
358
359/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XComponentContext > m_xContext
Definition: filehandler.cxx:78
LogHandlerHelper m_aHandlerHelper
Definition: filehandler.cxx:79
OUString m_sFileURL
Definition: filehandler.cxx:80
std::unique_ptr< ::osl::File > m_pFile
Definition: filehandler.cxx:81
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_extensions_FileHandler(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &arguments)
FileValidity m_eFileValidity
Definition: filehandler.cxx:82
#define SAL_WARN(area, stream)
::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< XLogHandler, XServiceInfo > FileHandler_Base
Definition: filehandler.cxx:60
void dispose()
unsigned char sal_Bool