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 : #ifndef INCLUDED_COMPHELPER_LOGGING_HXX
21 : #define INCLUDED_COMPHELPER_LOGGING_HXX
22 :
23 : #include <comphelper/comphelperdllapi.h>
24 :
25 : #include <com/sun/star/uno/XComponentContext.hpp>
26 : #include <com/sun/star/logging/XLogHandler.hpp>
27 : #include <com/sun/star/logging/LogLevel.hpp>
28 :
29 : #include <boost/shared_ptr.hpp>
30 : #include <boost/optional.hpp>
31 :
32 :
33 : namespace comphelper
34 : {
35 :
36 :
37 :
38 : //= string conversions, employed by the templatized log* members of
39 : //= EventLogger
40 :
41 :
42 : namespace log { namespace convert
43 : {
44 0 : inline const OUString& convertLogArgToString( const OUString& _rValue )
45 : {
46 0 : return _rValue;
47 : }
48 :
49 0 : inline OUString convertLogArgToString( const sal_Char* _pAsciiValue )
50 : {
51 0 : return OUString::createFromAscii( _pAsciiValue );
52 : }
53 :
54 0 : inline OUString convertLogArgToString( double _nValue ) { return OUString::number( _nValue ); }
55 0 : inline OUString convertLogArgToString( float _nValue ) { return OUString::number( _nValue ); }
56 0 : inline OUString convertLogArgToString( sal_Int64 _nValue ) { return OUString::number( _nValue ); }
57 0 : inline OUString convertLogArgToString( sal_Int32 _nValue ) { return OUString::number( _nValue ); }
58 0 : inline OUString convertLogArgToString( sal_Int16 _nValue ) { return OUString::number( _nValue ); }
59 : inline OUString convertLogArgToString( sal_Unicode _nValue ) { return OUString( _nValue ); }
60 0 : inline OUString convertLogArgToString( bool _bValue ) { return OUString::boolean( _bValue ); }
61 : void convertLogArgToString(sal_Bool) SAL_DELETED_FUNCTION;
62 :
63 : } } // namespace log::convert
64 :
65 :
66 : //= EventLogger
67 :
68 : class EventLogger_Impl;
69 : typedef ::boost::optional< OUString > OptionalString;
70 :
71 : /** encapsulates an com::sun::star::logging::XLogger
72 :
73 : The class silences several (unlikely) errors which could potentially happen
74 : when working with a logger. Additionally, it provides some convenience methods
75 : for logging events.
76 :
77 : You can use this class as follows
78 : <pre>
79 : EventLogger aLogger( xContext, sLoggerName );
80 : ....
81 : aLogger.log( LogLevel::SEVERE, sSomeMessage );
82 : aLogger.logp( LogLevel::CONFIG, "MyClass", "MyMethod", sSomeMessage, SomeParameter1, SomeParameter2, SomeParameter3 );
83 : </pre>
84 :
85 : The <code>log</code> and <code>logp</code> calls support up to 6 parameters, which can be of
86 : arbitrary type. For every parameter, there must exist a function <code>convertLogArgToString</code>
87 : which takes an argument of the respective type, and returns a string.
88 :
89 : After a parameter has been converted to a string using the above mentioned
90 : <code>convertLogArgToString</code> function, a placeholder $1$ (resp. $2$ resp. $4$ ...)
91 : in the message will be replaced with this string, and the resulting message will be logged.
92 : */
93 0 : class COMPHELPER_DLLPUBLIC EventLogger
94 : {
95 : protected:
96 : ::boost::shared_ptr< EventLogger_Impl > m_pImpl;
97 :
98 : public:
99 : /** creates an <code>EventLogger</code> instance working with a css.logging.XLogger
100 : instance given by ASCII name.
101 :
102 : @param _rxContext
103 : the component context to create services
104 :
105 : @param _rLoggerName
106 : the ASCII name of the logger to create.
107 : */
108 : EventLogger(
109 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
110 : const sal_Char* _pAsciiLoggerName
111 : );
112 :
113 : ~EventLogger();
114 :
115 : public:
116 : /// determines whether an event with the given level would be logged
117 : bool isLoggable( const sal_Int32 _nLogLevel ) const;
118 :
119 :
120 : //- XLogger::log equivalents/wrappers
121 : //- string messages
122 :
123 : /// logs a given message, without any arguments, or source class/method names
124 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage ) const
125 : {
126 : if ( isLoggable( _nLogLevel ) )
127 : return impl_log( _nLogLevel, NULL, NULL, _rMessage );
128 : return false;
129 : }
130 :
131 : /** logs a given message, replacing a placeholder in the message with an argument
132 :
133 : The function takes, additionally to the log level and the message, an arbitrary
134 : argument. This argument is converted to a string using an overloaded function
135 : named <code>convertLogArgToString</code>. Then, a placeholder "$1$"
136 : is searched in the message string, and replaced with the argument string.
137 : */
138 : template< typename ARGTYPE1 >
139 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1 ) const
140 : {
141 : if ( isLoggable( _nLogLevel ) )
142 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
143 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
144 : return false;
145 : }
146 :
147 : /// logs a given message, replacing 2 placeholders in the message with respective values
148 : template< typename ARGTYPE1, typename ARGTYPE2 >
149 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
150 : {
151 : if ( isLoggable( _nLogLevel ) )
152 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
153 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
154 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
155 : return false;
156 : }
157 :
158 : /// logs a given message, replacing 3 placeholders in the message with respective values
159 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
160 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
161 : {
162 : if ( isLoggable( _nLogLevel ) )
163 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
164 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
165 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
166 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
167 : return false;
168 : }
169 :
170 : /// logs a given message, replacing 4 placeholders in the message with respective values
171 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
172 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
173 : {
174 : if ( isLoggable( _nLogLevel ) )
175 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
176 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
177 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
178 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
179 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
180 : return false;
181 : }
182 :
183 : /// logs a given message, replacing 5 placeholders in the message with respective values
184 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
185 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
186 : {
187 : if ( isLoggable( _nLogLevel ) )
188 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
189 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
190 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
191 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
192 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
193 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
194 : return false;
195 : }
196 :
197 : /// logs a given message, replacing 6 placeholders in the message with respective values
198 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
199 : bool log( const sal_Int32 _nLogLevel, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
200 : {
201 : if ( isLoggable( _nLogLevel ) )
202 : return impl_log( _nLogLevel, NULL, NULL, _rMessage,
203 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
204 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
205 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
206 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
207 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
208 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
209 : return false;
210 : }
211 :
212 :
213 : //- XLogger::log equivalents/wrappers
214 : //- ASCII messages
215 :
216 : /// logs a given message, without any arguments, or source class/method names
217 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage ) const
218 : {
219 : if ( isLoggable( _nLogLevel ) )
220 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ) );
221 : return false;
222 : }
223 :
224 : /** logs a given message, replacing a placeholder in the message with an argument
225 :
226 : The function takes, additionally to the log level and the message, an arbitrary
227 : argument. This argument is converted to a string using an overloaded function
228 : named <code>convertLogArgToString</code>. Then, a placeholder "$1$"
229 : is searched in the message string, and replaced with the argument string.
230 : */
231 : template< typename ARGTYPE1 >
232 0 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1 ) const
233 : {
234 0 : if ( isLoggable( _nLogLevel ) )
235 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
236 0 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
237 0 : return false;
238 : }
239 :
240 : /// logs a given message, replacing 2 placeholders in the message with respective values
241 : template< typename ARGTYPE1, typename ARGTYPE2 >
242 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
243 : {
244 : if ( isLoggable( _nLogLevel ) )
245 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
246 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
247 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
248 : return false;
249 : }
250 :
251 : /// logs a given message, replacing 3 placeholders in the message with respective values
252 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
253 0 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
254 : {
255 0 : if ( isLoggable( _nLogLevel ) )
256 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
257 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
258 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
259 0 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
260 0 : return false;
261 : }
262 :
263 : /// logs a given message, replacing 4 placeholders in the message with respective values
264 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
265 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
266 : {
267 : if ( isLoggable( _nLogLevel ) )
268 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
269 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
270 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
271 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
272 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
273 : return false;
274 : }
275 :
276 : /// logs a given message, replacing 5 placeholders in the message with respective values
277 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
278 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
279 : {
280 : if ( isLoggable( _nLogLevel ) )
281 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
282 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
283 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
284 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
285 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
286 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
287 : return false;
288 : }
289 :
290 : /// logs a given message, replacing 6 placeholders in the message with respective values
291 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
292 : bool log( const sal_Int32 _nLogLevel, const sal_Char* _pMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
293 : {
294 : if ( isLoggable( _nLogLevel ) )
295 : return impl_log( _nLogLevel, NULL, NULL, OUString::createFromAscii( _pMessage ),
296 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
297 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
298 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
299 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
300 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
301 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
302 : return false;
303 : }
304 :
305 :
306 : //- XLogger::logp equivalents/wrappers
307 : //- string messages
308 :
309 : /// logs a given message, without any arguments, or source class/method names
310 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage ) const
311 : {
312 : if ( isLoggable( _nLogLevel ) )
313 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage );
314 : return false;
315 : }
316 :
317 : /** logs a given message, replacing a placeholder in the message with an argument
318 :
319 : The function takes, additionally to the logp level and the message, an arbitrary
320 : argument. This argument is converted to a string using an overloaded function
321 : named <code>convertLogArgToString</code>. Then, a placeholder "$1$"
322 : is searched in the message string, and replaced with the argument string.
323 : */
324 : template< typename ARGTYPE1 >
325 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1 ) const
326 : {
327 : if ( isLoggable( _nLogLevel ) )
328 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
329 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
330 : return false;
331 : }
332 :
333 : /// logs a given message, replacing 2 placeholders in the message with respective values
334 : template< typename ARGTYPE1, typename ARGTYPE2 >
335 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
336 : {
337 : if ( isLoggable( _nLogLevel ) )
338 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
339 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
340 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
341 : return false;
342 : }
343 :
344 : /// logs a given message, replacing 3 placeholders in the message with respective values
345 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
346 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
347 : {
348 : if ( isLoggable( _nLogLevel ) )
349 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
350 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
351 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
352 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
353 : return false;
354 : }
355 :
356 : /// logs a given message, replacing 4 placeholders in the message with respective values
357 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
358 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
359 : {
360 : if ( isLoggable( _nLogLevel ) )
361 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
362 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
363 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
364 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
365 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
366 : return false;
367 : }
368 :
369 : /// logs a given message, replacing 5 placeholders in the message with respective values
370 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
371 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
372 : {
373 : if ( isLoggable( _nLogLevel ) )
374 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
375 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
376 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
377 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
378 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
379 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
380 : return false;
381 : }
382 :
383 : /// logs a given message, replacing 6 placeholders in the message with respective values
384 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
385 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const OUString& _rMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
386 : {
387 : if ( isLoggable( _nLogLevel ) )
388 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, _rMessage,
389 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
390 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
391 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
392 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
393 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
394 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
395 : return false;
396 : }
397 :
398 :
399 : //- XLogger::logp equivalents/wrappers
400 : //- ASCII messages
401 :
402 : /// logs a given ASCII message, without any arguments, or source class/method names
403 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage ) const
404 : {
405 : if ( isLoggable( _nLogLevel ) )
406 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ) );
407 : return false;
408 : }
409 :
410 : /** logs a given ASCII message, replacing a placeholder in the message with an argument
411 :
412 : The function takes, additionally to the logp level and the message, an arbitrary
413 : argument. This argument is converted to a string using an overloaded function
414 : named <code>convertLogArgToString</code>. Then, a placeholder "$1$"
415 : is searched in the message string, and replaced with the argument string.
416 : */
417 : template< typename ARGTYPE1 >
418 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1 ) const
419 : {
420 : if ( isLoggable( _nLogLevel ) )
421 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
422 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
423 : return false;
424 : }
425 :
426 : /// logs a given ASCII message, replacing 2 placeholders in the message with respective values
427 : template< typename ARGTYPE1, typename ARGTYPE2 >
428 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
429 : {
430 : if ( isLoggable( _nLogLevel ) )
431 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
432 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
433 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
434 : return false;
435 : }
436 :
437 : /// logs a given ASCII message, replacing 3 placeholders in the message with respective values
438 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
439 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
440 : {
441 : if ( isLoggable( _nLogLevel ) )
442 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
443 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
444 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
445 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
446 : return false;
447 : }
448 :
449 : /// logs a given ASCII message, replacing 4 placeholders in the message with respective values
450 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
451 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
452 : {
453 : if ( isLoggable( _nLogLevel ) )
454 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
455 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
456 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
457 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
458 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
459 : return false;
460 : }
461 :
462 : /// logs a given ASCII message, replacing 5 placeholders in the message with respective values
463 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
464 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
465 : {
466 : if ( isLoggable( _nLogLevel ) )
467 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
468 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
469 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
470 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
471 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
472 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
473 : return false;
474 : }
475 :
476 : /// logs a given ASCII message, replacing 6 placeholders in the message with respective values
477 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
478 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Char* _pAsciiMessage, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
479 : {
480 : if ( isLoggable( _nLogLevel ) )
481 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, OUString::createFromAscii( _pAsciiMessage ),
482 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
483 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
484 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
485 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
486 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
487 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
488 : return false;
489 : }
490 :
491 : protected:
492 : bool impl_log(
493 : const sal_Int32 _nLogLevel,
494 : const sal_Char* _pSourceClass,
495 : const sal_Char* _pSourceMethod,
496 : const OUString& _rMessage,
497 : const OptionalString& _rArgument1 = OptionalString(),
498 : const OptionalString& _rArgument2 = OptionalString(),
499 : const OptionalString& _rArgument3 = OptionalString(),
500 : const OptionalString& _rArgument4 = OptionalString(),
501 : const OptionalString& _rArgument5 = OptionalString(),
502 : const OptionalString& _rArgument6 = OptionalString()
503 : ) const;
504 : };
505 :
506 :
507 : //= ResourceBasedEventLogger
508 :
509 : struct ResourceBasedEventLogger_Data;
510 : /** extends the EventLogger class with functionality to load log messages from
511 : a resource bundle.
512 : */
513 0 : class COMPHELPER_DLLPUBLIC ResourceBasedEventLogger : public EventLogger
514 : {
515 : private:
516 : ::boost::shared_ptr< ResourceBasedEventLogger_Data > m_pData;
517 :
518 : public:
519 : /** creates a resource based event logger
520 : @param _rxContext
521 : the component context for creating new components
522 : @param _pResourceBundleBaseName
523 : the ASCII base name of the resource bundle to use. Will be used
524 : in conjunction with XResourceBundleLoader::loadResource.
525 : @param _pAsciiLoggerName
526 : the ASCII name of the logger to work with. If NULL, the office-wide
527 : default logger will be used.
528 :
529 : */
530 : ResourceBasedEventLogger(
531 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
532 : const sal_Char* _pResourceBundleBaseName,
533 : const sal_Char* _pAsciiLoggerName = NULL
534 : );
535 :
536 :
537 : //- XLogger::log equivalents/wrappers
538 : //- resource IDs
539 :
540 : /// logs a given message, without any arguments, or source class/method names
541 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID ) const
542 : {
543 0 : if ( isLoggable( _nLogLevel ) )
544 0 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ) );
545 0 : return false;
546 : }
547 :
548 : /** logs a given message, replacing a placeholder in the message with an argument
549 :
550 : The function takes, additionally to the log level and the message, an arbitrary
551 : argument. This argument is converted to a string using an overloaded function
552 : named <code>convertLogArgToString</code>. Then, a placeholder "$1$"
553 : is searched in the message string, and replaced with the argument string.
554 : */
555 : template< typename ARGTYPE1 >
556 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1 ) const
557 : {
558 0 : if ( isLoggable( _nLogLevel ) )
559 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
560 0 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
561 0 : return false;
562 : }
563 :
564 : /// logs a given message, replacing 2 placeholders in the message with respective values
565 : template< typename ARGTYPE1, typename ARGTYPE2 >
566 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
567 : {
568 0 : if ( isLoggable( _nLogLevel ) )
569 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
570 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
571 0 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
572 0 : return false;
573 : }
574 :
575 : /// logs a given message, replacing 3 placeholders in the message with respective values
576 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
577 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
578 : {
579 0 : if ( isLoggable( _nLogLevel ) )
580 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
581 0 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
582 0 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
583 0 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
584 0 : return false;
585 : }
586 :
587 : /// logs a given message, replacing 4 placeholders in the message with respective values
588 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
589 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
590 : {
591 0 : if ( isLoggable( _nLogLevel ) )
592 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
593 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
594 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
595 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
596 0 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
597 0 : return false;
598 : }
599 :
600 : /// logs a given message, replacing 5 placeholders in the message with respective values
601 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
602 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
603 : {
604 0 : if ( isLoggable( _nLogLevel ) )
605 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
606 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
607 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
608 0 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
609 0 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
610 0 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
611 0 : return false;
612 : }
613 :
614 : /// logs a given message, replacing 6 placeholders in the message with respective values
615 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
616 0 : bool log( const sal_Int32 _nLogLevel, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
617 : {
618 0 : if ( isLoggable( _nLogLevel ) )
619 : return impl_log( _nLogLevel, NULL, NULL, impl_loadStringMessage_nothrow( _nMessageResID ),
620 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
621 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
622 0 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
623 0 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
624 0 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
625 0 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
626 0 : return false;
627 : }
628 :
629 :
630 : //- XLogger::logp equivalents/wrappers
631 : //- resource IDs
632 :
633 : /// logs a given ASCII message, without any arguments, or source class/method names
634 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID ) const
635 : {
636 : if ( isLoggable( _nLogLevel ) )
637 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ) );
638 : return false;
639 : }
640 :
641 : /** logs a given ASCII message, replacing a placeholder in the message with an argument
642 : */
643 : template< typename ARGTYPE1 >
644 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1 ) const
645 : {
646 : if ( isLoggable( _nLogLevel ) )
647 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
648 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ) );
649 : return false;
650 : }
651 :
652 : /// logs a given ASCII message, replacing 2 placeholders in the message with respective values
653 : template< typename ARGTYPE1, typename ARGTYPE2 >
654 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2 ) const
655 : {
656 : if ( isLoggable( _nLogLevel ) )
657 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
658 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
659 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ) );
660 : return false;
661 : }
662 :
663 : /// logs a given ASCII message, replacing 3 placeholders in the message with respective values
664 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3 >
665 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3 ) const
666 : {
667 : if ( isLoggable( _nLogLevel ) )
668 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
669 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
670 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
671 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ) );
672 : return false;
673 : }
674 :
675 : /// logs a given ASCII message, replacing 4 placeholders in the message with respective values
676 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4 >
677 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4 ) const
678 : {
679 : if ( isLoggable( _nLogLevel ) )
680 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
681 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
682 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
683 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
684 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ) );
685 : return false;
686 : }
687 :
688 : /// logs a given ASCII message, replacing 5 placeholders in the message with respective values
689 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5 >
690 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5 ) const
691 : {
692 : if ( isLoggable( _nLogLevel ) )
693 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
694 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
695 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
696 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
697 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
698 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ) );
699 : return false;
700 : }
701 :
702 : /// logs a given ASCII message, replacing 6 placeholders in the message with respective values
703 : template< typename ARGTYPE1, typename ARGTYPE2, typename ARGTYPE3, typename ARGTYPE4, typename ARGTYPE5, typename ARGTYPE6 >
704 : bool logp( const sal_Int32 _nLogLevel, const sal_Char* _pSourceClass, const sal_Char* _pSourceMethod, const sal_Int32 _nMessageResID, ARGTYPE1 _argument1, ARGTYPE2 _argument2, ARGTYPE3 _argument3, ARGTYPE4 _argument4, ARGTYPE5 _argument5, ARGTYPE6 _argument6 ) const
705 : {
706 : if ( isLoggable( _nLogLevel ) )
707 : return impl_log( _nLogLevel, _pSourceClass, _pSourceMethod, impl_loadStringMessage_nothrow( _nMessageResID ),
708 : OptionalString( log::convert::convertLogArgToString( _argument1 ) ),
709 : OptionalString( log::convert::convertLogArgToString( _argument2 ) ),
710 : OptionalString( log::convert::convertLogArgToString( _argument3 ) ),
711 : OptionalString( log::convert::convertLogArgToString( _argument4 ) ),
712 : OptionalString( log::convert::convertLogArgToString( _argument5 ) ),
713 : OptionalString( log::convert::convertLogArgToString( _argument6 ) ) );
714 : return false;
715 : }
716 :
717 : private:
718 : OUString impl_loadStringMessage_nothrow( const sal_Int32 _nMessageResID ) const;
719 : };
720 :
721 :
722 : } // namespace comphelper
723 :
724 :
725 : #endif // INCLUDED_COMPHELPER_LOGGING_HXX
726 :
727 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|