Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / misc / logging.cxx
blob3bce820a82ac1f7e3cc3db8172b16d0817289880
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <comphelper/logging.hxx>
23 #include <com/sun/star/logging/LoggerPool.hpp>
25 #include <comphelper/diagnose_ex.hxx>
26 #include <osl/diagnose.h>
29 namespace comphelper
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;
38 class EventLogger_Impl
40 private:
41 Reference< XComponentContext > m_aContext;
42 Reference< XLogger > m_xLogger;
44 public:
45 EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName );
47 bool isValid() const { return m_xLogger.is(); }
48 const Reference< XLogger >& getLogger() const { return m_xLogger; }
51 EventLogger_Impl::EventLogger_Impl( const Reference< XComponentContext >& _rxContext, const OUString& _rLoggerName )
52 :m_aContext( _rxContext )
54 try
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();
62 catch( const Exception& )
64 TOOLS_WARN_EXCEPTION(
65 "comphelper", "EventLogger_Impl::impl_createLogger_nothrow: caught an exception!" );
69 EventLogger::EventLogger( const Reference< XComponentContext >& _rxContext, const char* _pAsciiLoggerName )
70 :m_pImpl( std::make_shared<EventLogger_Impl>( _rxContext, OUString::createFromAscii( _pAsciiLoggerName ) ) )
74 bool EventLogger::isLoggable( const sal_Int32 _nLogLevel ) const
76 if ( !m_pImpl->isValid() )
77 return false;
79 try
81 return m_pImpl->getLogger()->isLoggable( _nLogLevel );
83 catch( const Exception& )
85 TOOLS_WARN_EXCEPTION( "comphelper", "EventLogger::isLoggable: caught an exception!" );
88 return false;
91 const css::uno::Reference<css::logging::XLogger> & EventLogger::getLogger() const
93 return m_pImpl->getLogger();
97 namespace
99 void lcl_replaceParameter( OUString& _inout_Message, const char* _rPlaceHolder, std::u16string_view _rReplacement )
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;
106 _inout_Message = _inout_Message.replaceAt( nPlaceholderPosition, strlen(_rPlaceHolder), _rReplacement );
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
117 OUString sMessage( _rMessage );
118 if ( !!_rArgument1 )
119 lcl_replaceParameter( sMessage, "$1$", *_rArgument1 );
121 if ( !!_rArgument2 )
122 lcl_replaceParameter( sMessage, "$2$", *_rArgument2 );
124 if ( !!_rArgument3 )
125 lcl_replaceParameter( sMessage, "$3$", *_rArgument3 );
127 if ( !!_rArgument4 )
128 lcl_replaceParameter( sMessage, "$4$", *_rArgument4 );
130 if ( !!_rArgument5 )
131 lcl_replaceParameter( sMessage, "$5$", *_rArgument5 );
133 if ( !!_rArgument6 )
134 lcl_replaceParameter( sMessage, "$6$", *_rArgument6 );
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 )
142 xLogger->logp(
143 _nLogLevel,
144 OUString::createFromAscii( _pSourceClass ),
145 OUString::createFromAscii( _pSourceMethod ),
146 sMessage
149 else
151 xLogger->log( _nLogLevel, sMessage );
154 catch( const Exception& )
156 TOOLS_WARN_EXCEPTION( "comphelper", "EventLogger::impl_log: caught an exception!" );
159 } // namespace comphelper
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */