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>
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
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;
106 virtual void SAL_CALL disposing()
override;
109 typedef ComponentMethodGuard< FileHandler > MethodGuard;
110 void enterMethod( MethodGuard::Access );
111 void leaveMethod( MethodGuard::Access );
116 bool impl_prepareFile_nothrow();
119 void impl_writeString_nothrow(
const OString& _rEntry );
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)
135 ::osl::MutexGuard aGuard(
m_aMutex );
137 if ( arguments.getLength() != 1 )
138 throw IllegalArgumentException( OUString(), *
this, 1 );
140 Sequence< NamedValue > aSettings;
144 impl_doStringsubstitution_nothrow(
m_sFileURL );
146 else if ( arguments[0] >>= aSettings )
152 if ( aTypedSettings.get_ensureType(
"FileURL",
m_sFileURL ) )
153 impl_doStringsubstitution_nothrow(
m_sFileURL );
156 throw IllegalArgumentException( OUString(), *
this, 1 );
161 FileHandler::~FileHandler()
163 if ( !rBHelper.bDisposed )
171 bool FileHandler::impl_prepareFile_nothrow()
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))
185 ::osl::FileBase::RC res =
m_pFile->open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
189 #if OSL_DEBUG_LEVEL > 0
192 SAL_WARN(
"extensions.logging",
"FileHandler::impl_prepareFile_nothrow: could not open the designated log file:"
194 <<
"\nerror code: " <<
static_cast<sal_Int32
>(res) );
201 impl_writeString_nothrow( sHead );
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& )
236 void SAL_CALL FileHandler::disposing()
242 impl_writeString_nothrow( sTail );
250 void FileHandler::enterMethod( MethodGuard::Access )
256 void FileHandler::leaveMethod( MethodGuard::Access )
262 OUString SAL_CALL FileHandler::getEncoding()
264 MethodGuard aGuard( *
this );
271 void SAL_CALL FileHandler::setEncoding(
const OUString& _rEncoding )
273 MethodGuard aGuard( *
this );
278 Reference< XLogFormatter > SAL_CALL FileHandler::getFormatter()
280 MethodGuard aGuard( *
this );
285 void SAL_CALL FileHandler::setFormatter(
const Reference< XLogFormatter >& _rxFormatter )
287 MethodGuard aGuard( *
this );
292 ::sal_Int32 SAL_CALL FileHandler::getLevel()
294 MethodGuard aGuard( *
this );
299 void SAL_CALL FileHandler::setLevel( ::sal_Int32 _nLevel )
301 MethodGuard aGuard( *
this );
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() )
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 )
344 Sequence< OUString > SAL_CALL FileHandler::getSupportedServiceNames()
346 return {
"com.sun.star.logging.FileHandler" };
351extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
353 css::uno::XComponentContext *context,
354 css::uno::Sequence<css::uno::Any>
const &arguments)
356 return cppu::acquire(
new logging::FileHandler(context, arguments));
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XComponentContext > m_xContext
LogHandlerHelper m_aHandlerHelper
std::unique_ptr< ::osl::File > m_pFile
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
#define SAL_WARN(area, stream)
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