bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / logging / loggerconfig.cxx
blobb951945fee1807bebc20138643b392ff1ac4cab7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "loggerconfig.hxx"
22 #include <stdio.h>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 #include <com/sun/star/util/XChangesBatch.hpp>
29 #include <com/sun/star/logging/LogLevel.hpp>
30 #include <com/sun/star/lang/NullPointerException.hpp>
31 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/logging/XLogHandler.hpp>
34 #include <com/sun/star/logging/XLogFormatter.hpp>
36 #include <tools/diagnose_ex.h>
37 #include <osl/process.h>
38 #include <rtl/ustrbuf.hxx>
40 #include <cppuhelper/component_context.hxx>
42 #include <vector>
43 #include <sal/macros.h>
46 namespace logging
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::logging::XLogger;
52 using ::com::sun::star::lang::XMultiServiceFactory;
53 using ::com::sun::star::uno::Sequence;
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::container::XNameContainer;
56 using ::com::sun::star::uno::UNO_QUERY_THROW;
57 using ::com::sun::star::lang::XSingleServiceFactory;
58 using ::com::sun::star::uno::XInterface;
59 using ::com::sun::star::util::XChangesBatch;
60 using ::com::sun::star::uno::makeAny;
61 using ::com::sun::star::lang::NullPointerException;
62 using ::com::sun::star::uno::Exception;
63 using ::com::sun::star::lang::ServiceNotRegisteredException;
64 using ::com::sun::star::beans::NamedValue;
65 using ::com::sun::star::logging::XLogHandler;
66 using ::com::sun::star::logging::XLogFormatter;
67 using ::com::sun::star::container::XNameAccess;
68 using ::com::sun::star::uno::XComponentContext;
70 namespace LogLevel = ::com::sun::star::logging::LogLevel;
72 namespace
75 typedef void (*SettingTranslation)( const Reference< XLogger >&, const OUString&, Any& );
78 void lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, OUString& _inout_rFileURL )
80 struct Variable
82 const sal_Char* pVariablePattern;
83 const sal_Int32 nPatternLength;
84 rtl_TextEncoding eEncoding;
85 const OUString sVariableValue;
87 Variable( const sal_Char* _pVariablePattern, const sal_Int32 _nPatternLength, rtl_TextEncoding _eEncoding,
88 const OUString& _rVariableValue )
89 :pVariablePattern( _pVariablePattern )
90 ,nPatternLength( _nPatternLength )
91 ,eEncoding( _eEncoding )
92 ,sVariableValue( _rVariableValue )
97 OUString sLoggerName;
98 try { sLoggerName = _rxLogger->getName(); }
99 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
101 TimeValue aTimeValue;
102 oslDateTime aDateTime;
103 OSL_VERIFY( osl_getSystemTime( &aTimeValue ) );
104 OSL_VERIFY( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) );
106 char buffer[ 30 ];
107 const size_t buffer_size = sizeof( buffer );
109 snprintf( buffer, buffer_size, "%04i-%02i-%02i",
110 (int)aDateTime.Year,
111 (int)aDateTime.Month,
112 (int)aDateTime.Day );
113 rtl::OUString sDate = rtl::OUString::createFromAscii( buffer );
115 snprintf( buffer, buffer_size, "%02i-%02i-%02i.%03i",
116 (int)aDateTime.Hours,
117 (int)aDateTime.Minutes,
118 (int)aDateTime.Seconds,
119 ::sal::static_int_cast< sal_Int16 >( aDateTime.NanoSeconds / 10000000 ) );
120 rtl::OUString sTime = rtl::OUString::createFromAscii( buffer );
122 rtl::OUStringBuffer aBuff;
123 aBuff.append( sDate );
124 aBuff.append( '.' );
125 aBuff.append( sTime );
126 rtl::OUString sDateTime = aBuff.makeStringAndClear();
128 oslProcessIdentifier aProcessId = 0;
129 oslProcessInfo info;
130 info.Size = sizeof (oslProcessInfo);
131 if ( osl_getProcessInfo ( 0, osl_Process_IDENTIFIER, &info ) == osl_Process_E_None)
132 aProcessId = info.Ident;
133 rtl::OUString aPID = OUString::number( aProcessId );
135 Variable aVariables[] =
137 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName ),
138 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(date)" ), sDate ),
139 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(time)" ), sTime ),
140 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(datetime)" ), sDateTime ),
141 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(pid)" ), aPID )
144 for ( size_t i = 0; i < SAL_N_ELEMENTS( aVariables ); ++i )
146 OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
147 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
148 if ( ( nVariableIndex == 0 )
149 || ( ( nVariableIndex > 0 )
150 && ( sPattern[ nVariableIndex - 1 ] != '$' )
154 // found an (unescaped) variable
155 _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
161 void lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const OUString& _rSettingName, Any& _inout_rSettingValue )
163 if ( _rSettingName != "FileURL" )
164 // not interested in this setting
165 return;
167 OUString sURL;
168 OSL_VERIFY( _inout_rSettingValue >>= sURL );
169 lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
170 _inout_rSettingValue <<= sURL;
174 Reference< XInterface > lcl_createInstanceFromSetting_throw(
175 const Reference<XComponentContext>& _rContext,
176 const Reference< XLogger >& _rxLogger,
177 const Reference< XNameAccess >& _rxLoggerSettings,
178 const sal_Char* _pServiceNameAsciiNodeName,
179 const sal_Char* _pServiceSettingsAsciiNodeName,
180 SettingTranslation _pSettingTranslation = NULL
183 Reference< XInterface > xInstance;
185 // read the settings for the to-be-created service
186 Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
187 OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
189 Sequence< OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
190 size_t nServiceSettingCount( aSettingNames.getLength() );
191 Sequence< NamedValue > aSettings( nServiceSettingCount );
192 if ( nServiceSettingCount )
194 const OUString* pSettingNames = aSettingNames.getConstArray();
195 const OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
196 NamedValue* pSetting = aSettings.getArray();
198 for ( ;
199 pSettingNames != pSettingNamesEnd;
200 ++pSettingNames, ++pSetting
203 pSetting->Name = *pSettingNames;
204 pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
206 if ( _pSettingTranslation )
207 (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
211 OUString sServiceName;
212 _rxLoggerSettings->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
213 if ( !sServiceName.isEmpty() )
215 bool bSuccess = false;
216 if ( aSettings.getLength() )
218 Sequence< Any > aConstructionArgs(1);
219 aConstructionArgs[0] <<= aSettings;
220 xInstance = _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aConstructionArgs, _rContext);
221 bSuccess = xInstance.is();
223 else
225 xInstance = _rContext->getServiceManager()->createInstanceWithContext(sServiceName, _rContext);
226 bSuccess = xInstance.is();
229 if ( !bSuccess )
230 throw ServiceNotRegisteredException( sServiceName );
233 return xInstance;
238 void initializeLoggerFromConfiguration( const Reference<XComponentContext>& _rContext, const Reference< XLogger >& _rxLogger )
242 if ( !_rxLogger.is() )
243 throw NullPointerException();
245 Reference< XMultiServiceFactory > xConfigProvider(
246 com::sun::star::configuration::theDefaultProvider::get(_rContext));
248 // write access to the "Settings" node (which includes settings for all loggers)
249 Sequence< Any > aArguments(1);
250 aArguments[0] <<= NamedValue(
251 OUString( "nodepath" ),
252 makeAny( OUString( "/org.openoffice.Office.Logging/Settings" ) )
254 Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
255 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
256 aArguments
257 ), UNO_QUERY_THROW );
259 OUString sLoggerName( _rxLogger->getName() );
260 if ( !xAllSettings->hasByName( sLoggerName ) )
262 // no node yet for this logger. Create default settings.
263 Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
264 Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
265 xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
266 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
267 xChanges->commitChanges();
270 // actually read and forward the settings
271 Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
273 // the log level
274 sal_Int32 nLogLevel( LogLevel::OFF );
275 OSL_VERIFY( xLoggerSettings->getByName("LogLevel") >>= nLogLevel );
276 _rxLogger->setLevel( nLogLevel );
278 // the default handler, if any
279 Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
280 if ( !xUntyped.is() )
281 // no handler -> we're done
282 return;
283 Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
284 _rxLogger->addLogHandler( xHandler );
286 // The newly created handler might have an own (default) level. Ensure that it uses
287 // the same level as the logger.
288 xHandler->setLevel( nLogLevel );
290 // the default formatter for the handler
291 xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
292 if ( !xUntyped.is() )
293 // no formatter -> we're done
294 return;
295 Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
296 xHandler->setFormatter( xFormatter );
298 // TODO: we could first create the formatter, then the handler. This would allow
299 // passing the formatter as value in the component context, so the handler would
300 // not create an own default formatter
302 catch( const Exception& )
304 DBG_UNHANDLED_EXCEPTION();
309 } // namespace logging
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */