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 .
20 #include <sal/config.h>
21 #include <sal/log.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/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/util/PathSubstitution.hpp>
30 #include <com/sun/star/util/XStringSubstitution.hpp>
32 #include <comphelper/diagnose_ex.hxx>
34 #include <cppuhelper/compbase.hxx>
35 #include <cppuhelper/basemutex.hxx>
36 #include <cppuhelper/supportsservice.hxx>
38 #include <osl/file.hxx>
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
;
58 typedef ::cppu::WeakComponentImplHelper
< XLogHandler
64 class FileHandler
:public ::cppu::BaseMutex
65 ,public FileHandler_Base
70 /// never attempted to open the file
78 Reference
<XComponentContext
> m_xContext
;
79 LogHandlerHelper m_aHandlerHelper
;
81 std::unique_ptr
< ::osl::File
> m_pFile
;
82 FileValidity m_eFileValidity
;
85 FileHandler(const css::uno::Reference
<XComponentContext
> &context
,
86 const css::uno::Sequence
<css::uno::Any
> &arguments
);
87 virtual ~FileHandler() override
;
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
;
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
;
106 virtual void SAL_CALL
disposing() override
;
109 typedef ComponentMethodGuard
< FileHandler
> MethodGuard
;
110 void enterMethod( MethodGuard::Access
);
111 void leaveMethod( MethodGuard::Access
);
114 /** prepares our output file for writing
116 bool impl_prepareFile_nothrow();
118 /// writes the given string to our file
119 void impl_writeString_nothrow( const OString
& _rEntry
);
121 /** does string substitution on a (usually externally provided) file url
123 void impl_doStringsubstitution_nothrow( OUString
& _inout_rURL
);
128 FileHandler::FileHandler(const css::uno::Reference
<XComponentContext
> &context
,
129 const css::uno::Sequence
<css::uno::Any
> &arguments
)
130 :FileHandler_Base( m_aMutex
)
131 ,m_xContext( context
)
132 ,m_aHandlerHelper( context
, m_aMutex
, rBHelper
)
133 ,m_eFileValidity( eUnknown
)
135 ::osl::MutexGuard
aGuard( m_aMutex
);
137 if ( arguments
.getLength() != 1 )
138 throw IllegalArgumentException( OUString(), *this, 1 );
140 Sequence
< NamedValue
> aSettings
;
141 if ( arguments
[0] >>= m_sFileURL
)
143 // create( [in] string URL );
144 impl_doStringsubstitution_nothrow( m_sFileURL
);
146 else if ( arguments
[0] >>= aSettings
)
148 // createWithSettings( [in] sequence< css::beans::NamedValue > Settings )
149 ::comphelper::NamedValueCollection
aTypedSettings( aSettings
);
150 m_aHandlerHelper
.initFromSettings( aTypedSettings
);
152 if ( aTypedSettings
.get_ensureType( "FileURL", m_sFileURL
) )
153 impl_doStringsubstitution_nothrow( m_sFileURL
);
156 throw IllegalArgumentException( OUString(), *this, 1 );
158 m_aHandlerHelper
.setIsInitialized();
161 FileHandler::~FileHandler()
163 if ( !rBHelper
.bDisposed
)
171 bool FileHandler::impl_prepareFile_nothrow()
173 if ( m_eFileValidity
== eUnknown
)
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
))
180 ::osl::FileStatus
aStatus(osl_FileStatus_Mask_Validate
);
181 if (::osl::FileBase::E_None
== aFileItem
.getFileStatus(aStatus
))
182 ::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
189 #if OSL_DEBUG_LEVEL > 0
190 if ( m_eFileValidity
== eInvalid
)
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
) );
197 if ( m_eFileValidity
== eValid
)
200 if ( m_aHandlerHelper
.getEncodedHead( sHead
) )
201 impl_writeString_nothrow( sHead
);
205 return m_eFileValidity
== eValid
;
209 void FileHandler::impl_writeString_nothrow( const OString
& _rEntry
)
211 OSL_PRECOND(m_pFile
, "FileHandler::impl_writeString_nothrow: no file!");
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!" );
222 void FileHandler::impl_doStringsubstitution_nothrow( OUString
& _inout_rURL
)
226 Reference
< XStringSubstitution
> xStringSubst(PathSubstitution::create(m_xContext
));
227 _inout_rURL
= xStringSubst
->substituteVariables( _inout_rURL
, true );
229 catch( const Exception
& )
231 DBG_UNHANDLED_EXCEPTION("extensions.logging");
236 void SAL_CALL
FileHandler::disposing()
238 if ( m_eFileValidity
== eValid
)
241 if ( m_aHandlerHelper
.getEncodedTail( sTail
) )
242 impl_writeString_nothrow( sTail
);
246 m_aHandlerHelper
.setFormatter( nullptr );
250 void FileHandler::enterMethod( MethodGuard::Access
)
252 m_aHandlerHelper
.enterMethod();
256 void FileHandler::leaveMethod( MethodGuard::Access
)
262 OUString SAL_CALL
FileHandler::getEncoding()
264 MethodGuard
aGuard( *this );
266 OSL_VERIFY( m_aHandlerHelper
.getEncoding( sEncoding
) );
271 void SAL_CALL
FileHandler::setEncoding( const OUString
& _rEncoding
)
273 MethodGuard
aGuard( *this );
274 OSL_VERIFY( m_aHandlerHelper
.setEncoding( _rEncoding
) );
278 Reference
< XLogFormatter
> SAL_CALL
FileHandler::getFormatter()
280 MethodGuard
aGuard( *this );
281 return m_aHandlerHelper
.getFormatter();
285 void SAL_CALL
FileHandler::setFormatter( const Reference
< XLogFormatter
>& _rxFormatter
)
287 MethodGuard
aGuard( *this );
288 m_aHandlerHelper
.setFormatter( _rxFormatter
);
292 ::sal_Int32 SAL_CALL
FileHandler::getLevel()
294 MethodGuard
aGuard( *this );
295 return m_aHandlerHelper
.getLevel();
299 void SAL_CALL
FileHandler::setLevel( ::sal_Int32 _nLevel
)
301 MethodGuard
aGuard( *this );
302 m_aHandlerHelper
.setLevel( _nLevel
);
306 void SAL_CALL
FileHandler::flush( )
308 MethodGuard
aGuard( *this );
311 OSL_PRECOND(false, "FileHandler::flush: no file!");
314 ::osl::FileBase::RC res
= m_pFile
->sync();
315 OSL_ENSURE(res
== ::osl::FileBase::E_None
, "FileHandler::flush: Could not sync logfile to filesystem.");
319 sal_Bool SAL_CALL
FileHandler::publish( const LogRecord
& _rRecord
)
321 MethodGuard
aGuard( *this );
323 if ( !impl_prepareFile_nothrow() )
327 if ( !m_aHandlerHelper
.formatForPublishing( _rRecord
, sEntry
) )
330 impl_writeString_nothrow( sEntry
);
334 OUString SAL_CALL
FileHandler::getImplementationName()
336 return "com.sun.star.comp.extensions.FileHandler";
339 sal_Bool SAL_CALL
FileHandler::supportsService( const OUString
& _rServiceName
)
341 return cppu::supportsService(this, _rServiceName
);
344 Sequence
< OUString
> SAL_CALL
FileHandler::getSupportedServiceNames()
346 return { "com.sun.star.logging.FileHandler" };
349 } // namespace logging
351 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
352 com_sun_star_comp_extensions_FileHandler(
353 css::uno::XComponentContext
*context
,
354 css::uno::Sequence
<css::uno::Any
> const &arguments
)
356 return cppu::acquire(new logging::FileHandler(context
, arguments
));
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */