1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppu.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "osl/mutex.hxx"
32*cdf0e10cSrcweir #include "osl/thread.h"
33*cdf0e10cSrcweir #include "uno/dispatcher.h"
34*cdf0e10cSrcweir #include "typelib/typedescription.hxx"
35*cdf0e10cSrcweir #include "cppu/helper/purpenv/Environment.hxx"
36*cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx"
37*cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
38*cdf0e10cSrcweir #include "rtl/logfile.hxx"
39*cdf0e10cSrcweir #include "uno/environment.hxx"
40*cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx>
41*cdf0e10cSrcweir #include <hash_map>
42*cdf0e10cSrcweir #include <memory>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir class LogBridge : public cppu::Enterable
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	osl::Mutex          m_mutex;
49*cdf0e10cSrcweir 	sal_Int32           m_count;
50*cdf0e10cSrcweir 	oslThreadIdentifier m_threadId;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	virtual  ~LogBridge(void);
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir public:
55*cdf0e10cSrcweir 	explicit LogBridge(void);
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
58*cdf0e10cSrcweir 	virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	virtual void v_enter(void);
61*cdf0e10cSrcweir 	virtual void v_leave(void);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	virtual int  v_isValid(rtl::OUString * pReason);
64*cdf0e10cSrcweir };
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir LogBridge::LogBridge(void)
67*cdf0e10cSrcweir 	: m_count   (0)
68*cdf0e10cSrcweir 	  ,m_threadId(0)
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir LogBridge::~LogBridge(void)
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 	OSL_ASSERT(m_count >= 0);
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir void LogBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir 	enter();
80*cdf0e10cSrcweir     pCallee(pParam);
81*cdf0e10cSrcweir 	leave();
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir void LogBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir 	OSL_ASSERT(m_count > 0);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	-- m_count;
89*cdf0e10cSrcweir 	pCallee(pParam);
90*cdf0e10cSrcweir 	++ m_count;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	if (!m_threadId)
93*cdf0e10cSrcweir 		m_threadId = osl_getThreadIdentifier(NULL);
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void LogBridge::v_enter(void)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	m_mutex.acquire();
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	OSL_ASSERT(m_count >= 0);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	if (m_count == 0)
103*cdf0e10cSrcweir 		m_threadId = osl_getThreadIdentifier(NULL);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	++ m_count;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir void LogBridge::v_leave(void)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	OSL_ASSERT(m_count > 0);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	-- m_count;
113*cdf0e10cSrcweir 	if (!m_count)
114*cdf0e10cSrcweir 		m_threadId = 0;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	m_mutex.release();
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir int LogBridge::v_isValid(rtl::OUString * pReason)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	int result = 1;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	result = m_count > 0;
125*cdf0e10cSrcweir 	if (!result)
126*cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	else
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		result = m_threadId == osl_getThreadIdentifier(NULL);
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		if (!result)
133*cdf0e10cSrcweir 			*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	if (result)
137*cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	return result;
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     void traceValue(typelib_TypeDescriptionReference* _pTypeRef,void* pArg)
143*cdf0e10cSrcweir     {
144*cdf0e10cSrcweir         switch(_pTypeRef->eTypeClass)
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             case typelib_TypeClass_STRING:
147*cdf0e10cSrcweir                 {
148*cdf0e10cSrcweir                     const ::rtl::OString sValue( ::rtl::OUStringToOString(*static_cast< ::rtl::OUString*>(pArg),osl_getThreadTextEncoding()));
149*cdf0e10cSrcweir                     rtl_logfile_trace( "%s", sValue.getStr());
150*cdf0e10cSrcweir                 }
151*cdf0e10cSrcweir                 break;
152*cdf0e10cSrcweir             case typelib_TypeClass_BOOLEAN:
153*cdf0e10cSrcweir                 rtl_logfile_trace( "%d", *static_cast<sal_Bool*>(pArg));
154*cdf0e10cSrcweir                 break;
155*cdf0e10cSrcweir             case typelib_TypeClass_BYTE:
156*cdf0e10cSrcweir                 rtl_logfile_trace( "%d", *static_cast<sal_Int8*>(pArg));
157*cdf0e10cSrcweir                 break;
158*cdf0e10cSrcweir             case typelib_TypeClass_CHAR:
159*cdf0e10cSrcweir                 rtl_logfile_trace( "%c", *static_cast<sal_Char*>(pArg));
160*cdf0e10cSrcweir                 break;
161*cdf0e10cSrcweir             case typelib_TypeClass_SHORT:
162*cdf0e10cSrcweir             case typelib_TypeClass_UNSIGNED_SHORT:
163*cdf0e10cSrcweir                 rtl_logfile_trace( "%d", *static_cast<sal_Int16*>(pArg));
164*cdf0e10cSrcweir                 break;
165*cdf0e10cSrcweir             case typelib_TypeClass_LONG:
166*cdf0e10cSrcweir             case typelib_TypeClass_UNSIGNED_LONG:
167*cdf0e10cSrcweir             case typelib_TypeClass_ENUM:
168*cdf0e10cSrcweir                 rtl_logfile_trace( "%d", *static_cast<sal_Int32*>(pArg));
169*cdf0e10cSrcweir                 break;
170*cdf0e10cSrcweir             case typelib_TypeClass_HYPER:
171*cdf0e10cSrcweir             case typelib_TypeClass_UNSIGNED_HYPER:
172*cdf0e10cSrcweir                 rtl_logfile_trace( "%d", *static_cast<sal_Int64*>(pArg));
173*cdf0e10cSrcweir                 break;
174*cdf0e10cSrcweir             case typelib_TypeClass_FLOAT:
175*cdf0e10cSrcweir                 rtl_logfile_trace( "%f", *static_cast<float*>(pArg));
176*cdf0e10cSrcweir                 break;
177*cdf0e10cSrcweir             case typelib_TypeClass_DOUBLE:
178*cdf0e10cSrcweir                 rtl_logfile_trace( "%f", *static_cast<double*>(pArg));
179*cdf0e10cSrcweir                 break;
180*cdf0e10cSrcweir             case typelib_TypeClass_TYPE:
181*cdf0e10cSrcweir                 {
182*cdf0e10cSrcweir                     const ::rtl::OString sValue( ::rtl::OUStringToOString(((com::sun::star::uno::Type*)pArg)->getTypeName(),osl_getThreadTextEncoding()));
183*cdf0e10cSrcweir                     rtl_logfile_trace( "%s", sValue.getStr());
184*cdf0e10cSrcweir                 }
185*cdf0e10cSrcweir                 break;
186*cdf0e10cSrcweir             case typelib_TypeClass_ANY:
187*cdf0e10cSrcweir                 if ( static_cast<uno_Any*>(pArg)->pData )
188*cdf0e10cSrcweir                     traceValue(static_cast<uno_Any*>(pArg)->pType,static_cast<uno_Any*>(pArg)->pData);
189*cdf0e10cSrcweir                 else
190*cdf0e10cSrcweir                     rtl_logfile_trace( "void");
191*cdf0e10cSrcweir                 break;
192*cdf0e10cSrcweir             case typelib_TypeClass_EXCEPTION:
193*cdf0e10cSrcweir                 rtl_logfile_trace( "exception");
194*cdf0e10cSrcweir                 break;
195*cdf0e10cSrcweir             case typelib_TypeClass_INTERFACE:
196*cdf0e10cSrcweir                 {
197*cdf0e10cSrcweir                     const ::rtl::OString sValue( ::rtl::OUStringToOString(_pTypeRef->pTypeName,osl_getThreadTextEncoding()));
198*cdf0e10cSrcweir                     rtl_logfile_trace( "%s 0x%p", sValue.getStr(),pArg);
199*cdf0e10cSrcweir                 }
200*cdf0e10cSrcweir                 break;
201*cdf0e10cSrcweir             case typelib_TypeClass_VOID:
202*cdf0e10cSrcweir                 rtl_logfile_trace( "void");
203*cdf0e10cSrcweir                 break;
204*cdf0e10cSrcweir             default:
205*cdf0e10cSrcweir                 rtl_logfile_trace( "0x%p", pArg);
206*cdf0e10cSrcweir                 break;
207*cdf0e10cSrcweir         } // switch(pParams[i].pTypeRef->eTypeClass)
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void LogProbe(
212*cdf0e10cSrcweir 	bool                                pre,
213*cdf0e10cSrcweir 	void                              * /*pThis*/,
214*cdf0e10cSrcweir 	void                              * /*pContext*/,
215*cdf0e10cSrcweir 	typelib_TypeDescriptionReference  * pReturnTypeRef,
216*cdf0e10cSrcweir 	typelib_MethodParameter           * pParams,
217*cdf0e10cSrcweir 	sal_Int32                           nParams,
218*cdf0e10cSrcweir 	typelib_TypeDescription     const * pMemberType,
219*cdf0e10cSrcweir 	void                              * pReturn,
220*cdf0e10cSrcweir 	void                              * pArgs[],
221*cdf0e10cSrcweir 	uno_Any                          ** ppException )
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     static ::std::auto_ptr< ::rtl::Logfile> pLogger;
224*cdf0e10cSrcweir     ::rtl::OString sTemp;
225*cdf0e10cSrcweir     if ( pMemberType && pMemberType->pTypeName )
226*cdf0e10cSrcweir         sTemp = ::rtl::OUStringToOString(pMemberType->pTypeName,RTL_TEXTENCODING_ASCII_US);
227*cdf0e10cSrcweir     if ( pre  )
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         rtl_logfile_longTrace( "{ LogBridge () %s", sTemp.getStr() );
230*cdf0e10cSrcweir         if ( nParams )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             rtl_logfile_trace( "\n| : ( LogBridge ");
233*cdf0e10cSrcweir             for(sal_Int32 i = 0;i < nParams;++i)
234*cdf0e10cSrcweir             {
235*cdf0e10cSrcweir                 if ( i > 0 )
236*cdf0e10cSrcweir                     rtl_logfile_trace( ",");
237*cdf0e10cSrcweir                 traceValue(pParams[i].pTypeRef,pArgs[i]);
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir             }
240*cdf0e10cSrcweir             rtl_logfile_trace( ")");
241*cdf0e10cSrcweir         } // if ( nParams )
242*cdf0e10cSrcweir         rtl_logfile_trace( "\n");
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir     else if ( !pre )
245*cdf0e10cSrcweir     {
246*cdf0e10cSrcweir         rtl_logfile_longTrace( "} LogBridge () %s",sTemp.getStr());
247*cdf0e10cSrcweir         if ( ppException && *ppException )
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             rtl_logfile_trace( " excption occured : ");
250*cdf0e10cSrcweir     		typelib_TypeDescription * pElementTypeDescr = 0;
251*cdf0e10cSrcweir             TYPELIB_DANGER_GET( &pElementTypeDescr, (*ppException)->pType );
252*cdf0e10cSrcweir             const ::rtl::OString sValue( ::rtl::OUStringToOString(pElementTypeDescr->pTypeName,osl_getThreadTextEncoding()));
253*cdf0e10cSrcweir             rtl_logfile_trace( "%s", sValue.getStr());
254*cdf0e10cSrcweir             TYPELIB_DANGER_RELEASE( pElementTypeDescr );
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir         else if ( pReturnTypeRef )
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             rtl_logfile_trace( " return : ");
259*cdf0e10cSrcweir             traceValue(pReturnTypeRef,pReturn);
260*cdf0e10cSrcweir         } // if ( pReturn && pReturnTypeRef )
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         rtl_logfile_trace( "\n");
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
267*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir     cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new LogBridge());
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping     ** ppMapping,
273*cdf0e10cSrcweir 								   uno_Environment  * pFrom,
274*cdf0e10cSrcweir 								   uno_Environment  * pTo )
275*cdf0e10cSrcweir {
276*cdf0e10cSrcweir 	cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo,LogProbe);
277*cdf0e10cSrcweir }
278