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