File: | comphelper/source/misc/logging.cxx |
Location: | line 185, column 17 |
Description: | Called C++ object pointer is null |
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 | #include <comphelper/componentcontext.hxx> | |||
23 | ||||
24 | #include <com/sun/star/logging/LoggerPool.hpp> | |||
25 | #include <com/sun/star/logging/LogLevel.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 | class EventLogger_Impl | |||
54 | { | |||
55 | private: | |||
56 | ::comphelper::ComponentContext m_aContext; | |||
57 | ::rtl::OUString m_sLoggerName; | |||
58 | Reference< XLogger > m_xLogger; | |||
59 | ||||
60 | public: | |||
61 | EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const ::rtl::OUString& _rLoggerName ) | |||
62 | :m_aContext( _rxContext ) | |||
63 | ,m_sLoggerName( _rLoggerName ) | |||
64 | { | |||
65 | impl_createLogger_nothrow(); | |||
66 | } | |||
67 | ||||
68 | inline bool isValid() const { return m_xLogger.is(); } | |||
69 | inline const ::rtl::OUString& getName() const { return m_sLoggerName; } | |||
70 | inline const Reference< XLogger >& getLogger() const { return m_xLogger; } | |||
71 | inline const ::comphelper::ComponentContext& getContext() const { return m_aContext; } | |||
72 | ||||
73 | private: | |||
74 | void impl_createLogger_nothrow(); | |||
75 | }; | |||
76 | ||||
77 | //==================================================================== | |||
78 | //= EventLogger_Impl - implementation | |||
79 | //==================================================================== | |||
80 | //-------------------------------------------------------------------- | |||
81 | void EventLogger_Impl::impl_createLogger_nothrow() | |||
82 | { | |||
83 | try | |||
84 | { | |||
85 | Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext.getUNOContext() ), UNO_QUERY_THROW ); | |||
86 | if ( !m_sLoggerName.isEmpty() ) | |||
87 | m_xLogger = xPool->getNamedLogger( m_sLoggerName ); | |||
88 | else | |||
89 | m_xLogger = xPool->getDefaultLogger(); | |||
90 | } | |||
91 | catch( const Exception& e ) | |||
92 | { | |||
93 | (void)e; | |||
94 | OSL_FAIL( "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "94" ": "), "%s", "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" ); } } while (false); | |||
95 | } | |||
96 | } | |||
97 | ||||
98 | //==================================================================== | |||
99 | //= EventLogger | |||
100 | //==================================================================== | |||
101 | //-------------------------------------------------------------------- | |||
102 | EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pAsciiLoggerName ) | |||
103 | :m_pImpl( new EventLogger_Impl( _rxContext, ::rtl::OUString::createFromAscii( _pAsciiLoggerName ) ) ) | |||
104 | { | |||
105 | } | |||
106 | ||||
107 | //-------------------------------------------------------------------- | |||
108 | EventLogger::~EventLogger() | |||
109 | { | |||
110 | } | |||
111 | ||||
112 | //-------------------------------------------------------------------- | |||
113 | bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const | |||
114 | { | |||
115 | if ( !m_pImpl->isValid() ) | |||
116 | return false; | |||
117 | ||||
118 | try | |||
119 | { | |||
120 | return m_pImpl->getLogger()->isLoggable( _nLogLevel ); | |||
121 | } | |||
122 | catch( const Exception& e ) | |||
123 | { | |||
124 | (void)e; | |||
125 | OSL_FAIL( "EventLogger::isLoggable: caught an exception!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "125" ": "), "%s", "EventLogger::isLoggable: caught an exception!" ); } } while (false); | |||
126 | } | |||
127 | ||||
128 | return false; | |||
129 | } | |||
130 | ||||
131 | //-------------------------------------------------------------------- | |||
132 | namespace | |||
133 | { | |||
134 | void lcl_replaceParameter( ::rtl::OUString& _inout_Message, const ::rtl::OUString& _rPlaceHolder, const ::rtl::OUString& _rReplacement ) | |||
135 | { | |||
136 | sal_Int32 nPlaceholderPosition = _inout_Message.indexOf( _rPlaceHolder ); | |||
137 | OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" )do { if (true && (!(nPlaceholderPosition >= 0))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl" ), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "137" ": "), "%s", "lcl_replaceParameter: placeholder not found!" ); } } while (false); | |||
138 | if ( nPlaceholderPosition < 0 ) | |||
139 | return; | |||
140 | ||||
141 | _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, _rPlaceHolder.getLength(), _rReplacement ); | |||
142 | } | |||
143 | } | |||
144 | ||||
145 | //-------------------------------------------------------------------- | |||
146 | 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 | static ::rtl::OUString sPH1( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$1$" )(&("$1$")[0]), ((sal_Int32)((sizeof ("$1$") / sizeof (("$1$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
154 | static ::rtl::OUString sPH2( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$2$" )(&("$2$")[0]), ((sal_Int32)((sizeof ("$2$") / sizeof (("$2$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
155 | static ::rtl::OUString sPH3( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$3$" )(&("$3$")[0]), ((sal_Int32)((sizeof ("$3$") / sizeof (("$3$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
156 | static ::rtl::OUString sPH4( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$4$" )(&("$4$")[0]), ((sal_Int32)((sizeof ("$4$") / sizeof (("$4$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
157 | static ::rtl::OUString sPH5( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$5$" )(&("$5$")[0]), ((sal_Int32)((sizeof ("$5$") / sizeof (("$5$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
158 | static ::rtl::OUString sPH6( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "$6$" )(&("$6$")[0]), ((sal_Int32)((sizeof ("$6$") / sizeof (("$6$" )[0]))-1)), (((rtl_TextEncoding) 11)) ) ); | |||
159 | ||||
160 | ::rtl::OUString sMessage( _rMessage ); | |||
161 | if ( !!_rArgument1 ) | |||
| ||||
162 | lcl_replaceParameter( sMessage, sPH1, *_rArgument1 ); | |||
163 | ||||
164 | if ( !!_rArgument2 ) | |||
165 | lcl_replaceParameter( sMessage, sPH2, *_rArgument2 ); | |||
166 | ||||
167 | if ( !!_rArgument3 ) | |||
168 | lcl_replaceParameter( sMessage, sPH3, *_rArgument3 ); | |||
169 | ||||
170 | if ( !!_rArgument4 ) | |||
171 | lcl_replaceParameter( sMessage, sPH4, *_rArgument4 ); | |||
172 | ||||
173 | if ( !!_rArgument5 ) | |||
174 | lcl_replaceParameter( sMessage, sPH5, *_rArgument5 ); | |||
175 | ||||
176 | if ( !!_rArgument6 ) | |||
177 | lcl_replaceParameter( sMessage, sPH6, *_rArgument6 ); | |||
178 | ||||
179 | try | |||
180 | { | |||
181 | Reference< XLogger > xLogger( m_pImpl->getLogger() ); | |||
182 | OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" )do { if (true && (!(xLogger.is()))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "182" ": "), "%s", "EventLogger::impl_log: should never be called without a logger!" ); } } while (false); | |||
183 | if ( _pSourceClass && _pSourceMethod ) | |||
184 | { | |||
185 | xLogger->logp( | |||
| ||||
186 | _nLogLevel, | |||
187 | ::rtl::OUString::createFromAscii( _pSourceClass ), | |||
188 | ::rtl::OUString::createFromAscii( _pSourceMethod ), | |||
189 | sMessage | |||
190 | ); | |||
191 | } | |||
192 | else | |||
193 | { | |||
194 | xLogger->log( _nLogLevel, sMessage ); | |||
195 | } | |||
196 | } | |||
197 | catch( const Exception& e ) | |||
198 | { | |||
199 | (void)e; | |||
200 | OSL_FAIL( "EventLogger::impl_log: caught an exception!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "200" ": "), "%s", "EventLogger::impl_log: caught an exception!" ); } } while (false); | |||
201 | } | |||
202 | ||||
203 | return false; | |||
204 | } | |||
205 | ||||
206 | //==================================================================== | |||
207 | //= ResourceBasedEventLogger_Data | |||
208 | //==================================================================== | |||
209 | 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 | ResourceBasedEventLogger_Data() | |||
219 | :sBundleBaseName() | |||
220 | ,bBundleLoaded( false ) | |||
221 | ,xBundle() | |||
222 | { | |||
223 | } | |||
224 | }; | |||
225 | ||||
226 | //-------------------------------------------------------------------- | |||
227 | bool lcl_loadBundle_nothrow( const ComponentContext& _rContext, ResourceBasedEventLogger_Data& _rLoggerData ) | |||
228 | { | |||
229 | if ( _rLoggerData.bBundleLoaded ) | |||
230 | return _rLoggerData.xBundle.is(); | |||
231 | ||||
232 | // no matter what happens below, don't attempt creation ever again | |||
233 | _rLoggerData.bBundleLoaded = true; | |||
234 | ||||
235 | try | |||
236 | { | |||
237 | Reference< XResourceBundleLoader > xLoader( _rContext.getSingleton( "com.sun.star.resource.OfficeResourceLoader" ), UNO_QUERY_THROW ); | |||
238 | _rLoggerData.xBundle = Reference< XResourceBundle >( xLoader->loadBundle_Default( _rLoggerData.sBundleBaseName ), UNO_QUERY_THROW ); | |||
239 | } | |||
240 | catch( const Exception& e ) | |||
241 | { | |||
242 | (void)e; | |||
243 | OSL_FAIL( "lcl_loadBundle_nothrow: caught an exception!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "243" ": "), "%s", "lcl_loadBundle_nothrow: caught an exception!" ); } } while (false); | |||
244 | } | |||
245 | ||||
246 | return _rLoggerData.xBundle.is(); | |||
247 | } | |||
248 | ||||
249 | //-------------------------------------------------------------------- | |||
250 | ::rtl::OUString lcl_loadString_nothrow( const Reference< XResourceBundle >& _rxBundle, const sal_Int32 _nMessageResID ) | |||
251 | { | |||
252 | OSL_PRECOND( _rxBundle.is(), "lcl_loadString_nothrow: this will crash!" )do { if (true && (!(_rxBundle.is()))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "252" ": "), "%s", "lcl_loadString_nothrow: this will crash!" ); } } while (false); | |||
253 | ::rtl::OUString sMessage; | |||
254 | try | |||
255 | { | |||
256 | ::rtl::OUStringBuffer aBuffer; | |||
257 | aBuffer.appendAscii( "string:" ); | |||
258 | aBuffer.append( _nMessageResID ); | |||
259 | OSL_VERIFY( _rxBundle->getDirectElement( aBuffer.makeStringAndClear() ) >>= sMessage )do { if (!(_rxBundle->getDirectElement( aBuffer.makeStringAndClear () ) >>= sMessage)) do { if (true && (!(0))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "259" ": "), "OSL_ASSERT: %s", "0"); } } while (false); } while (0); | |||
260 | } | |||
261 | catch( const Exception& e ) | |||
262 | { | |||
263 | (void)e; | |||
264 | OSL_FAIL( "lcl_loadString_nothrow: caught an exception!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/misc/logging.cxx" ":" "264" ": "), "%s", "lcl_loadString_nothrow: caught an exception!" ); } } while (false); | |||
265 | } | |||
266 | return sMessage; | |||
267 | } | |||
268 | ||||
269 | //==================================================================== | |||
270 | //= ResourceBasedEventLogger | |||
271 | //==================================================================== | |||
272 | //-------------------------------------------------------------------- | |||
273 | ResourceBasedEventLogger::ResourceBasedEventLogger( const Reference< XComponentContext >& _rxContext, const sal_Char* _pResourceBundleBaseName, | |||
274 | const sal_Char* _pAsciiLoggerName ) | |||
275 | :EventLogger( _rxContext, _pAsciiLoggerName ) | |||
276 | ,m_pData( new ResourceBasedEventLogger_Data ) | |||
277 | { | |||
278 | m_pData->sBundleBaseName = ::rtl::OUString::createFromAscii( _pResourceBundleBaseName ); | |||
279 | } | |||
280 | ||||
281 | //-------------------------------------------------------------------- | |||
282 | ::rtl::OUString ResourceBasedEventLogger::impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const | |||
283 | { | |||
284 | ::rtl::OUString sMessage; | |||
285 | if ( lcl_loadBundle_nothrow( m_pImpl->getContext(), *m_pData ) ) | |||
286 | sMessage = lcl_loadString_nothrow( m_pData->xBundle, _nMessageResID ); | |||
287 | if ( sMessage.isEmpty() ) | |||
288 | { | |||
289 | ::rtl::OUStringBuffer aBuffer; | |||
290 | aBuffer.appendAscii( "<invalid event resource: '" ); | |||
291 | aBuffer.append( m_pData->sBundleBaseName ); | |||
292 | aBuffer.appendAscii( ":" ); | |||
293 | aBuffer.append( _nMessageResID ); | |||
294 | aBuffer.appendAscii( "'>" ); | |||
295 | sMessage = aBuffer.makeStringAndClear(); | |||
296 | } | |||
297 | return sMessage; | |||
298 | } | |||
299 | ||||
300 | //........................................................................ | |||
301 | } // namespace comphelper | |||
302 | //........................................................................ | |||
303 | ||||
304 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |