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 <comphelper/logging.hxx>
22 :
23 : #include <com/sun/star/logging/LoggerPool.hpp>
24 : #include <com/sun/star/logging/LogLevel.hpp>
25 : #include <com/sun/star/resource/OfficeResourceLoader.hpp>
26 : #include <com/sun/star/resource/XResourceBundle.hpp>
27 : #include <com/sun/star/resource/XResourceBundleLoader.hpp>
28 :
29 : #include <rtl/ustrbuf.hxx>
30 :
31 : //........................................................................
32 : namespace comphelper
33 : {
34 : //........................................................................
35 :
36 : using ::com::sun::star::uno::Reference;
37 : using ::com::sun::star::uno::XComponentContext;
38 : using ::com::sun::star::logging::XLoggerPool;
39 : using ::com::sun::star::logging::LoggerPool;
40 : using ::com::sun::star::logging::XLogger;
41 : using ::com::sun::star::uno::UNO_QUERY_THROW;
42 : using ::com::sun::star::uno::Exception;
43 : using ::com::sun::star::logging::XLogHandler;
44 : using ::com::sun::star::resource::XResourceBundle;
45 : using ::com::sun::star::resource::XResourceBundleLoader;
46 :
47 : namespace LogLevel = ::com::sun::star::logging::LogLevel;
48 :
49 : //====================================================================
50 : //= EventLogger_Impl - declaration
51 : //====================================================================
52 0 : class EventLogger_Impl
53 : {
54 : private:
55 : Reference< XComponentContext > m_aContext;
56 : OUString m_sLoggerName;
57 : Reference< XLogger > m_xLogger;
58 :
59 : public:
60 5 : EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
61 : :m_aContext( _rxContext )
62 5 : ,m_sLoggerName( _rLoggerName )
63 : {
64 5 : impl_createLogger_nothrow();
65 5 : }
66 :
67 224 : inline bool isValid() const { return m_xLogger.is(); }
68 : inline const OUString& getName() const { return m_sLoggerName; }
69 224 : inline const Reference< XLogger >& getLogger() const { return m_xLogger; }
70 0 : inline Reference< XComponentContext > getContext() const { return m_aContext; }
71 :
72 : private:
73 : void impl_createLogger_nothrow();
74 : };
75 :
76 : //====================================================================
77 : //= EventLogger_Impl - implementation
78 : //====================================================================
79 : //--------------------------------------------------------------------
80 5 : void EventLogger_Impl::impl_createLogger_nothrow()
81 : {
82 : try
83 : {
84 5 : Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext ) );
85 5 : if ( !m_sLoggerName.isEmpty() )
86 5 : m_xLogger = xPool->getNamedLogger( m_sLoggerName );
87 : else
88 0 : m_xLogger = xPool->getDefaultLogger();
89 : }
90 0 : catch( const Exception& e )
91 : {
92 : (void)e;
93 : OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
94 : }
95 5 : }
96 :
97 : //====================================================================
98 : //= EventLogger
99 : //====================================================================
100 : //--------------------------------------------------------------------
101 5 : EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName )
102 5 : :m_pImpl( new EventLogger_Impl( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
103 : {
104 5 : }
105 :
106 : //--------------------------------------------------------------------
107 0 : EventLogger::~EventLogger()
108 : {
109 0 : }
110 :
111 : //--------------------------------------------------------------------
112 224 : bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
113 : {
114 224 : if ( !m_pImpl->isValid() )
115 0 : return false;
116 :
117 : try
118 : {
119 224 : return m_pImpl->getLogger()->isLoggable( _nLogLevel );
120 : }
121 0 : catch( const Exception& e )
122 : {
123 : (void)e;
124 : OSL_FAIL( "EventLogger::isLoggable: caught an exception!" );
125 : }
126 :
127 0 : return false;
128 : }
129 :
130 : //--------------------------------------------------------------------
131 : namespace
132 : {
133 0 : void lcl_replaceParameter( OUString& _inout_Message, const OUString& _rPlaceHolder, const OUString& _rReplacement )
134 : {
135 0 : sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder );
136 : OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
137 0 : if ( nPlaceholderPosition < 0 )
138 0 : return;
139 :
140 0 : _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement );
141 : }
142 : }
143 :
144 : //--------------------------------------------------------------------
145 0 : bool EventLogger::impl_log( const sal_Int32 _nLogLevel,
146 : const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage,
147 : const OptionalString& _rArgument1, const OptionalString& _rArgument2,
148 : const OptionalString& _rArgument3, const OptionalString& _rArgument4,
149 : const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
150 : {
151 : // (if OUString had an indexOfAscii, we could save those ugly statics ...)
152 0 : static OUString sPH1( "$1$" );
153 0 : static OUString sPH2( "$2$" );
154 0 : static OUString sPH3( "$3$" );
155 0 : static OUString sPH4( "$4$" );
156 0 : static OUString sPH5( "$5$" );
157 0 : static OUString sPH6( "$6$" );
158 :
159 0 : OUString sMessage( _rMessage );
160 0 : if ( !!_rArgument1 )
161 0 : lcl_replaceParameter( sMessage, sPH1, *_rArgument1 );
162 :
163 0 : if ( !!_rArgument2 )
164 0 : lcl_replaceParameter( sMessage, sPH2, *_rArgument2 );
165 :
166 0 : if ( !!_rArgument3 )
167 0 : lcl_replaceParameter( sMessage, sPH3, *_rArgument3 );
168 :
169 0 : if ( !!_rArgument4 )
170 0 : lcl_replaceParameter( sMessage, sPH4, *_rArgument4 );
171 :
172 0 : if ( !!_rArgument5 )
173 0 : lcl_replaceParameter( sMessage, sPH5, *_rArgument5 );
174 :
175 0 : if ( !!_rArgument6 )
176 0 : lcl_replaceParameter( sMessage, sPH6, *_rArgument6 );
177 :
178 : try
179 : {
180 0 : Reference< XLogger > xLogger( m_pImpl->getLogger() );
181 : OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
182 0 : if ( _pSourceClass && _pSourceMethod )
183 : {
184 0 : xLogger->logp(
185 : _nLogLevel,
186 : OUString::createFromAscii( _pSourceClass ),
187 : OUString::createFromAscii( _pSourceMethod ),
188 : sMessage
189 0 : );
190 : }
191 : else
192 : {
193 0 : xLogger->log( _nLogLevel, sMessage );
194 0 : }
195 : }
196 0 : catch( const Exception& e )
197 : {
198 : (void)e;
199 : OSL_FAIL( "EventLogger::impl_log: caught an exception!" );
200 : }
201 :
202 0 : return false;
203 : }
204 :
205 : //====================================================================
206 : //= ResourceBasedEventLogger_Data
207 : //====================================================================
208 0 : struct ResourceBasedEventLogger_Data
209 : {
210 : /// the base name of the resource bundle
211 : OUString sBundleBaseName;
212 : /// did we already attempt to load the bundle?
213 : bool bBundleLoaded;
214 : /// the lazily loaded bundle
215 : Reference< XResourceBundle > xBundle;
216 :
217 0 : ResourceBasedEventLogger_Data()
218 : :sBundleBaseName()
219 : ,bBundleLoaded( false )
220 0 : ,xBundle()
221 : {
222 0 : }
223 : };
224 :
225 : //--------------------------------------------------------------------
226 0 : bool lcl_loadBundle_nothrow( Reference< XComponentContext > const & _rContext, ResourceBasedEventLogger_Data& _rLoggerData )
227 : {
228 0 : if ( _rLoggerData.bBundleLoaded )
229 0 : return _rLoggerData.xBundle.is();
230 :
231 : // no matter what happens below, don't attempt creation ever again
232 0 : _rLoggerData.bBundleLoaded = true;
233 :
234 : try
235 : {
236 : Reference< XResourceBundleLoader > xLoader(
237 : com::sun::star::resource::OfficeResourceLoader::get(
238 0 : _rContext ) );
239 0 : _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW );
240 : }
241 0 : catch( const Exception& e )
242 : {
243 : (void)e;
244 : OSL_FAIL( "lcl_loadBundle_nothrow: caught an exception!" );
245 : }
246 :
247 0 : return _rLoggerData.xBundle.is();
248 : }
249 :
250 : //--------------------------------------------------------------------
251 0 : OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID )
252 : {
253 : OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" );
254 0 : OUString sMessage;
255 : try
256 : {
257 0 : OUStringBuffer aBuffer;
258 0 : aBuffer.appendAscii( "string:" );
259 0 : aBuffer.append( _nMessageResID );
260 0 : OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage );
261 : }
262 0 : catch( const Exception& e )
263 : {
264 : (void)e;
265 : OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" );
266 : }
267 0 : return sMessage;
268 : }
269 :
270 : //====================================================================
271 : //= ResourceBasedEventLogger
272 : //====================================================================
273 : //--------------------------------------------------------------------
274 0 : ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName,
275 : const sal_Char* _pAsciiLoggerName )
276 : :EventLogger( _rxContext, _pAsciiLoggerName )
277 0 : ,m_pData( new ResourceBasedEventLogger_Data )
278 : {
279 0 : m_pData->sBundleBaseName = OUString::createFromAscii( _pResourceBundleBaseName );
280 0 : }
281 :
282 : //--------------------------------------------------------------------
283 0 : OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const
284 : {
285 0 : OUString sMessage;
286 0 : if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) )
287 0 : sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID );
288 0 : if ( sMessage.isEmpty() )
289 : {
290 0 : OUStringBuffer aBuffer;
291 0 : aBuffer.appendAscii( "<invalid event resource: '" );
292 0 : aBuffer.append( m_pData->sBundleBaseName );
293 0 : aBuffer.appendAscii( ":" );
294 0 : aBuffer.append( _nMessageResID );
295 0 : aBuffer.appendAscii( "'>" );
296 0 : sMessage = aBuffer.makeStringAndClear();
297 : }
298 0 : return sMessage;
299 : }
300 :
301 : //........................................................................
302 : } // namespace comphelper
303 : //........................................................................
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|