25#include <com/sun/star/configuration/theDefaultProvider.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27#include <com/sun/star/container/XNameContainer.hpp>
28#include <com/sun/star/lang/XSingleServiceFactory.hpp>
29#include <com/sun/star/util/XChangesBatch.hpp>
30#include <com/sun/star/logging/LogLevel.hpp>
31#include <com/sun/star/lang/NullPointerException.hpp>
32#include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
33#include <com/sun/star/beans/NamedValue.hpp>
34#include <com/sun/star/logging/XLogHandler.hpp>
35#include <com/sun/star/logging/XLogFormatter.hpp>
38#include <osl/process.h>
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::logging::XLogger;
49 using ::com::sun::star::lang::XMultiServiceFactory;
50 using ::com::sun::star::uno::Sequence;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::container::XNameContainer;
53 using ::com::sun::star::uno::UNO_QUERY_THROW;
54 using ::com::sun::star::lang::XSingleServiceFactory;
55 using ::com::sun::star::uno::XInterface;
56 using ::com::sun::star::util::XChangesBatch;
57 using ::com::sun::star::lang::NullPointerException;
58 using ::com::sun::star::uno::Exception;
59 using ::com::sun::star::lang::ServiceNotRegisteredException;
60 using ::com::sun::star::beans::NamedValue;
61 using ::com::sun::star::logging::XLogHandler;
62 using ::com::sun::star::logging::XLogFormatter;
63 using ::com::sun::star::container::XNameAccess;
64 using ::com::sun::star::uno::XComponentContext;
66 namespace LogLevel = ::com::sun::star::logging::LogLevel;
71 typedef void (*SettingTranslation)(
const Reference< XLogger >&,
const OUString&,
Any& );
74 void lcl_substituteFileHandlerURLVariables_nothrow(
const Reference< XLogger >& _rxLogger, OUString& _inout_rFileURL )
78 std::u16string_view pVariablePattern;
79 OUString sVariableValue;
83 try { sLoggerName = _rxLogger->getName(); }
87 oslDateTime aDateTime;
88 OSL_VERIFY( osl_getSystemTime( &aTimeValue ) );
89 OSL_VERIFY( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) );
92 const size_t buffer_size =
sizeof( buffer );
94 snprintf( buffer, buffer_size,
"%04i-%02i-%02i",
95 static_cast<int>(aDateTime.Year),
96 static_cast<int>(aDateTime.Month),
97 static_cast<int>(aDateTime.Day) );
98 OUString sDate = OUString::createFromAscii( buffer );
100 snprintf( buffer, buffer_size,
"%02i-%02i-%02i.%03i",
101 static_cast<int>(aDateTime.Hours),
102 static_cast<int>(aDateTime.Minutes),
103 static_cast<int>(aDateTime.Seconds),
104 ::sal::static_int_cast< sal_Int16 >( aDateTime.NanoSeconds / 10000000 ) );
105 OUString sTime = OUString::createFromAscii( buffer );
107 OUString sDateTime = sDate +
"." + sTime;
109 oslProcessIdentifier aProcessId = 0;
111 info.Size =
sizeof (oslProcessInfo);
112 if ( osl_getProcessInfo (
nullptr, osl_Process_IDENTIFIER, &info ) == osl_Process_E_None)
113 aProcessId = info.Ident;
114 OUString aPID = OUString::number( aProcessId );
118 {std::u16string_view(u
"$(loggername)"), sLoggerName},
119 {std::u16string_view(u
"$(date)"), sDate},
120 {std::u16string_view(u
"$(time)"), sTime},
121 {std::u16string_view(u
"$(datetime)"), sDateTime},
122 {std::u16string_view(u
"$(pid)"), aPID}
125 for (Variable
const & aVariable : aVariables)
127 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( aVariable.pVariablePattern );
128 if (nVariableIndex >= 0)
130 _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, aVariable.pVariablePattern.size(), aVariable.sVariableValue );
136 void lcl_transformFileHandlerSettings_nothrow(
const Reference< XLogger >& _rxLogger,
const OUString& _rSettingName, Any& _inout_rSettingValue )
138 if ( _rSettingName !=
"FileURL" )
143 OSL_VERIFY( _inout_rSettingValue >>= sURL );
144 lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
145 _inout_rSettingValue <<= sURL;
150 const Reference<XComponentContext>& _rContext,
151 const Reference< XLogger >& _rxLogger,
152 const Reference< XNameAccess >& _rxLoggerSettings,
153 const char* _pServiceNameAsciiNodeName,
154 const char* _pServiceSettingsAsciiNodeName,
155 SettingTranslation _pSettingTranslation =
nullptr
161 Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
162 OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
164 Sequence< OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
165 size_t nServiceSettingCount( aSettingNames.getLength() );
166 Sequence< NamedValue > aSettings( nServiceSettingCount );
167 if ( nServiceSettingCount )
169 const OUString* pSettingNames = aSettingNames.getConstArray();
170 const OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
171 NamedValue* pSetting = aSettings.getArray();
174 pSettingNames != pSettingNamesEnd;
175 ++pSettingNames, ++pSetting
178 pSetting->Name = *pSettingNames;
179 pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
181 if ( _pSettingTranslation )
182 _pSettingTranslation( _rxLogger, pSetting->Name, pSetting->Value );
187 _rxLoggerSettings->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>=
sServiceName;
190 bool bSuccess =
false;
191 if ( aSettings.hasElements() )
193 Sequence< Any > aConstructionArgs{
Any(aSettings) };
194 xInstance = _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aConstructionArgs, _rContext);
195 bSuccess = xInstance.is();
199 xInstance = _rContext->getServiceManager()->createInstanceWithContext(sServiceName, _rContext);
200 bSuccess = xInstance.is();
204 throw ServiceNotRegisteredException( sServiceName );
216 if ( !_rxLogger.is() )
217 throw NullPointerException();
220 css::configuration::theDefaultProvider::get(_rContext));
224 "nodepath",
Any(OUString(
"/org.openoffice.Office.Logging/Settings")))) };
225 Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
226 "com.sun.star.configuration.ConfigurationUpdateAccess",
228 ), UNO_QUERY_THROW );
230 OUString sLoggerName( _rxLogger->getName() );
231 if ( !xAllSettings->hasByName( sLoggerName ) )
236 xAllSettings->insertByName( sLoggerName,
Any( xLoggerSettings ) );
237 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
238 xChanges->commitChanges();
242 Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
245 sal_Int32 nLogLevel( LogLevel::OFF );
246 OSL_VERIFY( xLoggerSettings->getByName(
"LogLevel") >>= nLogLevel );
247 _rxLogger->setLevel( nLogLevel );
250 Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings,
"DefaultHandler",
"HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
251 if ( !xUntyped.is() )
254 Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
255 _rxLogger->addLogHandler( xHandler );
259 xHandler->setLevel( nLogLevel );
262 xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings,
"DefaultFormatter",
"FormatterSettings" );
263 if ( !xUntyped.is() )
266 Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
267 xHandler->setFormatter( xFormatter );
constexpr OUStringLiteral sServiceName
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< PropertyValue > aArguments
void initializeLoggerFromConfiguration(const Reference< XComponentContext > &_rContext, const Reference< XLogger > &_rxLogger)