Line data Source code
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 :
21 : #include "log_module.hxx"
22 : #include "methodguard.hxx"
23 : #include "loghandler.hxx"
24 :
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>
32 :
33 : #include <tools/diagnose_ex.h>
34 :
35 : #include <cppuhelper/compbase3.hxx>
36 : #include <cppuhelper/basemutex.hxx>
37 :
38 : #include <osl/thread.h>
39 : #include <osl/file.hxx>
40 :
41 : #include <rtl/strbuf.hxx>
42 :
43 : #include <memory>
44 :
45 : //........................................................................
46 : namespace logging
47 : {
48 : //........................................................................
49 :
50 : using ::com::sun::star::uno::Reference;
51 : using ::com::sun::star::logging::LogRecord;
52 : using ::com::sun::star::uno::RuntimeException;
53 : using ::com::sun::star::logging::XLogFormatter;
54 : using ::com::sun::star::uno::Sequence;
55 : using ::com::sun::star::uno::XInterface;
56 : using ::com::sun::star::uno::XComponentContext;
57 : using ::com::sun::star::logging::XLogHandler;
58 : using ::com::sun::star::lang::XServiceInfo;
59 : using ::com::sun::star::ucb::AlreadyInitializedException;
60 : using ::com::sun::star::lang::XInitialization;
61 : using ::com::sun::star::uno::Any;
62 : using ::com::sun::star::uno::Exception;
63 : using ::com::sun::star::lang::IllegalArgumentException;
64 : using ::com::sun::star::uno::UNO_QUERY_THROW;
65 : using ::com::sun::star::util::PathSubstitution;
66 : using ::com::sun::star::util::XStringSubstitution;
67 : using ::com::sun::star::beans::NamedValue;
68 :
69 : //====================================================================
70 : //= FileHandler - declaration
71 : //====================================================================
72 : typedef ::cppu::WeakComponentImplHelper3 < XLogHandler
73 : , XServiceInfo
74 : , XInitialization
75 : > FileHandler_Base;
76 : class FileHandler :public ::cppu::BaseMutex
77 : ,public FileHandler_Base
78 : {
79 : private:
80 : enum FileValidity
81 : {
82 : /// never attempted to open the file
83 : eUnknown,
84 : /// file is valid
85 : eValid,
86 : /// file is invalid
87 : eInvalid
88 : };
89 :
90 : private:
91 : Reference<XComponentContext> m_xContext;
92 : LogHandlerHelper m_aHandlerHelper;
93 : OUString m_sFileURL;
94 : ::std::auto_ptr< ::osl::File > m_pFile;
95 : FileValidity m_eFileValidity;
96 :
97 : protected:
98 : FileHandler( const Reference< XComponentContext >& _rxContext );
99 : virtual ~FileHandler();
100 :
101 : // XLogHandler
102 : virtual OUString SAL_CALL getEncoding() throw (RuntimeException);
103 : virtual void SAL_CALL setEncoding( const OUString& _encoding ) throw (RuntimeException);
104 : virtual Reference< XLogFormatter > SAL_CALL getFormatter() throw (RuntimeException);
105 : virtual void SAL_CALL setFormatter( const Reference< XLogFormatter >& _formatter ) throw (RuntimeException);
106 : virtual ::sal_Int32 SAL_CALL getLevel() throw (RuntimeException);
107 : virtual void SAL_CALL setLevel( ::sal_Int32 _level ) throw (RuntimeException);
108 : virtual void SAL_CALL flush( ) throw (RuntimeException);
109 : virtual ::sal_Bool SAL_CALL publish( const LogRecord& Record ) throw (RuntimeException);
110 :
111 : // XInitialization
112 : 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);
113 :
114 : // XServiceInfo
115 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
116 : virtual ::sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException);
117 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
118 :
119 : // OComponentHelper
120 : virtual void SAL_CALL disposing();
121 :
122 : public:
123 : // XServiceInfo - static version
124 : static OUString SAL_CALL getImplementationName_static();
125 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
126 : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
127 :
128 : public:
129 : typedef ComponentMethodGuard< FileHandler > MethodGuard;
130 : void enterMethod( MethodGuard::Access );
131 : void leaveMethod( MethodGuard::Access );
132 :
133 : private:
134 : /** prepares our output file for writing
135 : */
136 : bool impl_prepareFile_nothrow();
137 :
138 : /// writes the given string to our file
139 : void impl_writeString_nothrow( const OString& _rEntry );
140 :
141 : /** does string substitution on a (usually externally provided) file url
142 : */
143 : void impl_doStringsubstitution_nothrow( OUString& _inout_rURL );
144 : };
145 :
146 : //====================================================================
147 : //= FileHandler - implementation
148 : //====================================================================
149 : //--------------------------------------------------------------------
150 5 : FileHandler::FileHandler( const Reference< XComponentContext >& _rxContext )
151 : :FileHandler_Base( m_aMutex )
152 : ,m_xContext( _rxContext )
153 : ,m_aHandlerHelper( _rxContext, m_aMutex, rBHelper )
154 : ,m_sFileURL( )
155 : ,m_pFile( )
156 5 : ,m_eFileValidity( eUnknown )
157 : {
158 5 : }
159 :
160 : //--------------------------------------------------------------------
161 0 : FileHandler::~FileHandler()
162 : {
163 0 : if ( !rBHelper.bDisposed )
164 : {
165 0 : acquire();
166 0 : dispose();
167 : }
168 0 : }
169 :
170 : //--------------------------------------------------------------------
171 0 : bool FileHandler::impl_prepareFile_nothrow()
172 : {
173 0 : if ( m_eFileValidity == eUnknown )
174 : {
175 0 : m_pFile.reset( new ::osl::File( m_sFileURL ) );
176 : // check whether the log file already exists
177 0 : ::osl::DirectoryItem aFileItem;
178 0 : ::osl::DirectoryItem::get( m_sFileURL, aFileItem );
179 0 : ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
180 0 : if ( ::osl::FileBase::E_None == aFileItem.getFileStatus( aStatus ) )
181 0 : ::osl::File::remove( m_sFileURL );
182 :
183 0 : ::osl::FileBase::RC res = m_pFile->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
184 : m_eFileValidity = res == ::osl::FileBase::E_None
185 : ? eValid
186 0 : : eInvalid;
187 : #if OSL_DEBUG_LEVEL > 0
188 : if ( m_eFileValidity == eInvalid )
189 : {
190 : OStringBuffer sMessage;
191 : sMessage.append( "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:" );
192 : sMessage.append( "\nURL: " );
193 : sMessage.append( OString( m_sFileURL.getStr(), m_sFileURL.getLength(), osl_getThreadTextEncoding() ) );
194 : sMessage.append( "\nerror code: " );
195 : sMessage.append( (sal_Int32)res );
196 : OSL_FAIL( sMessage.makeStringAndClear().getStr() );
197 : }
198 : #endif
199 0 : if ( m_eFileValidity == eValid )
200 : {
201 0 : OString sHead;
202 0 : if ( m_aHandlerHelper.getEncodedHead( sHead ) )
203 0 : impl_writeString_nothrow( sHead );
204 0 : }
205 : }
206 :
207 0 : return m_eFileValidity == eValid;
208 : }
209 :
210 : //--------------------------------------------------------------------
211 0 : void FileHandler::impl_writeString_nothrow( const OString& _rEntry )
212 : {
213 : OSL_PRECOND( m_pFile.get(), "FileHandler::impl_writeString_nothrow: no file!" );
214 :
215 0 : sal_uInt64 nBytesToWrite( _rEntry.getLength() );
216 0 : sal_uInt64 nBytesWritten( 0 );
217 : #if OSL_DEBUG_LEVEL > 0
218 : ::osl::FileBase::RC res =
219 : #endif
220 0 : m_pFile->write( _rEntry.getStr(), nBytesToWrite, nBytesWritten );
221 : OSL_ENSURE( ( res == ::osl::FileBase::E_None ) && ( nBytesWritten == nBytesToWrite ),
222 : "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
223 0 : }
224 :
225 : //--------------------------------------------------------------------
226 5 : void FileHandler::impl_doStringsubstitution_nothrow( OUString& _inout_rURL )
227 : {
228 : try
229 : {
230 5 : Reference< XStringSubstitution > xStringSubst(PathSubstitution::create(m_xContext));
231 5 : _inout_rURL = xStringSubst->substituteVariables( _inout_rURL, true );
232 : }
233 0 : catch( const Exception& )
234 : {
235 : DBG_UNHANDLED_EXCEPTION();
236 : }
237 5 : }
238 :
239 : //--------------------------------------------------------------------
240 0 : void SAL_CALL FileHandler::disposing()
241 : {
242 0 : if ( m_eFileValidity == eValid )
243 : {
244 0 : OString sTail;
245 0 : if ( m_aHandlerHelper.getEncodedTail( sTail ) )
246 0 : impl_writeString_nothrow( sTail );
247 : }
248 :
249 0 : m_pFile.reset( NULL );
250 0 : m_aHandlerHelper.setFormatter( NULL );
251 0 : }
252 :
253 : //--------------------------------------------------------------------
254 10 : void FileHandler::enterMethod( MethodGuard::Access )
255 : {
256 10 : m_aHandlerHelper.enterMethod();
257 10 : }
258 :
259 : //--------------------------------------------------------------------
260 10 : void FileHandler::leaveMethod( MethodGuard::Access )
261 : {
262 10 : m_aMutex.release();
263 10 : }
264 :
265 : //--------------------------------------------------------------------
266 0 : OUString SAL_CALL FileHandler::getEncoding() throw (RuntimeException)
267 : {
268 0 : MethodGuard aGuard( *this );
269 0 : OUString sEncoding;
270 0 : OSL_VERIFY( m_aHandlerHelper.getEncoding( sEncoding ) );
271 0 : return sEncoding;
272 : }
273 :
274 : //--------------------------------------------------------------------
275 0 : void SAL_CALL FileHandler::setEncoding( const OUString& _rEncoding ) throw (RuntimeException)
276 : {
277 0 : MethodGuard aGuard( *this );
278 0 : OSL_VERIFY( m_aHandlerHelper.setEncoding( _rEncoding ) );
279 0 : }
280 :
281 : //--------------------------------------------------------------------
282 0 : Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter() throw (RuntimeException)
283 : {
284 0 : MethodGuard aGuard( *this );
285 0 : return m_aHandlerHelper.getFormatter();
286 : }
287 :
288 : //--------------------------------------------------------------------
289 5 : void SAL_CALL FileHandler::setFormatter( const Reference< XLogFormatter >& _rxFormatter ) throw (RuntimeException)
290 : {
291 5 : MethodGuard aGuard( *this );
292 5 : m_aHandlerHelper.setFormatter( _rxFormatter );
293 5 : }
294 :
295 : //--------------------------------------------------------------------
296 0 : ::sal_Int32 SAL_CALL FileHandler::getLevel() throw (RuntimeException)
297 : {
298 0 : MethodGuard aGuard( *this );
299 0 : return m_aHandlerHelper.getLevel();
300 : }
301 :
302 : //--------------------------------------------------------------------
303 5 : void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel ) throw (RuntimeException)
304 : {
305 5 : MethodGuard aGuard( *this );
306 5 : m_aHandlerHelper.setLevel( _nLevel );
307 5 : }
308 :
309 : //--------------------------------------------------------------------
310 0 : void SAL_CALL FileHandler::flush( ) throw (RuntimeException)
311 : {
312 0 : MethodGuard aGuard( *this );
313 0 : if(!m_pFile.get())
314 : {
315 : OSL_PRECOND(false, "FileHandler::flush: no file!");
316 0 : return;
317 : }
318 : #if OSL_DEBUG_LEVEL > 0
319 : ::osl::FileBase::RC res =
320 : #endif
321 0 : m_pFile->sync();
322 0 : OSL_ENSURE(res == ::osl::FileBase::E_None, "FileHandler::flush: Could not sync logfile to filesystem.");
323 : }
324 :
325 : //--------------------------------------------------------------------
326 0 : ::sal_Bool SAL_CALL FileHandler::publish( const LogRecord& _rRecord ) throw (RuntimeException)
327 : {
328 0 : MethodGuard aGuard( *this );
329 :
330 0 : if ( !impl_prepareFile_nothrow() )
331 0 : return sal_False;
332 :
333 0 : OString sEntry;
334 0 : if ( !m_aHandlerHelper.formatForPublishing( _rRecord, sEntry ) )
335 0 : return sal_False;
336 :
337 0 : impl_writeString_nothrow( sEntry );
338 0 : return sal_True;
339 : }
340 :
341 : //--------------------------------------------------------------------
342 5 : void SAL_CALL FileHandler::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
343 : {
344 5 : ::osl::MutexGuard aGuard( m_aMutex );
345 :
346 5 : if ( m_aHandlerHelper.getIsInitialized() )
347 0 : throw AlreadyInitializedException();
348 :
349 5 : if ( _rArguments.getLength() != 1 )
350 0 : throw IllegalArgumentException( OUString(), *this, 1 );
351 :
352 10 : Sequence< NamedValue > aSettings;
353 5 : if ( _rArguments[0] >>= m_sFileURL )
354 : {
355 : // create( [in] string URL );
356 0 : impl_doStringsubstitution_nothrow( m_sFileURL );
357 : }
358 5 : else if ( _rArguments[0] >>= aSettings )
359 : {
360 : // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
361 5 : ::comphelper::NamedValueCollection aTypedSettings( aSettings );
362 5 : m_aHandlerHelper.initFromSettings( aTypedSettings );
363 :
364 5 : if ( aTypedSettings.get_ensureType( "FileURL", m_sFileURL ) )
365 5 : impl_doStringsubstitution_nothrow( m_sFileURL );
366 : }
367 : else
368 0 : throw IllegalArgumentException( OUString(), *this, 1 );
369 :
370 10 : m_aHandlerHelper.setIsInitialized();
371 5 : }
372 :
373 : //--------------------------------------------------------------------
374 0 : OUString SAL_CALL FileHandler::getImplementationName() throw(RuntimeException)
375 : {
376 0 : return getImplementationName_static();
377 : }
378 :
379 : //--------------------------------------------------------------------
380 0 : ::sal_Bool SAL_CALL FileHandler::supportsService( const OUString& _rServiceName ) throw(RuntimeException)
381 : {
382 0 : const Sequence< OUString > aServiceNames( getSupportedServiceNames() );
383 0 : for ( const OUString* pServiceNames = aServiceNames.getConstArray();
384 0 : pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
385 : ++pServiceNames
386 : )
387 0 : if ( _rServiceName == *pServiceNames )
388 0 : return sal_True;
389 0 : return sal_False;
390 : }
391 :
392 : //--------------------------------------------------------------------
393 0 : Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames() throw(RuntimeException)
394 : {
395 0 : return getSupportedServiceNames_static();
396 : }
397 :
398 : //--------------------------------------------------------------------
399 5 : OUString SAL_CALL FileHandler::getImplementationName_static()
400 : {
401 5 : return OUString( "com.sun.star.comp.extensions.FileHandler" );
402 : }
403 :
404 : //--------------------------------------------------------------------
405 5 : Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames_static()
406 : {
407 5 : Sequence< OUString > aServiceNames(1);
408 5 : aServiceNames[0] = OUString( "com.sun.star.logging.FileHandler" );
409 5 : return aServiceNames;
410 : }
411 :
412 : //--------------------------------------------------------------------
413 5 : Reference< XInterface > FileHandler::Create( const Reference< XComponentContext >& _rxContext )
414 : {
415 5 : return *( new FileHandler( _rxContext ) );
416 : }
417 :
418 : //--------------------------------------------------------------------
419 15 : void createRegistryInfo_FileHandler()
420 : {
421 15 : static OAutoRegistration< FileHandler > aAutoRegistration;
422 15 : }
423 :
424 : //........................................................................
425 : } // namespace logging
426 : //........................................................................
427 :
428 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|