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