tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / cppu / source / LogBridge / LogBridge.cxx
blob34eb0fcabcbadc882848d4f6bd1181f31cfd1296
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 <osl/mutex.hxx>
22 #include <osl/thread.h>
23 #include <osl/thread.hxx>
24 #include <osl/diagnose.h>
25 #include <cppu/Enterable.hxx>
26 #include <cppu/helper/purpenv/Environment.hxx>
27 #include <cppu/helper/purpenv/Mapping.hxx>
28 #include <com/sun/star/uno/Type.hxx>
29 #include <sal/log.hxx>
31 namespace
33 class LogBridge : public cppu::Enterable
35 osl::Mutex m_mutex;
36 sal_Int32 m_count;
37 oslThreadIdentifier m_threadId;
39 virtual ~LogBridge() override;
41 public:
42 explicit LogBridge();
44 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) override;
45 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) override;
47 virtual void v_enter() override;
48 virtual void v_leave() override;
50 virtual bool v_isValid(OUString * pReason) override;
53 LogBridge::LogBridge()
54 : m_count (0)
55 ,m_threadId(0)
59 LogBridge::~LogBridge()
61 OSL_ASSERT(m_count >= 0);
64 void LogBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
66 enter();
67 pCallee(pParam);
68 leave();
71 void LogBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
73 OSL_ASSERT(m_count > 0);
75 -- m_count;
76 pCallee(pParam);
77 ++ m_count;
79 if (!m_threadId)
80 m_threadId = osl::Thread::getCurrentIdentifier();
83 void LogBridge::v_enter()
85 m_mutex.acquire();
87 OSL_ASSERT(m_count >= 0);
89 if (m_count == 0)
90 m_threadId = osl::Thread::getCurrentIdentifier();
92 ++ m_count;
95 void LogBridge::v_leave()
97 OSL_ASSERT(m_count > 0);
99 -- m_count;
100 if (!m_count)
101 m_threadId = 0;
104 m_mutex.release();
107 bool LogBridge::v_isValid(OUString * pReason)
109 bool result = m_count > 0;
110 if (!result)
112 *pReason = "not entered";
114 else
116 result = m_threadId == osl::Thread::getCurrentIdentifier();
118 if (!result)
119 *pReason = "wrong thread";
122 if (result)
123 *pReason = "OK";
125 return result;
128 void traceValue(typelib_TypeDescriptionReference* _pTypeRef,void* pArg)
130 switch(_pTypeRef->eTypeClass)
132 case typelib_TypeClass_STRING:
133 SAL_INFO("cppu.log", "" << *static_cast< OUString*>(pArg));
134 break;
135 case typelib_TypeClass_BOOLEAN:
136 SAL_INFO("cppu.log", "" << *static_cast<sal_Bool*>(pArg));
137 break;
138 case typelib_TypeClass_BYTE:
139 SAL_INFO("cppu.log", "" << *static_cast<sal_Int8*>(pArg));
140 break;
141 case typelib_TypeClass_CHAR:
142 SAL_INFO("cppu.log", "" << *static_cast<char*>(pArg));
143 break;
144 case typelib_TypeClass_SHORT:
145 case typelib_TypeClass_UNSIGNED_SHORT:
146 SAL_INFO("cppu.log", "" << *static_cast<sal_Int16*>(pArg));
147 break;
148 case typelib_TypeClass_LONG:
149 case typelib_TypeClass_UNSIGNED_LONG:
150 case typelib_TypeClass_ENUM:
151 SAL_INFO("cppu.log", "" << *static_cast<sal_Int32*>(pArg));
152 break;
153 case typelib_TypeClass_HYPER:
154 case typelib_TypeClass_UNSIGNED_HYPER:
155 SAL_INFO("cppu.log", "" << *static_cast<sal_Int64*>(pArg));
156 break;
157 case typelib_TypeClass_FLOAT:
158 SAL_INFO("cppu.log", "" << *static_cast<float*>(pArg));
159 break;
160 case typelib_TypeClass_DOUBLE:
161 SAL_INFO("cppu.log", "" << *static_cast<double*>(pArg));
162 break;
163 case typelib_TypeClass_TYPE:
164 SAL_INFO("cppu.log", "" << static_cast<css::uno::Type*>(pArg)->getTypeName());
165 break;
166 case typelib_TypeClass_ANY:
167 if ( static_cast<uno_Any*>(pArg)->pData )
168 traceValue(static_cast<uno_Any*>(pArg)->pType,static_cast<uno_Any*>(pArg)->pData);
169 else
170 SAL_INFO("cppu.log", "void");
171 break;
172 case typelib_TypeClass_EXCEPTION:
173 SAL_INFO("cppu.log", "exception");
174 break;
175 case typelib_TypeClass_INTERFACE:
176 SAL_INFO("cppu.log", "" << _pTypeRef->pTypeName << "0x" << std::hex << pArg);
177 break;
178 case typelib_TypeClass_VOID:
179 SAL_INFO("cppu.log", "void");
180 break;
181 default:
182 SAL_INFO("cppu.log", "0x" << std::hex << pArg);
183 break;
184 } // switch(pParams[i].pTypeRef->eTypeClass)
188 static void LogProbe(
189 bool pre,
190 SAL_UNUSED_PARAMETER void * /*pThis*/,
191 SAL_UNUSED_PARAMETER void * /*pContext*/,
192 typelib_TypeDescriptionReference * pReturnTypeRef,
193 typelib_MethodParameter * pParams,
194 sal_Int32 nParams,
195 typelib_TypeDescription const * pMemberType,
196 void * pReturn,
197 void * pArgs[],
198 uno_Any ** ppException )
200 OString sTemp;
201 if ( pMemberType && pMemberType->pTypeName )
202 sTemp = OUStringToOString(
203 OUString::unacquired(&pMemberType->pTypeName),RTL_TEXTENCODING_ASCII_US);
204 if ( pre )
206 SAL_INFO("cppu.log", "{ LogBridge () " << sTemp );
207 if ( nParams )
209 SAL_INFO("cppu.log", "\n| : ( LogBridge ");
210 for(sal_Int32 i = 0;i < nParams;++i)
212 if ( i > 0 )
213 SAL_INFO("cppu.log", ",");
214 traceValue(pParams[i].pTypeRef,pArgs[i]);
217 SAL_INFO("cppu.log", ")");
218 } // if ( nParams )
219 SAL_INFO("cppu.log", "\n");
221 else if ( !pre )
223 SAL_INFO("cppu.log", "} LogBridge () " << sTemp);
224 if ( ppException && *ppException )
226 SAL_INFO("cppu.log", " exception occurred : ");
227 typelib_TypeDescription * pElementTypeDescr = nullptr;
228 TYPELIB_DANGER_GET( &pElementTypeDescr, (*ppException)->pType );
229 SAL_INFO("cppu.log", "" << pElementTypeDescr->pTypeName);
230 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
232 else if ( pReturnTypeRef )
234 SAL_INFO("cppu.log", " return : ");
235 traceValue(pReturnTypeRef,pReturn);
236 } // if ( pReturn && pReturnTypeRef )
238 SAL_INFO("cppu.log", "\n");
242 #ifdef DISABLE_DYNLOADING
244 #define uno_initEnvironment log_uno_uno_initEnvironment
245 #define uno_ext_getMapping log_uno_uno_ext_getMapping
247 #endif
249 extern "C" void SAL_DLLPUBLIC_EXPORT uno_initEnvironment(uno_Environment * pEnv) noexcept
251 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new LogBridge());
254 extern "C" void SAL_DLLPUBLIC_EXPORT uno_ext_getMapping(uno_Mapping ** ppMapping,
255 uno_Environment * pFrom,
256 uno_Environment * pTo )
258 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo,LogProbe);
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */