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