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