1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "loggerconfig.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /** === begin UNO includes === **/
34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/logging/LogLevel.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/logging/XLogHandler.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/logging/XLogFormatter.hpp>
44*cdf0e10cSrcweir /** === end UNO includes === **/
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <cppuhelper/component_context.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <vector>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //........................................................................
55*cdf0e10cSrcweir namespace logging
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir //........................................................................
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	/** === begin UNO using === **/
60*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
61*cdf0e10cSrcweir     using ::com::sun::star::logging::XLogger;
62*cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
63*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
64*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
65*cdf0e10cSrcweir     using ::com::sun::star::container::XNameContainer;
66*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
67*cdf0e10cSrcweir     using ::com::sun::star::lang::XSingleServiceFactory;
68*cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
69*cdf0e10cSrcweir     using ::com::sun::star::util::XChangesBatch;
70*cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
71*cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
72*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
73*cdf0e10cSrcweir     using ::com::sun::star::lang::ServiceNotRegisteredException;
74*cdf0e10cSrcweir     using ::com::sun::star::beans::NamedValue;
75*cdf0e10cSrcweir     using ::com::sun::star::logging::XLogHandler;
76*cdf0e10cSrcweir     using ::com::sun::star::logging::XLogFormatter;
77*cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
78*cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
79*cdf0e10cSrcweir 	/** === end UNO using === **/
80*cdf0e10cSrcweir     namespace LogLevel = ::com::sun::star::logging::LogLevel;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     namespace
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir 	    //----------------------------------------------------------------
85*cdf0e10cSrcweir         typedef void (*SettingTranslation)( const Reference< XLogger >&, const ::rtl::OUString&, Any& );
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         //----------------------------------------------------------------
88*cdf0e10cSrcweir         void    lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, ::rtl::OUString& _inout_rFileURL )
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             struct Variable
91*cdf0e10cSrcweir             {
92*cdf0e10cSrcweir                 const sal_Char*         pVariablePattern;
93*cdf0e10cSrcweir                 const sal_Int32         nPatternLength;
94*cdf0e10cSrcweir                 rtl_TextEncoding        eEncoding;
95*cdf0e10cSrcweir                 const ::rtl::OUString   sVariableValue;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir                 Variable( const sal_Char* _pVariablePattern,  const sal_Int32 _nPatternLength, rtl_TextEncoding _eEncoding,
98*cdf0e10cSrcweir                         const ::rtl::OUString& _rVariableValue )
99*cdf0e10cSrcweir                     :pVariablePattern( _pVariablePattern )
100*cdf0e10cSrcweir                     ,nPatternLength( _nPatternLength )
101*cdf0e10cSrcweir                     ,eEncoding( _eEncoding )
102*cdf0e10cSrcweir                     ,sVariableValue( _rVariableValue )
103*cdf0e10cSrcweir                 {
104*cdf0e10cSrcweir                 }
105*cdf0e10cSrcweir             };
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir             ::rtl::OUString sLoggerName;
108*cdf0e10cSrcweir             try { sLoggerName = _rxLogger->getName(); }
109*cdf0e10cSrcweir             catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir             Variable aVariables[] =
112*cdf0e10cSrcweir             {
113*cdf0e10cSrcweir                 Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName )
114*cdf0e10cSrcweir             };
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             for ( size_t i = 0; i < sizeof( aVariables ) / sizeof( aVariables[0] ); ++i )
117*cdf0e10cSrcweir             {
118*cdf0e10cSrcweir                 ::rtl::OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
119*cdf0e10cSrcweir                 sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
120*cdf0e10cSrcweir                 if  (   ( nVariableIndex == 0 )
121*cdf0e10cSrcweir                     ||  (   ( nVariableIndex > 0 )
122*cdf0e10cSrcweir                         &&  ( sPattern[ nVariableIndex - 1 ] != '$' )
123*cdf0e10cSrcweir                         )
124*cdf0e10cSrcweir                     )
125*cdf0e10cSrcweir                 {
126*cdf0e10cSrcweir                     // found an (unescaped) variable
127*cdf0e10cSrcweir                     _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
128*cdf0e10cSrcweir                 }
129*cdf0e10cSrcweir             }
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         //----------------------------------------------------------------
133*cdf0e10cSrcweir         void    lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const ::rtl::OUString& _rSettingName, Any& _inout_rSettingValue )
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             if ( !_rSettingName.equalsAscii( "FileURL" ) )
136*cdf0e10cSrcweir                 // not interested in this setting
137*cdf0e10cSrcweir                 return;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir             ::rtl::OUString sURL;
140*cdf0e10cSrcweir             OSL_VERIFY( _inout_rSettingValue >>= sURL );
141*cdf0e10cSrcweir             lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
142*cdf0e10cSrcweir             _inout_rSettingValue <<= sURL;
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	    //----------------------------------------------------------------
146*cdf0e10cSrcweir         Reference< XInterface > lcl_createInstanceFromSetting_throw(
147*cdf0e10cSrcweir                 const ::comphelper::ComponentContext& _rContext,
148*cdf0e10cSrcweir                 const Reference< XLogger >& _rxLogger,
149*cdf0e10cSrcweir                 const Reference< XNameAccess >& _rxLoggerSettings,
150*cdf0e10cSrcweir                 const sal_Char* _pServiceNameAsciiNodeName,
151*cdf0e10cSrcweir                 const sal_Char* _pServiceSettingsAsciiNodeName,
152*cdf0e10cSrcweir                 SettingTranslation _pSettingTranslation = NULL
153*cdf0e10cSrcweir             )
154*cdf0e10cSrcweir         {
155*cdf0e10cSrcweir             Reference< XInterface > xInstance;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             // read the settings for the to-be-created service
158*cdf0e10cSrcweir             Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
159*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
162*cdf0e10cSrcweir             size_t nServiceSettingCount( aSettingNames.getLength() );
163*cdf0e10cSrcweir             Sequence< NamedValue > aSettings( nServiceSettingCount );
164*cdf0e10cSrcweir             if ( nServiceSettingCount )
165*cdf0e10cSrcweir             {
166*cdf0e10cSrcweir                 const ::rtl::OUString* pSettingNames = aSettingNames.getConstArray();
167*cdf0e10cSrcweir                 const ::rtl::OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
168*cdf0e10cSrcweir                 NamedValue* pSetting = aSettings.getArray();
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir                 for (   ;
171*cdf0e10cSrcweir                         pSettingNames != pSettingNamesEnd;
172*cdf0e10cSrcweir                         ++pSettingNames, ++pSetting
173*cdf0e10cSrcweir                     )
174*cdf0e10cSrcweir                 {
175*cdf0e10cSrcweir                     pSetting->Name = *pSettingNames;
176*cdf0e10cSrcweir                     pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir                     if ( _pSettingTranslation )
179*cdf0e10cSrcweir                         (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
180*cdf0e10cSrcweir                 }
181*cdf0e10cSrcweir             }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             ::rtl::OUString sServiceName;
184*cdf0e10cSrcweir             _rxLoggerSettings->getByName( ::rtl::OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
185*cdf0e10cSrcweir             if ( sServiceName.getLength() )
186*cdf0e10cSrcweir             {
187*cdf0e10cSrcweir                 bool bSuccess = false;
188*cdf0e10cSrcweir                 if ( aSettings.getLength() )
189*cdf0e10cSrcweir                 {
190*cdf0e10cSrcweir                     Sequence< Any > aConstructionArgs(1);
191*cdf0e10cSrcweir                     aConstructionArgs[0] <<= aSettings;
192*cdf0e10cSrcweir                     bSuccess = _rContext.createComponentWithArguments( sServiceName, aConstructionArgs, xInstance );
193*cdf0e10cSrcweir                 }
194*cdf0e10cSrcweir                 else
195*cdf0e10cSrcweir                 {
196*cdf0e10cSrcweir                     bSuccess = _rContext.createComponent( sServiceName, xInstance );
197*cdf0e10cSrcweir                 }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir                 if ( !bSuccess )
200*cdf0e10cSrcweir                     throw ServiceNotRegisteredException( sServiceName, NULL );
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir             return xInstance;
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	//--------------------------------------------------------------------
208*cdf0e10cSrcweir     void initializeLoggerFromConfiguration( const ::comphelper::ComponentContext& _rContext, const Reference< XLogger >& _rxLogger )
209*cdf0e10cSrcweir     {
210*cdf0e10cSrcweir         try
211*cdf0e10cSrcweir         {
212*cdf0e10cSrcweir             if ( !_rxLogger.is() )
213*cdf0e10cSrcweir                 throw NullPointerException();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir             // the configuration provider
216*cdf0e10cSrcweir             Reference< XMultiServiceFactory > xConfigProvider;
217*cdf0e10cSrcweir             ::rtl::OUString sConfigProvServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) );
218*cdf0e10cSrcweir             if ( !_rContext.createComponent( sConfigProvServiceName, xConfigProvider ) )
219*cdf0e10cSrcweir                 throw ServiceNotRegisteredException( sConfigProvServiceName, _rxLogger );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir             // write access to the "Settings" node (which includes settings for all loggers)
222*cdf0e10cSrcweir             Sequence< Any > aArguments(1);
223*cdf0e10cSrcweir             aArguments[0] <<= NamedValue(
224*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) ),
225*cdf0e10cSrcweir                 makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Logging/Settings" ) ) )
226*cdf0e10cSrcweir             );
227*cdf0e10cSrcweir             Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
228*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
229*cdf0e10cSrcweir                 aArguments
230*cdf0e10cSrcweir             ), UNO_QUERY_THROW );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             ::rtl::OUString sLoggerName( _rxLogger->getName() );
233*cdf0e10cSrcweir             if ( !xAllSettings->hasByName( sLoggerName ) )
234*cdf0e10cSrcweir             {
235*cdf0e10cSrcweir                 // no node yet for this logger. Create default settings.
236*cdf0e10cSrcweir                 Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
237*cdf0e10cSrcweir                 Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
238*cdf0e10cSrcweir                 xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
239*cdf0e10cSrcweir                 Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
240*cdf0e10cSrcweir                 xChanges->commitChanges();
241*cdf0e10cSrcweir             }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir             // actually read and forward the settings
244*cdf0e10cSrcweir             Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir             // the log level
247*cdf0e10cSrcweir             sal_Int32 nLogLevel( LogLevel::OFF );
248*cdf0e10cSrcweir             OSL_VERIFY( xLoggerSettings->getByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "LogLevel" ) ) ) >>= nLogLevel );
249*cdf0e10cSrcweir             _rxLogger->setLevel( nLogLevel );
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir             // the default handler, if any
252*cdf0e10cSrcweir             Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
253*cdf0e10cSrcweir             if ( !xUntyped.is() )
254*cdf0e10cSrcweir                 // no handler -> we're done
255*cdf0e10cSrcweir                 return;
256*cdf0e10cSrcweir             Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
257*cdf0e10cSrcweir             _rxLogger->addLogHandler( xHandler );
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir             // The newly created handler might have an own (default) level. Ensure that it uses
260*cdf0e10cSrcweir             // the same level as the logger.
261*cdf0e10cSrcweir             xHandler->setLevel( nLogLevel );
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir             // the default formatter for the handler
264*cdf0e10cSrcweir             xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
265*cdf0e10cSrcweir             if ( !xUntyped.is() )
266*cdf0e10cSrcweir                 // no formatter -> we're done
267*cdf0e10cSrcweir                 return;
268*cdf0e10cSrcweir             Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
269*cdf0e10cSrcweir             xHandler->setFormatter( xFormatter );
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir             // TODO: we could first create the formatter, then the handler. This would allow
272*cdf0e10cSrcweir             // passing the formatter as value in the component context, so the handler would
273*cdf0e10cSrcweir             // not create an own default formatter
274*cdf0e10cSrcweir         }
275*cdf0e10cSrcweir         catch( const Exception& )
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
278*cdf0e10cSrcweir         }
279*cdf0e10cSrcweir     }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir //........................................................................
282*cdf0e10cSrcweir } // namespace logging
283*cdf0e10cSrcweir //........................................................................
284