LibreOffice Module comphelper (master) 1
logging.cxx
Go to the documentation of this file.
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
22
23#include <com/sun/star/logging/LoggerPool.hpp>
24
26#include <osl/diagnose.h>
27
28
29namespace comphelper
30{
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::XComponentContext;
33 using ::com::sun::star::logging::XLoggerPool;
34 using ::com::sun::star::logging::LoggerPool;
35 using ::com::sun::star::logging::XLogger;
36 using ::com::sun::star::uno::Exception;
37
39 {
40 private:
41 Reference< XComponentContext > m_aContext;
42 Reference< XLogger > m_xLogger;
43
44 public:
45 EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName );
46
47 bool isValid() const { return m_xLogger.is(); }
48 const Reference< XLogger >& getLogger() const { return m_xLogger; }
49 };
50
51 EventLogger_Impl::EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
52 :m_aContext( _rxContext )
53 {
54 try
55 {
56 Reference< XLoggerPool > xPool( LoggerPool::get( m_aContext ) );
57 if ( !_rLoggerName.isEmpty() )
58 m_xLogger = xPool->getNamedLogger( _rLoggerName );
59 else
60 m_xLogger = xPool->getDefaultLogger();
61 }
62 catch( const Exception& )
63 {
65 "comphelper", "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
66 }
67 }
68
69 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const char* _pAsciiLoggerName )
70 :m_pImpl( std::make_shared<EventLogger_Impl>( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
71 {
72 }
73
74 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
75 {
76 if ( !m_pImpl->isValid() )
77 return false;
78
79 try
80 {
81 return m_pImpl->getLogger()->isLoggable( _nLogLevel );
82 }
83 catch( const Exception& )
84 {
85 TOOLS_WARN_EXCEPTION( "comphelper", "EventLogger::isLoggable: caught an exception!" );
86 }
87
88 return false;
89 }
90
91 const css::uno::Reference<css::logging::XLogger> & EventLogger::getLogger() const
92 {
93 return m_pImpl->getLogger();
94 }
95
96
97 namespace
98 {
99 void lcl_replaceParameter( OUString& _inout_Message, const char* _rPlaceHolder, std::u16string_view _rReplacement )
100 {
101 sal_Int32 nPlaceholderPosition = _inout_Message.indexOfAsciiL( _rPlaceHolder, strlen(_rPlaceHolder) );
102 OSL_ENSURE( nPlaceholderPosition >= 0, "lcl_replaceParameter: placeholder not found!" );
103 if ( nPlaceholderPosition < 0 )
104 return;
105
106 _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, strlen(_rPlaceHolder), _rReplacement );
107 }
108 }
109
110
111 void EventLogger::impl_log( const sal_Int32 _nLogLevel,
112 const char* _pSourceClass, const char* _pSourceMethod, const OUString& _rMessage,
113 const OptionalString& _rArgument1, const OptionalString& _rArgument2,
114 const OptionalString& _rArgument3, const OptionalString& _rArgument4,
115 const OptionalString& _rArgument5, const OptionalString& _rArgument6 ) const
116 {
117 OUString sMessage( _rMessage );
118 if ( !!_rArgument1 )
119 lcl_replaceParameter( sMessage, "$1$", *_rArgument1 );
120
121 if ( !!_rArgument2 )
122 lcl_replaceParameter( sMessage, "$2$", *_rArgument2 );
123
124 if ( !!_rArgument3 )
125 lcl_replaceParameter( sMessage, "$3$", *_rArgument3 );
126
127 if ( !!_rArgument4 )
128 lcl_replaceParameter( sMessage, "$4$", *_rArgument4 );
129
130 if ( !!_rArgument5 )
131 lcl_replaceParameter( sMessage, "$5$", *_rArgument5 );
132
133 if ( !!_rArgument6 )
134 lcl_replaceParameter( sMessage, "$6$", *_rArgument6 );
135
136 try
137 {
138 Reference< XLogger > xLogger( m_pImpl->getLogger() );
139 OSL_PRECOND( xLogger.is(), "EventLogger::impl_log: should never be called without a logger!" );
140 if ( _pSourceClass && _pSourceMethod )
141 {
142 xLogger->logp(
143 _nLogLevel,
144 OUString::createFromAscii( _pSourceClass ),
145 OUString::createFromAscii( _pSourceMethod ),
147 );
148 }
149 else
150 {
151 xLogger->log( _nLogLevel, sMessage );
152 }
153 }
154 catch( const Exception& )
155 {
156 TOOLS_WARN_EXCEPTION( "comphelper", "EventLogger::impl_log: caught an exception!" );
157 }
158 }
159} // namespace comphelper
160
161/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
Reference< XLogger > m_xLogger
Definition: logging.cxx:42
EventLogger_Impl(const Reference< XComponentContext > &_rxContext, const OUString &_rLoggerName)
Definition: logging.cxx:51
Reference< XComponentContext > m_aContext
Definition: logging.cxx:41
const Reference< XLogger > & getLogger() const
Definition: logging.cxx:48
bool isLoggable(const sal_Int32 _nLogLevel) const
determines whether an event with the given level would be logged
Definition: logging.cxx:74
std::shared_ptr< EventLogger_Impl > m_pImpl
Definition: logging.hxx:94
const css::uno::Reference< css::logging::XLogger > & getLogger() const
Definition: logging.cxx:91
void impl_log(const sal_Int32 _nLogLevel, const char *_pSourceClass, const char *_pSourceMethod, const OUString &_rMessage, const OptionalString &_rArgument1=OptionalString(), const OptionalString &_rArgument2=OptionalString(), const OptionalString &_rArgument3=OptionalString(), const OptionalString &_rArgument4=OptionalString(), const OptionalString &_rArgument5=OptionalString(), const OptionalString &_rArgument6=OptionalString()) const
Definition: logging.cxx:111
EventLogger(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const char *_pAsciiLoggerName)
creates an EventLogger instance working with a css.logging.XLogger instance given by ASCII name.
Definition: logging.cxx:69
Reference< XComponentContext > m_aContext
#define TOOLS_WARN_EXCEPTION(area, stream)
Logs an message along with a nicely formatted version of the current exception.
@ Exception
::std::optional< OUString > OptionalString
Definition: logging.hxx:67
std::shared_ptr< T > make_shared(Args &&... args)
OUString sMessage