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