1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "log_services.hxx"
23 #include "methodguard.hxx"
24 #include "loghandler.hxx"
26 #include <com/sun/star/logging/XLogHandler.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
29 #include <com/sun/star/lang/XInitialization.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/util/PathSubstitution.hpp>
32 #include <com/sun/star/util/XStringSubstitution.hpp>
34 #include <tools/diagnose_ex.h>
36 #include <cppuhelper/compbase3.hxx>
37 #include <cppuhelper/basemutex.hxx>
38 #include <cppuhelper/supportsservice.hxx>
40 #include <osl/thread.h>
41 #include <osl/file.hxx>
43 #include <rtl/strbuf.hxx>
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 typedef ::cppu::WeakComponentImplHelper3
< XLogHandler
75 class FileHandler
:public ::cppu::BaseMutex
76 ,public FileHandler_Base
81 /// never attempted to open the file
90 Reference
<XComponentContext
> m_xContext
;
91 LogHandlerHelper m_aHandlerHelper
;
93 ::std::unique_ptr
< ::osl::File
> m_pFile
;
94 FileValidity m_eFileValidity
;
97 FileHandler( const Reference
< XComponentContext
>& _rxContext
);
98 virtual ~FileHandler();
101 virtual OUString SAL_CALL
getEncoding() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
102 virtual void SAL_CALL
setEncoding( const OUString
& _encoding
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
103 virtual Reference
< XLogFormatter
> SAL_CALL
getFormatter() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
104 virtual void SAL_CALL
setFormatter( const Reference
< XLogFormatter
>& _formatter
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
105 virtual ::sal_Int32 SAL_CALL
getLevel() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
106 virtual void SAL_CALL
setLevel( ::sal_Int32 _level
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
107 virtual void SAL_CALL
flush( ) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
108 virtual sal_Bool SAL_CALL
publish( const LogRecord
& Record
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
111 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
, std::exception
) SAL_OVERRIDE
;
114 virtual OUString SAL_CALL
getImplementationName() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
115 virtual sal_Bool SAL_CALL
supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
116 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
119 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
122 // XServiceInfo - static version
123 static OUString SAL_CALL
getImplementationName_static();
124 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_static();
125 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
128 typedef ComponentMethodGuard
< FileHandler
> MethodGuard
;
129 void enterMethod( MethodGuard::Access
);
130 void leaveMethod( MethodGuard::Access
);
133 /** prepares our output file for writing
135 bool impl_prepareFile_nothrow();
137 /// writes the given string to our file
138 void impl_writeString_nothrow( const OString
& _rEntry
);
140 /** does string substitution on a (usually externally provided) file url
142 void impl_doStringsubstitution_nothrow( OUString
& _inout_rURL
);
145 FileHandler::FileHandler( const Reference
< XComponentContext
>& _rxContext
)
146 :FileHandler_Base( m_aMutex
)
147 ,m_xContext( _rxContext
)
148 ,m_aHandlerHelper( _rxContext
, m_aMutex
, rBHelper
)
151 ,m_eFileValidity( eUnknown
)
156 FileHandler::~FileHandler()
158 if ( !rBHelper
.bDisposed
)
166 bool FileHandler::impl_prepareFile_nothrow()
168 if ( m_eFileValidity
== eUnknown
)
170 m_pFile
.reset( new ::osl::File( m_sFileURL
) );
171 // check whether the log file already exists
172 ::osl::DirectoryItem aFileItem
;
173 ::osl::DirectoryItem::get( m_sFileURL
, aFileItem
);
174 ::osl::FileStatus
aStatus( osl_FileStatus_Mask_Validate
);
175 if ( ::osl::FileBase::E_None
== aFileItem
.getFileStatus( aStatus
) )
176 ::osl::File::remove( m_sFileURL
);
178 ::osl::FileBase::RC res
= m_pFile
->open( osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
179 m_eFileValidity
= res
== ::osl::FileBase::E_None
182 #if OSL_DEBUG_LEVEL > 0
183 if ( m_eFileValidity
== eInvalid
)
185 OStringBuffer sMessage
;
186 sMessage
.append( "FileHandler::impl_prepareFile_nothrow: could not open the designated log file:" );
187 sMessage
.append( "\nURL: " );
188 sMessage
.append( OString( m_sFileURL
.getStr(), m_sFileURL
.getLength(), osl_getThreadTextEncoding() ) );
189 sMessage
.append( "\nerror code: " );
190 sMessage
.append( (sal_Int32
)res
);
191 OSL_FAIL( sMessage
.makeStringAndClear().getStr() );
194 if ( m_eFileValidity
== eValid
)
197 if ( m_aHandlerHelper
.getEncodedHead( sHead
) )
198 impl_writeString_nothrow( sHead
);
202 return m_eFileValidity
== eValid
;
206 void FileHandler::impl_writeString_nothrow( const OString
& _rEntry
)
208 OSL_PRECOND( m_pFile
.get(), "FileHandler::impl_writeString_nothrow: no file!" );
210 sal_uInt64
nBytesToWrite( _rEntry
.getLength() );
211 sal_uInt64
nBytesWritten( 0 );
212 #if OSL_DEBUG_LEVEL > 0
213 ::osl::FileBase::RC res
=
215 m_pFile
->write( _rEntry
.getStr(), nBytesToWrite
, nBytesWritten
);
216 OSL_ENSURE( ( res
== ::osl::FileBase::E_None
) && ( nBytesWritten
== nBytesToWrite
),
217 "FileHandler::impl_writeString_nothrow: could not write the log entry!" );
221 void FileHandler::impl_doStringsubstitution_nothrow( OUString
& _inout_rURL
)
225 Reference
< XStringSubstitution
> xStringSubst(PathSubstitution::create(m_xContext
));
226 _inout_rURL
= xStringSubst
->substituteVariables( _inout_rURL
, true );
228 catch( const Exception
& )
230 DBG_UNHANDLED_EXCEPTION();
235 void SAL_CALL
FileHandler::disposing()
237 if ( m_eFileValidity
== eValid
)
240 if ( m_aHandlerHelper
.getEncodedTail( sTail
) )
241 impl_writeString_nothrow( sTail
);
245 m_aHandlerHelper
.setFormatter( NULL
);
249 void FileHandler::enterMethod( MethodGuard::Access
)
251 m_aHandlerHelper
.enterMethod();
255 void FileHandler::leaveMethod( MethodGuard::Access
)
261 OUString SAL_CALL
FileHandler::getEncoding() throw (RuntimeException
, std::exception
)
263 MethodGuard
aGuard( *this );
265 OSL_VERIFY( m_aHandlerHelper
.getEncoding( sEncoding
) );
270 void SAL_CALL
FileHandler::setEncoding( const OUString
& _rEncoding
) throw (RuntimeException
, std::exception
)
272 MethodGuard
aGuard( *this );
273 OSL_VERIFY( m_aHandlerHelper
.setEncoding( _rEncoding
) );
277 Reference
< XLogFormatter
> SAL_CALL
FileHandler::getFormatter() throw (RuntimeException
, std::exception
)
279 MethodGuard
aGuard( *this );
280 return m_aHandlerHelper
.getFormatter();
284 void SAL_CALL
FileHandler::setFormatter( const Reference
< XLogFormatter
>& _rxFormatter
) throw (RuntimeException
, std::exception
)
286 MethodGuard
aGuard( *this );
287 m_aHandlerHelper
.setFormatter( _rxFormatter
);
291 ::sal_Int32 SAL_CALL
FileHandler::getLevel() throw (RuntimeException
, std::exception
)
293 MethodGuard
aGuard( *this );
294 return m_aHandlerHelper
.getLevel();
298 void SAL_CALL
FileHandler::setLevel( ::sal_Int32 _nLevel
) throw (RuntimeException
, std::exception
)
300 MethodGuard
aGuard( *this );
301 m_aHandlerHelper
.setLevel( _nLevel
);
305 void SAL_CALL
FileHandler::flush( ) throw (RuntimeException
, std::exception
)
307 MethodGuard
aGuard( *this );
310 OSL_PRECOND(false, "FileHandler::flush: no file!");
313 #if OSL_DEBUG_LEVEL > 0
314 ::osl::FileBase::RC res
=
317 OSL_ENSURE(res
== ::osl::FileBase::E_None
, "FileHandler::flush: Could not sync logfile to filesystem.");
321 sal_Bool SAL_CALL
FileHandler::publish( const LogRecord
& _rRecord
) throw (RuntimeException
, std::exception
)
323 MethodGuard
aGuard( *this );
325 if ( !impl_prepareFile_nothrow() )
329 if ( !m_aHandlerHelper
.formatForPublishing( _rRecord
, sEntry
) )
332 impl_writeString_nothrow( sEntry
);
337 void SAL_CALL
FileHandler::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
, std::exception
)
339 ::osl::MutexGuard
aGuard( m_aMutex
);
341 if ( m_aHandlerHelper
.getIsInitialized() )
342 throw AlreadyInitializedException();
344 if ( _rArguments
.getLength() != 1 )
345 throw IllegalArgumentException( OUString(), *this, 1 );
347 Sequence
< NamedValue
> aSettings
;
348 if ( _rArguments
[0] >>= m_sFileURL
)
350 // create( [in] string URL );
351 impl_doStringsubstitution_nothrow( m_sFileURL
);
353 else if ( _rArguments
[0] >>= aSettings
)
355 // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
356 ::comphelper::NamedValueCollection
aTypedSettings( aSettings
);
357 m_aHandlerHelper
.initFromSettings( aTypedSettings
);
359 if ( aTypedSettings
.get_ensureType( "FileURL", m_sFileURL
) )
360 impl_doStringsubstitution_nothrow( m_sFileURL
);
363 throw IllegalArgumentException( OUString(), *this, 1 );
365 m_aHandlerHelper
.setIsInitialized();
369 OUString SAL_CALL
FileHandler::getImplementationName() throw(RuntimeException
, std::exception
)
371 return getImplementationName_static();
374 sal_Bool SAL_CALL
FileHandler::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
)
376 return cppu::supportsService(this, _rServiceName
);
380 Sequence
< OUString
> SAL_CALL
FileHandler::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
382 return getSupportedServiceNames_static();
386 OUString SAL_CALL
FileHandler::getImplementationName_static()
388 return OUString( "com.sun.star.comp.extensions.FileHandler" );
392 Sequence
< OUString
> SAL_CALL
FileHandler::getSupportedServiceNames_static()
394 Sequence
< OUString
> aServiceNames(1);
395 aServiceNames
[0] = "com.sun.star.logging.FileHandler";
396 return aServiceNames
;
400 Reference
< XInterface
> FileHandler::Create( const Reference
< XComponentContext
>& _rxContext
)
402 return *( new FileHandler( _rxContext
) );
406 void createRegistryInfo_FileHandler()
408 static OAutoRegistration
< FileHandler
> aAutoRegistration
;
412 } // namespace logging
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */