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 "loggerconfig.hxx"
23 #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
27 #include <com/sun/star/util/XChangesBatch.hpp>
28 #include <com/sun/star/logging/LogLevel.hpp>
29 #include <com/sun/star/lang/NullPointerException.hpp>
30 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
31 #include <com/sun/star/beans/NamedValue.hpp>
32 #include <com/sun/star/logging/XLogHandler.hpp>
33 #include <com/sun/star/logging/XLogFormatter.hpp>
35 #include <tools/diagnose_ex.h>
37 #include <comphelper/componentcontext.hxx>
39 #include <cppuhelper/component_context.hxx>
42 #include <sal/macros.h>
44 //........................................................................
47 //........................................................................
49 using ::com::sun::star::uno::Reference
;
50 using ::com::sun::star::logging::XLogger
;
51 using ::com::sun::star::lang::XMultiServiceFactory
;
52 using ::com::sun::star::uno::Sequence
;
53 using ::com::sun::star::uno::Any
;
54 using ::com::sun::star::container::XNameContainer
;
55 using ::com::sun::star::uno::UNO_QUERY_THROW
;
56 using ::com::sun::star::lang::XSingleServiceFactory
;
57 using ::com::sun::star::uno::XInterface
;
58 using ::com::sun::star::util::XChangesBatch
;
59 using ::com::sun::star::uno::makeAny
;
60 using ::com::sun::star::lang::NullPointerException
;
61 using ::com::sun::star::uno::Exception
;
62 using ::com::sun::star::lang::ServiceNotRegisteredException
;
63 using ::com::sun::star::beans::NamedValue
;
64 using ::com::sun::star::logging::XLogHandler
;
65 using ::com::sun::star::logging::XLogFormatter
;
66 using ::com::sun::star::container::XNameAccess
;
67 using ::com::sun::star::uno::XComponentContext
;
69 namespace LogLevel
= ::com::sun::star::logging::LogLevel
;
73 //----------------------------------------------------------------
74 typedef void (*SettingTranslation
)( const Reference
< XLogger
>&, const OUString
&, Any
& );
76 //----------------------------------------------------------------
77 void lcl_substituteFileHandlerURLVariables_nothrow( const Reference
< XLogger
>& _rxLogger
, OUString
& _inout_rFileURL
)
81 const sal_Char
* pVariablePattern
;
82 const sal_Int32 nPatternLength
;
83 rtl_TextEncoding eEncoding
;
84 const OUString sVariableValue
;
86 Variable( const sal_Char
* _pVariablePattern
, const sal_Int32 _nPatternLength
, rtl_TextEncoding _eEncoding
,
87 const OUString
& _rVariableValue
)
88 :pVariablePattern( _pVariablePattern
)
89 ,nPatternLength( _nPatternLength
)
90 ,eEncoding( _eEncoding
)
91 ,sVariableValue( _rVariableValue
)
97 try { sLoggerName
= _rxLogger
->getName(); }
98 catch( const Exception
& ) { DBG_UNHANDLED_EXCEPTION(); }
100 Variable aVariables
[] =
102 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName
)
105 for ( size_t i
= 0; i
< SAL_N_ELEMENTS( aVariables
); ++i
)
107 OUString
sPattern( aVariables
[i
].pVariablePattern
, aVariables
[i
].nPatternLength
, aVariables
[i
].eEncoding
);
108 sal_Int32 nVariableIndex
= _inout_rFileURL
.indexOf( sPattern
);
109 if ( ( nVariableIndex
== 0 )
110 || ( ( nVariableIndex
> 0 )
111 && ( sPattern
[ nVariableIndex
- 1 ] != '$' )
115 // found an (unescaped) variable
116 _inout_rFileURL
= _inout_rFileURL
.replaceAt( nVariableIndex
, sPattern
.getLength(), aVariables
[i
].sVariableValue
);
121 //----------------------------------------------------------------
122 void lcl_transformFileHandlerSettings_nothrow( const Reference
< XLogger
>& _rxLogger
, const OUString
& _rSettingName
, Any
& _inout_rSettingValue
)
124 if ( _rSettingName
!= "FileURL" )
125 // not interested in this setting
129 OSL_VERIFY( _inout_rSettingValue
>>= sURL
);
130 lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger
, sURL
);
131 _inout_rSettingValue
<<= sURL
;
134 //----------------------------------------------------------------
135 Reference
< XInterface
> lcl_createInstanceFromSetting_throw(
136 const ::comphelper::ComponentContext
& _rContext
,
137 const Reference
< XLogger
>& _rxLogger
,
138 const Reference
< XNameAccess
>& _rxLoggerSettings
,
139 const sal_Char
* _pServiceNameAsciiNodeName
,
140 const sal_Char
* _pServiceSettingsAsciiNodeName
,
141 SettingTranslation _pSettingTranslation
= NULL
144 Reference
< XInterface
> xInstance
;
146 // read the settings for the to-be-created service
147 Reference
< XNameAccess
> xServiceSettingsNode( _rxLoggerSettings
->getByName(
148 OUString::createFromAscii( _pServiceSettingsAsciiNodeName
) ), UNO_QUERY_THROW
);
150 Sequence
< OUString
> aSettingNames( xServiceSettingsNode
->getElementNames() );
151 size_t nServiceSettingCount( aSettingNames
.getLength() );
152 Sequence
< NamedValue
> aSettings( nServiceSettingCount
);
153 if ( nServiceSettingCount
)
155 const OUString
* pSettingNames
= aSettingNames
.getConstArray();
156 const OUString
* pSettingNamesEnd
= aSettingNames
.getConstArray() + aSettingNames
.getLength();
157 NamedValue
* pSetting
= aSettings
.getArray();
160 pSettingNames
!= pSettingNamesEnd
;
161 ++pSettingNames
, ++pSetting
164 pSetting
->Name
= *pSettingNames
;
165 pSetting
->Value
= xServiceSettingsNode
->getByName( *pSettingNames
);
167 if ( _pSettingTranslation
)
168 (_pSettingTranslation
)( _rxLogger
, pSetting
->Name
, pSetting
->Value
);
172 OUString sServiceName
;
173 _rxLoggerSettings
->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName
) ) >>= sServiceName
;
174 if ( !sServiceName
.isEmpty() )
176 bool bSuccess
= false;
177 if ( aSettings
.getLength() )
179 Sequence
< Any
> aConstructionArgs(1);
180 aConstructionArgs
[0] <<= aSettings
;
181 bSuccess
= _rContext
.createComponentWithArguments( sServiceName
, aConstructionArgs
, xInstance
);
185 bSuccess
= _rContext
.createComponent( sServiceName
, xInstance
);
189 throw ServiceNotRegisteredException( sServiceName
, NULL
);
196 //--------------------------------------------------------------------
197 void initializeLoggerFromConfiguration( const ::comphelper::ComponentContext
& _rContext
, const Reference
< XLogger
>& _rxLogger
)
201 if ( !_rxLogger
.is() )
202 throw NullPointerException();
204 Reference
< XMultiServiceFactory
> xConfigProvider(
205 com::sun::star::configuration::theDefaultProvider::get(
206 _rContext
.getUNOContext()));
208 // write access to the "Settings" node (which includes settings for all loggers)
209 Sequence
< Any
> aArguments(1);
210 aArguments
[0] <<= NamedValue(
211 OUString( "nodepath" ),
212 makeAny( OUString( "/org.openoffice.Office.Logging/Settings" ) )
214 Reference
< XNameContainer
> xAllSettings( xConfigProvider
->createInstanceWithArguments(
215 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
217 ), UNO_QUERY_THROW
);
219 OUString
sLoggerName( _rxLogger
->getName() );
220 if ( !xAllSettings
->hasByName( sLoggerName
) )
222 // no node yet for this logger. Create default settings.
223 Reference
< XSingleServiceFactory
> xNodeFactory( xAllSettings
, UNO_QUERY_THROW
);
224 Reference
< XInterface
> xLoggerSettings( xNodeFactory
->createInstance(), UNO_QUERY_THROW
);
225 xAllSettings
->insertByName( sLoggerName
, makeAny( xLoggerSettings
) );
226 Reference
< XChangesBatch
> xChanges( xAllSettings
, UNO_QUERY_THROW
);
227 xChanges
->commitChanges();
230 // actually read and forward the settings
231 Reference
< XNameAccess
> xLoggerSettings( xAllSettings
->getByName( sLoggerName
), UNO_QUERY_THROW
);
234 sal_Int32
nLogLevel( LogLevel::OFF
);
235 OSL_VERIFY( xLoggerSettings
->getByName( OUString( "LogLevel" ) ) >>= nLogLevel
);
236 _rxLogger
->setLevel( nLogLevel
);
238 // the default handler, if any
239 Reference
< XInterface
> xUntyped( lcl_createInstanceFromSetting_throw( _rContext
, _rxLogger
, xLoggerSettings
, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow
) );
240 if ( !xUntyped
.is() )
241 // no handler -> we're done
243 Reference
< XLogHandler
> xHandler( xUntyped
, UNO_QUERY_THROW
);
244 _rxLogger
->addLogHandler( xHandler
);
246 // The newly created handler might have an own (default) level. Ensure that it uses
247 // the same level as the logger.
248 xHandler
->setLevel( nLogLevel
);
250 // the default formatter for the handler
251 xUntyped
= lcl_createInstanceFromSetting_throw( _rContext
, _rxLogger
, xLoggerSettings
, "DefaultFormatter", "FormatterSettings" );
252 if ( !xUntyped
.is() )
253 // no formatter -> we're done
255 Reference
< XLogFormatter
> xFormatter( xUntyped
, UNO_QUERY_THROW
);
256 xHandler
->setFormatter( xFormatter
);
258 // TODO: we could first create the formatter, then the handler. This would allow
259 // passing the formatter as value in the component context, so the handler would
260 // not create an own default formatter
262 catch( const Exception
& )
264 DBG_UNHANDLED_EXCEPTION();
268 //........................................................................
269 } // namespace logging
270 //........................................................................
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */