Line data Source code
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 "osl/mutex.hxx"
22 : #include "osl/thread.h"
23 : #include "osl/thread.hxx"
24 : #include <osl/diagnose.h>
25 : #include "uno/dispatcher.h"
26 : #include "typelib/typedescription.hxx"
27 : #include <cppu/Enterable.hxx>
28 : #include "cppu/helper/purpenv/Environment.hxx"
29 : #include "cppu/helper/purpenv/Mapping.hxx"
30 : #include "cppu/EnvDcp.hxx"
31 : #include "uno/environment.hxx"
32 : #include <com/sun/star/uno/Type.hxx>
33 : #include <sal/log.hxx>
34 :
35 : namespace
36 : {
37 : class LogBridge : public cppu::Enterable
38 : {
39 : osl::Mutex m_mutex;
40 : sal_Int32 m_count;
41 : oslThreadIdentifier m_threadId;
42 :
43 : virtual ~LogBridge();
44 :
45 : public:
46 : explicit LogBridge();
47 :
48 : virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) SAL_OVERRIDE;
49 : virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) SAL_OVERRIDE;
50 :
51 : virtual void v_enter() SAL_OVERRIDE;
52 : virtual void v_leave() SAL_OVERRIDE;
53 :
54 : virtual bool v_isValid(rtl::OUString * pReason) SAL_OVERRIDE;
55 : };
56 :
57 0 : LogBridge::LogBridge()
58 : : m_count (0)
59 0 : ,m_threadId(0)
60 : {
61 0 : }
62 :
63 0 : LogBridge::~LogBridge()
64 : {
65 : OSL_ASSERT(m_count >= 0);
66 0 : }
67 :
68 0 : void LogBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
69 : {
70 0 : enter();
71 0 : pCallee(pParam);
72 0 : leave();
73 0 : }
74 :
75 0 : void LogBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
76 : {
77 : OSL_ASSERT(m_count > 0);
78 :
79 0 : -- m_count;
80 0 : pCallee(pParam);
81 0 : ++ m_count;
82 :
83 0 : if (!m_threadId)
84 0 : m_threadId = osl::Thread::getCurrentIdentifier();
85 0 : }
86 :
87 0 : void LogBridge::v_enter()
88 : {
89 0 : m_mutex.acquire();
90 :
91 : OSL_ASSERT(m_count >= 0);
92 :
93 0 : if (m_count == 0)
94 0 : m_threadId = osl::Thread::getCurrentIdentifier();
95 :
96 0 : ++ m_count;
97 0 : }
98 :
99 0 : void LogBridge::v_leave()
100 : {
101 : OSL_ASSERT(m_count > 0);
102 :
103 0 : -- m_count;
104 0 : if (!m_count)
105 0 : m_threadId = 0;
106 :
107 :
108 0 : m_mutex.release();
109 0 : }
110 :
111 0 : bool LogBridge::v_isValid(rtl::OUString * pReason)
112 : {
113 0 : bool result = m_count > 0;
114 0 : if (!result)
115 : {
116 0 : *pReason = "not entered";
117 : }
118 : else
119 : {
120 0 : result = m_threadId == osl::Thread::getCurrentIdentifier();
121 :
122 0 : if (!result)
123 0 : *pReason = "wrong thread";
124 : }
125 :
126 0 : if (result)
127 0 : *pReason = "OK";
128 :
129 0 : return result;
130 : }
131 :
132 0 : void traceValue(typelib_TypeDescriptionReference* _pTypeRef,void* pArg)
133 : {
134 0 : switch(_pTypeRef->eTypeClass)
135 : {
136 : case typelib_TypeClass_STRING:
137 : {
138 0 : const ::rtl::OString sValue( ::rtl::OUStringToOString(*static_cast< ::rtl::OUString*>(pArg),osl_getThreadTextEncoding()));
139 0 : SAL_INFO("cppu.log", "" << sValue.getStr());
140 : }
141 0 : break;
142 : case typelib_TypeClass_BOOLEAN:
143 : SAL_INFO("cppu.log", "" << *static_cast<sal_Bool*>(pArg));
144 0 : break;
145 : case typelib_TypeClass_BYTE:
146 : SAL_INFO("cppu.log", "" << *static_cast<sal_Int8*>(pArg));
147 0 : break;
148 : case typelib_TypeClass_CHAR:
149 : SAL_INFO("cppu.log", "" << *static_cast<sal_Char*>(pArg));
150 0 : break;
151 : case typelib_TypeClass_SHORT:
152 : case typelib_TypeClass_UNSIGNED_SHORT:
153 : SAL_INFO("cppu.log", "" << *static_cast<sal_Int16*>(pArg));
154 0 : break;
155 : case typelib_TypeClass_LONG:
156 : case typelib_TypeClass_UNSIGNED_LONG:
157 : case typelib_TypeClass_ENUM:
158 : SAL_INFO("cppu.log", "" << *static_cast<sal_Int32*>(pArg));
159 0 : break;
160 : case typelib_TypeClass_HYPER:
161 : case typelib_TypeClass_UNSIGNED_HYPER:
162 : SAL_INFO("cppu.log", "" << *static_cast<sal_Int64*>(pArg));
163 0 : break;
164 : case typelib_TypeClass_FLOAT:
165 : SAL_INFO("cppu.log", "" << *static_cast<float*>(pArg));
166 0 : break;
167 : case typelib_TypeClass_DOUBLE:
168 : SAL_INFO("cppu.log", "" << *static_cast<double*>(pArg));
169 0 : break;
170 : case typelib_TypeClass_TYPE:
171 : {
172 0 : const ::rtl::OString sValue( ::rtl::OUStringToOString(static_cast<com::sun::star::uno::Type*>(pArg)->getTypeName(),osl_getThreadTextEncoding()));
173 0 : SAL_INFO("cppu.log", "" << sValue.getStr());
174 : }
175 0 : break;
176 : case typelib_TypeClass_ANY:
177 0 : if ( static_cast<uno_Any*>(pArg)->pData )
178 0 : traceValue(static_cast<uno_Any*>(pArg)->pType,static_cast<uno_Any*>(pArg)->pData);
179 : else
180 : SAL_INFO("cppu.log", "void");
181 0 : break;
182 : case typelib_TypeClass_EXCEPTION:
183 : SAL_INFO("cppu.log", "exception");
184 0 : break;
185 : case typelib_TypeClass_INTERFACE:
186 : {
187 0 : const ::rtl::OString sValue( ::rtl::OUStringToOString(_pTypeRef->pTypeName,osl_getThreadTextEncoding()));
188 0 : SAL_INFO("cppu.log", "" << sValue.getStr() << "0x" << std::hex << pArg);
189 : }
190 0 : break;
191 : case typelib_TypeClass_VOID:
192 : SAL_INFO("cppu.log", "void");
193 0 : break;
194 : default:
195 : SAL_INFO("cppu.log", "0x" << std::hex << pArg);
196 0 : break;
197 : } // switch(pParams[i].pTypeRef->eTypeClass)
198 0 : }
199 : }
200 :
201 0 : void LogProbe(
202 : bool pre,
203 : SAL_UNUSED_PARAMETER void * /*pThis*/,
204 : SAL_UNUSED_PARAMETER void * /*pContext*/,
205 : typelib_TypeDescriptionReference * pReturnTypeRef,
206 : typelib_MethodParameter * pParams,
207 : sal_Int32 nParams,
208 : typelib_TypeDescription const * pMemberType,
209 : void * pReturn,
210 : void * pArgs[],
211 : uno_Any ** ppException )
212 : {
213 0 : ::rtl::OString sTemp;
214 0 : if ( pMemberType && pMemberType->pTypeName )
215 0 : sTemp = ::rtl::OUStringToOString(pMemberType->pTypeName,RTL_TEXTENCODING_ASCII_US);
216 0 : if ( pre )
217 : {
218 : SAL_INFO("cppu.log", "{ LogBridge () " << sTemp.getStr() );
219 0 : if ( nParams )
220 : {
221 : SAL_INFO("cppu.log", "\n| : ( LogBridge ");
222 0 : for(sal_Int32 i = 0;i < nParams;++i)
223 : {
224 0 : if ( i > 0 )
225 : SAL_INFO("cppu.log", ",");
226 0 : traceValue(pParams[i].pTypeRef,pArgs[i]);
227 :
228 : }
229 : SAL_INFO("cppu.log", ")");
230 : } // if ( nParams )
231 : SAL_INFO("cppu.log", "\n");
232 : }
233 0 : else if ( !pre )
234 : {
235 : SAL_INFO("cppu.log", "} LogBridge () " << sTemp.getStr());
236 0 : if ( ppException && *ppException )
237 : {
238 : SAL_INFO("cppu.log", " exception occurred : ");
239 0 : typelib_TypeDescription * pElementTypeDescr = 0;
240 0 : TYPELIB_DANGER_GET( &pElementTypeDescr, (*ppException)->pType );
241 0 : const ::rtl::OString sValue( ::rtl::OUStringToOString(pElementTypeDescr->pTypeName,osl_getThreadTextEncoding()));
242 : SAL_INFO("cppu.log", "" << sValue.getStr());
243 0 : TYPELIB_DANGER_RELEASE( pElementTypeDescr );
244 : }
245 0 : else if ( pReturnTypeRef )
246 : {
247 : SAL_INFO("cppu.log", " return : ");
248 0 : traceValue(pReturnTypeRef,pReturn);
249 : } // if ( pReturn && pReturnTypeRef )
250 :
251 : SAL_INFO("cppu.log", "\n");
252 0 : }
253 0 : }
254 :
255 : #ifdef DISABLE_DYNLOADING
256 :
257 : #define uno_initEnvironment log_uno_uno_initEnvironment
258 : #define uno_ext_getMapping log_uno_uno_ext_getMapping
259 :
260 : #endif
261 :
262 0 : extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
263 : SAL_THROW_EXTERN_C()
264 : {
265 0 : cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new LogBridge());
266 0 : }
267 :
268 0 : extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
269 : uno_Environment * pFrom,
270 : uno_Environment * pTo )
271 : {
272 0 : cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo,LogProbe);
273 0 : }
274 :
275 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|