Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "loggerconfig.hxx"
22 : #include <stdio.h>
23 :
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>
35 :
36 : #include <tools/diagnose_ex.h>
37 : #include <osl/process.h>
38 : #include <rtl/ustrbuf.hxx>
39 :
40 : #include <cppuhelper/component_context.hxx>
41 :
42 : #include <vector>
43 : #include <sal/macros.h>
44 :
45 : //........................................................................
46 : namespace logging
47 : {
48 : //........................................................................
49 :
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;
69 :
70 : namespace LogLevel = ::com::sun::star::logging::LogLevel;
71 :
72 : namespace
73 : {
74 : //----------------------------------------------------------------
75 : typedef void (*SettingTranslation)( const Reference< XLogger >&, const OUString&, Any& );
76 :
77 : //----------------------------------------------------------------
78 5 : void lcl_substituteFileHandlerURLVariables_nothrow( const Reference< XLogger >& _rxLogger, OUString& _inout_rFileURL )
79 : {
80 25 : struct Variable
81 : {
82 : const sal_Char* pVariablePattern;
83 : const sal_Int32 nPatternLength;
84 : rtl_TextEncoding eEncoding;
85 : const OUString sVariableValue;
86 :
87 25 : 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 25 : ,sVariableValue( _rVariableValue )
93 : {
94 25 : }
95 : };
96 :
97 5 : OUString sLoggerName;
98 5 : try { sLoggerName = _rxLogger->getName(); }
99 0 : catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
100 :
101 : TimeValue aTimeValue;
102 : oslDateTime aDateTime;
103 5 : OSL_VERIFY( osl_getSystemTime( &aTimeValue ) );
104 5 : OSL_VERIFY( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) );
105 :
106 : char buffer[ 30 ];
107 5 : const size_t buffer_size = sizeof( buffer );
108 :
109 : snprintf( buffer, buffer_size, "%04i-%02i-%02i",
110 : (int)aDateTime.Year,
111 : (int)aDateTime.Month,
112 5 : (int)aDateTime.Day );
113 10 : rtl::OUString sDate = rtl::OUString::createFromAscii( buffer );
114 :
115 : snprintf( buffer, buffer_size, "%02i-%02i-%02i.%03i",
116 : (int)aDateTime.Hours,
117 : (int)aDateTime.Minutes,
118 : (int)aDateTime.Seconds,
119 5 : ::sal::static_int_cast< sal_Int16 >( aDateTime.NanoSeconds / 10000000 ) );
120 10 : rtl::OUString sTime = rtl::OUString::createFromAscii( buffer );
121 :
122 10 : rtl::OUStringBuffer aBuff;
123 5 : aBuff.append( sDate );
124 5 : aBuff.append( sal_Unicode( '.' ) );
125 5 : aBuff.append( sTime );
126 10 : rtl::OUString sDateTime = aBuff.makeStringAndClear();
127 :
128 5 : oslProcessIdentifier aProcessId = 0;
129 : oslProcessInfo info;
130 5 : info.Size = sizeof (oslProcessInfo);
131 5 : if ( osl_getProcessInfo ( 0, osl_Process_IDENTIFIER, &info ) == osl_Process_E_None)
132 5 : aProcessId = info.Ident;
133 10 : rtl::OUString aPID = rtl::OUString::valueOf( sal_Int64( aProcessId ) );
134 :
135 : Variable aVariables[] =
136 : {
137 5 : Variable( RTL_CONSTASCII_USTRINGPARAM( "$(loggername)" ), sLoggerName ),
138 5 : Variable( RTL_CONSTASCII_USTRINGPARAM( "$(date)" ), sDate ),
139 5 : Variable( RTL_CONSTASCII_USTRINGPARAM( "$(time)" ), sTime ),
140 5 : Variable( RTL_CONSTASCII_USTRINGPARAM( "$(datetime)" ), sDateTime ),
141 5 : Variable( RTL_CONSTASCII_USTRINGPARAM( "$(pid)" ), aPID )
142 35 : };
143 :
144 30 : for ( size_t i = 0; i < SAL_N_ELEMENTS( aVariables ); ++i )
145 : {
146 25 : OUString sPattern( aVariables[i].pVariablePattern, aVariables[i].nPatternLength, aVariables[i].eEncoding );
147 25 : sal_Int32 nVariableIndex = _inout_rFileURL.indexOf( sPattern );
148 25 : if ( ( nVariableIndex == 0 )
149 30 : || ( ( nVariableIndex > 0 )
150 5 : && ( sPattern[ nVariableIndex - 1 ] != '$' )
151 : )
152 : )
153 : {
154 : // found an (unescaped) variable
155 5 : _inout_rFileURL = _inout_rFileURL.replaceAt( nVariableIndex, sPattern.getLength(), aVariables[i].sVariableValue );
156 : }
157 30 : }
158 5 : }
159 :
160 : //----------------------------------------------------------------
161 5 : void lcl_transformFileHandlerSettings_nothrow( const Reference< XLogger >& _rxLogger, const OUString& _rSettingName, Any& _inout_rSettingValue )
162 : {
163 5 : if ( _rSettingName != "FileURL" )
164 : // not interested in this setting
165 5 : return;
166 :
167 5 : OUString sURL;
168 5 : OSL_VERIFY( _inout_rSettingValue >>= sURL );
169 5 : lcl_substituteFileHandlerURLVariables_nothrow( _rxLogger, sURL );
170 5 : _inout_rSettingValue <<= sURL;
171 : }
172 :
173 : //----------------------------------------------------------------
174 10 : 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
181 : )
182 : {
183 10 : Reference< XInterface > xInstance;
184 :
185 : // read the settings for the to-be-created service
186 10 : Reference< XNameAccess > xServiceSettingsNode( _rxLoggerSettings->getByName(
187 20 : OUString::createFromAscii( _pServiceSettingsAsciiNodeName ) ), UNO_QUERY_THROW );
188 :
189 20 : Sequence< OUString > aSettingNames( xServiceSettingsNode->getElementNames() );
190 10 : size_t nServiceSettingCount( aSettingNames.getLength() );
191 20 : Sequence< NamedValue > aSettings( nServiceSettingCount );
192 10 : if ( nServiceSettingCount )
193 : {
194 5 : const OUString* pSettingNames = aSettingNames.getConstArray();
195 5 : const OUString* pSettingNamesEnd = aSettingNames.getConstArray() + aSettingNames.getLength();
196 5 : NamedValue* pSetting = aSettings.getArray();
197 :
198 10 : for ( ;
199 : pSettingNames != pSettingNamesEnd;
200 : ++pSettingNames, ++pSetting
201 : )
202 : {
203 5 : pSetting->Name = *pSettingNames;
204 5 : pSetting->Value = xServiceSettingsNode->getByName( *pSettingNames );
205 :
206 5 : if ( _pSettingTranslation )
207 5 : (_pSettingTranslation)( _rxLogger, pSetting->Name, pSetting->Value );
208 : }
209 : }
210 :
211 20 : OUString sServiceName;
212 10 : _rxLoggerSettings->getByName( OUString::createFromAscii( _pServiceNameAsciiNodeName ) ) >>= sServiceName;
213 10 : if ( !sServiceName.isEmpty() )
214 : {
215 10 : bool bSuccess = false;
216 10 : if ( aSettings.getLength() )
217 : {
218 5 : Sequence< Any > aConstructionArgs(1);
219 5 : aConstructionArgs[0] <<= aSettings;
220 5 : xInstance = _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName, aConstructionArgs, _rContext);
221 5 : bSuccess = xInstance.is();
222 : }
223 : else
224 : {
225 5 : xInstance = _rContext->getServiceManager()->createInstanceWithContext(sServiceName, _rContext);
226 5 : bSuccess = xInstance.is();
227 : }
228 :
229 10 : if ( !bSuccess )
230 0 : throw ServiceNotRegisteredException( sServiceName, NULL );
231 : }
232 :
233 20 : return xInstance;
234 : }
235 : }
236 :
237 : //--------------------------------------------------------------------
238 5 : void initializeLoggerFromConfiguration( const Reference<XComponentContext>& _rContext, const Reference< XLogger >& _rxLogger )
239 : {
240 : try
241 : {
242 5 : if ( !_rxLogger.is() )
243 0 : throw NullPointerException();
244 :
245 : Reference< XMultiServiceFactory > xConfigProvider(
246 5 : com::sun::star::configuration::theDefaultProvider::get(_rContext));
247 :
248 : // write access to the "Settings" node (which includes settings for all loggers)
249 10 : Sequence< Any > aArguments(1);
250 10 : aArguments[0] <<= NamedValue(
251 : OUString( "nodepath" ),
252 : makeAny( OUString( "/org.openoffice.Office.Logging/Settings" ) )
253 5 : );
254 5 : Reference< XNameContainer > xAllSettings( xConfigProvider->createInstanceWithArguments(
255 : OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
256 : aArguments
257 10 : ), UNO_QUERY_THROW );
258 :
259 10 : OUString sLoggerName( _rxLogger->getName() );
260 5 : if ( !xAllSettings->hasByName( sLoggerName ) )
261 : {
262 : // no node yet for this logger. Create default settings.
263 5 : Reference< XSingleServiceFactory > xNodeFactory( xAllSettings, UNO_QUERY_THROW );
264 10 : Reference< XInterface > xLoggerSettings( xNodeFactory->createInstance(), UNO_QUERY_THROW );
265 5 : xAllSettings->insertByName( sLoggerName, makeAny( xLoggerSettings ) );
266 10 : Reference< XChangesBatch > xChanges( xAllSettings, UNO_QUERY_THROW );
267 10 : xChanges->commitChanges();
268 : }
269 :
270 : // actually read and forward the settings
271 10 : Reference< XNameAccess > xLoggerSettings( xAllSettings->getByName( sLoggerName ), UNO_QUERY_THROW );
272 :
273 : // the log level
274 5 : sal_Int32 nLogLevel( LogLevel::OFF );
275 5 : OSL_VERIFY( xLoggerSettings->getByName("LogLevel") >>= nLogLevel );
276 5 : _rxLogger->setLevel( nLogLevel );
277 :
278 : // the default handler, if any
279 10 : Reference< XInterface > xUntyped( lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultHandler", "HandlerSettings", &lcl_transformFileHandlerSettings_nothrow ) );
280 5 : if ( !xUntyped.is() )
281 : // no handler -> we're done
282 0 : return;
283 10 : Reference< XLogHandler > xHandler( xUntyped, UNO_QUERY_THROW );
284 5 : _rxLogger->addLogHandler( xHandler );
285 :
286 : // The newly created handler might have an own (default) level. Ensure that it uses
287 : // the same level as the logger.
288 5 : xHandler->setLevel( nLogLevel );
289 :
290 : // the default formatter for the handler
291 5 : xUntyped = lcl_createInstanceFromSetting_throw( _rContext, _rxLogger, xLoggerSettings, "DefaultFormatter", "FormatterSettings" );
292 5 : if ( !xUntyped.is() )
293 : // no formatter -> we're done
294 0 : return;
295 10 : Reference< XLogFormatter > xFormatter( xUntyped, UNO_QUERY_THROW );
296 10 : xHandler->setFormatter( xFormatter );
297 :
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
301 : }
302 0 : catch( const Exception& )
303 : {
304 : DBG_UNHANDLED_EXCEPTION();
305 : }
306 : }
307 :
308 : //........................................................................
309 : } // namespace logging
310 : //........................................................................
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|