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