bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / logging / filehandler.cxx
blobf4df55a03a805970d979b868f692fa46c257ba95
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 "log_module.hxx"
22 #include "methodguard.hxx"
23 #include "loghandler.hxx"
25 #include <com/sun/star/logging/XLogHandler.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/util/PathSubstitution.hpp>
31 #include <com/sun/star/util/XStringSubstitution.hpp>
33 #include <tools/diagnose_ex.h>
35 #include <comphelper/componentcontext.hxx>
37 #include <cppuhelper/compbase3.hxx>
38 #include <cppuhelper/basemutex.hxx>
40 #include <osl/thread.h>
41 #include <osl/file.hxx>
43 #include <rtl/strbuf.hxx>
45 #include <memory>
47 //........................................................................
48 namespace logging
50 //........................................................................
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::logging::LogRecord;
54 using ::com::sun::star::uno::RuntimeException;
55 using ::com::sun::star::logging::XLogFormatter;
56 using ::com::sun::star::uno::Sequence;
57 using ::com::sun::star::uno::XInterface;
58 using ::com::sun::star::uno::XComponentContext;
59 using ::com::sun::star::logging::XLogHandler;
60 using ::com::sun::star::lang::XServiceInfo;
61 using ::com::sun::star::ucb::AlreadyInitializedException;
62 using ::com::sun::star::lang::XInitialization;
63 using ::com::sun::star::uno::Any;
64 using ::com::sun::star::uno::Exception;
65 using ::com::sun::star::lang::IllegalArgumentException;
66 using ::com::sun::star::uno::UNO_QUERY_THROW;
67 using ::com::sun::star::util::PathSubstitution;
68 using ::com::sun::star::util::XStringSubstitution;
69 using ::com::sun::star::beans::NamedValue;
71 //====================================================================
72 //= FileHandler - declaration
73 //====================================================================
74 typedef ::cppu::WeakComponentImplHelper3 < XLogHandler
75 , XServiceInfo
76 , XInitialization
77 > FileHandler_Base;
78 class FileHandler :public ::cppu::BaseMutex
79 ,public FileHandler_Base
81 private:
82 enum FileValidity
84 /// never attempted to open the file
85 eUnknown,
86 /// file is valid
87 eValid,
88 /// file is invalid
89 eInvalid
92 private:
93 ::comphelper::ComponentContext m_aContext;
94 LogHandlerHelper m_aHandlerHelper;
95 OUString m_sFileURL;
96 ::std::auto_ptr< ::osl::File > m_pFile;
97 FileValidity m_eFileValidity;
99 protected:
100 FileHandler( const Reference< XComponentContext >& _rxContext );
101 virtual ~FileHandler();
103 // XLogHandler
104 virtual OUString SAL_CALL getEncoding() throw (RuntimeException);
105 virtual void SAL_CALL setEncoding( const OUString& _encoding ) throw (RuntimeException);
106 virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException);
107 virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException);
108 virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
109 virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
110 virtual void SAL_CALL flush( ) throw (RuntimeException);
111 virtual ::sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException);
113 // XInitialization
114 virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
116 // XServiceInfo
117 virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
118 virtual ::sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException);
119 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
121 // OComponentHelper
122 virtual void SAL_CALL disposing();
124 public:
125 // XServiceInfo - static version
126 static OUString SAL_CALL getImplementationName_static();
127 static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
128 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
130 public:
131 typedef ComponentMethodGuard< FileHandler > MethodGuard;
132 void enterMethod( MethodGuard::Access );
133 void leaveMethod( MethodGuard::Access );
135 private:
136 /** prepares our output file for writing
138 bool impl_prepareFile_nothrow();
140 /// writes the given string to our file
141 void impl_writeString_nothrow( const OString& _rEntry );
143 /** does string substitution on a (usually externally provided) file url
145 void impl_doStringsubstitution_nothrow( OUString& _inout_rURL );
148 //====================================================================
149 //= FileHandler - implementation
150 //====================================================================
151 //--------------------------------------------------------------------
152 FileHandler::FileHandler( const Reference< XComponentContext >& _rxContext )
153 :FileHandler_Base( m_aMutex )
154 ,m_aContext( _rxContext )
155 ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
156 ,m_sFileURL( )
157 ,m_pFile( )
158 ,m_eFileValidity( eUnknown )
162 //--------------------------------------------------------------------
163 FileHandler::~FileHandler()
165 if ( !rBHelper.bDisposed )
167 acquire();
168 dispose();
172 //--------------------------------------------------------------------
173 bool FileHandler::impl_prepareFile_nothrow()
175 if ( m_eFileValidity == eUnknown )
177 m_pFile.reset( new ::osl::File( m_sFileURL ) );
178 // check whether the log file already exists
179 ::osl::DirectoryItem aFileItem;
180 ::osl::DirectoryItem::get( m_sFileURL, aFileItem );
181 ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
182 if ( ::osl::FileBase::E_None == aFileItem.getFileStatus( aStatus ) )
183 ::osl::File::remove( m_sFileURL );
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 )
192 OStringBuffer sMessage;
193 sMessage.append( "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:" );
194 sMessage.append( "\nURL: " );
195 sMessage.append( OString( m_sFileURL.getStr(), m_sFileURL.getLength(), osl_getThreadTextEncoding() ) );
196 sMessage.append( "\nerror code: " );
197 sMessage.append( (sal_Int32)res );
198 OSL_FAIL( sMessage.makeStringAndClear().getStr() );
200 #endif
201 if ( m_eFileValidity == eValid )
203 OString sHead;
204 if ( m_aHandlerHelper.getEncodedHead( sHead ) )
205 impl_writeString_nothrow( sHead );
209 return m_eFileValidity == eValid;
212 //--------------------------------------------------------------------
213 void FileHandler::impl_writeString_nothrow( const OString& _rEntry )
215 OSL_PRECOND( m_pFile.get(), "FileHandler::impl_writeString_nothrow: no file!" );
217 sal_uInt64 nBytesToWrite( _rEntry.getLength() );
218 sal_uInt64 nBytesWritten( 0 );
219 #if OSL_DEBUG_LEVEL > 0
220 ::osl::FileBase::RC res =
221 #endif
222 m_pFile->write( _rEntry.getStr(), nBytesToWrite, nBytesWritten );
223 OSL_ENSURE( ( res == ::osl::FileBase::E_None ) && ( nBytesWritten == nBytesToWrite ),
224 "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
227 //--------------------------------------------------------------------
228 void FileHandler::impl_doStringsubstitution_nothrow( OUString& _inout_rURL )
232 Reference< XStringSubstitution > xStringSubst(PathSubstitution::create(m_aContext.getUNOContext()));
233 _inout_rURL = xStringSubst->substituteVariables( _inout_rURL, true );
235 catch( const Exception& )
237 DBG_UNHANDLED_EXCEPTION();
241 //--------------------------------------------------------------------
242 void SAL_CALL FileHandler::disposing()
244 if ( m_eFileValidity == eValid )
246 OString sTail;
247 if ( m_aHandlerHelper.getEncodedTail( sTail ) )
248 impl_writeString_nothrow( sTail );
251 m_pFile.reset( NULL );
252 m_aHandlerHelper.setFormatter( NULL );
255 //--------------------------------------------------------------------
256 void FileHandler::enterMethod( MethodGuard::Access )
258 m_aHandlerHelper.enterMethod();
261 //--------------------------------------------------------------------
262 void FileHandler::leaveMethod( MethodGuard::Access )
264 m_aMutex.release();
267 //--------------------------------------------------------------------
268 OUString SAL_CALL FileHandler::getEncoding() throw (RuntimeException)
270 MethodGuard aGuard( *this );
271 OUString sEncoding;
272 OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
273 return sEncoding;
276 //--------------------------------------------------------------------
277 void SAL_CALL FileHandler::setEncoding( const OUString& _rEncoding ) throw (RuntimeException)
279 MethodGuard aGuard( *this );
280 OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
283 //--------------------------------------------------------------------
284 Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter() throw (RuntimeException)
286 MethodGuard aGuard( *this );
287 return m_aHandlerHelper.getFormatter();
290 //--------------------------------------------------------------------
291 void SAL_CALL FileHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException)
293 MethodGuard aGuard( *this );
294 m_aHandlerHelper.setFormatter( _rxFormatter );
297 //--------------------------------------------------------------------
298 ::sal_Int32 SAL_CALL FileHandler::getLevel() throw (RuntimeException)
300 MethodGuard aGuard( *this );
301 return m_aHandlerHelper.getLevel();
304 //--------------------------------------------------------------------
305 void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException)
307 MethodGuard aGuard( *this );
308 m_aHandlerHelper.setLevel( _nLevel );
311 //--------------------------------------------------------------------
312 void SAL_CALL FileHandler::flush( ) throw (RuntimeException)
314 MethodGuard aGuard( *this );
315 if(!m_pFile.get())
317 OSL_PRECOND(false, "FileHandler::flush: no file!");
318 return;
320 #if OSL_DEBUG_LEVEL > 0
321 ::osl::FileBase::RC res =
322 #endif
323 m_pFile->sync();
324 OSL_ENSURE(res == ::osl::FileBase::E_None, "FileHandler::flush: Could not sync logfile to filesystem.");
327 //--------------------------------------------------------------------
328 ::sal_Bool SAL_CALL FileHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException)
330 MethodGuard aGuard( *this );
332 if ( !impl_prepareFile_nothrow() )
333 return sal_False;
335 OString sEntry;
336 if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
337 return sal_False;
339 impl_writeString_nothrow( sEntry );
340 return sal_True;
343 //--------------------------------------------------------------------
344 void SAL_CALL FileHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
346 ::osl::MutexGuard aGuard( m_aMutex );
348 if ( m_aHandlerHelper.getIsInitialized() )
349 throw AlreadyInitializedException();
351 if ( _rArguments.getLength() != 1 )
352 throw IllegalArgumentException( OUString(), *this, 1 );
354 Sequence< NamedValue > aSettings;
355 if ( _rArguments[0] >>= m_sFileURL )
357 // create( [in] string URL );
358 impl_doStringsubstitution_nothrow( m_sFileURL );
360 else if ( _rArguments[0] >>= aSettings )
362 // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
363 ::comphelper::NamedValueCollection aTypedSettings( aSettings );
364 m_aHandlerHelper.initFromSettings( aTypedSettings );
366 if ( aTypedSettings.get_ensureType( "FileURL", m_sFileURL ) )
367 impl_doStringsubstitution_nothrow( m_sFileURL );
369 else
370 throw IllegalArgumentException( OUString(), *this, 1 );
372 m_aHandlerHelper.setIsInitialized();
375 //--------------------------------------------------------------------
376 OUString SAL_CALL FileHandler::getImplementationName() throw(RuntimeException)
378 return getImplementationName_static();
381 //--------------------------------------------------------------------
382 ::sal_Bool SAL_CALL FileHandler::supportsService( const OUString& _rServiceName ) throw(RuntimeException)
384 const Sequence< OUString > aServiceNames( getSupportedServiceNames() );
385 for ( const OUString* pServiceNames = aServiceNames.getConstArray();
386 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
387 ++pServiceNames
389 if ( _rServiceName == *pServiceNames )
390 return sal_True;
391 return sal_False;
394 //--------------------------------------------------------------------
395 Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames() throw(RuntimeException)
397 return getSupportedServiceNames_static();
400 //--------------------------------------------------------------------
401 OUString SAL_CALL FileHandler::getImplementationName_static()
403 return OUString( "com.sun.star.comp.extensions.FileHandler" );
406 //--------------------------------------------------------------------
407 Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames_static()
409 Sequence< OUString > aServiceNames(1);
410 aServiceNames[0] = OUString( "com.sun.star.logging.FileHandler" );
411 return aServiceNames;
414 //--------------------------------------------------------------------
415 Reference< XInterface > FileHandler::Create( const Reference< XComponentContext >& _rxContext )
417 return *( new FileHandler( _rxContext ) );
420 //--------------------------------------------------------------------
421 void createRegistryInfo_FileHandler()
423 static OAutoRegistration< FileHandler > aAutoRegistration;
426 //........................................................................
427 } // namespace logging
428 //........................................................................
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */