Update ooo320-m1
[ooovba.git] / pyuno / source / module / pyuno_util.cxx
blob343962ec25e4c9ea8ca649ebcc62f038f36bedc6
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: pyuno_util.cxx,v $
10 * $Revision: 1.9 $
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 #include "pyuno_impl.hxx"
33 #include <time.h>
34 #include <osl/thread.h>
36 #include <typelib/typedescription.hxx>
38 #include <rtl/strbuf.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <osl/time.h>
42 #include <com/sun/star/beans/XMaterialHolder.hpp>
44 using rtl::OUStringToOString;
45 using rtl::OUString;
46 using rtl::OString;
47 using rtl::OStringBuffer;
48 using rtl::OUStringBuffer;
51 using com::sun::star::uno::TypeDescription;
52 using com::sun::star::uno::Sequence;
53 using com::sun::star::uno::Reference;
54 using com::sun::star::uno::XInterface;
55 using com::sun::star::uno::Any;
56 using com::sun::star::uno::Type;
57 using com::sun::star::uno::UNO_QUERY;
58 using com::sun::star::uno::TypeClass;
59 using com::sun::star::uno::RuntimeException;
60 using com::sun::star::uno::XComponentContext;
61 using com::sun::star::lang::XSingleServiceFactory;
62 using com::sun::star::script::XTypeConverter;
63 using com::sun::star::beans::XMaterialHolder;
65 #define USTR_ASCII(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
66 namespace pyuno
68 PyRef ustring2PyUnicode( const OUString & str )
70 PyRef ret;
71 #if Py_UNICODE_SIZE == 2
72 // YD force conversion since python/2 uses wchar_t
73 ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
74 #else
75 OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
76 ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
77 #endif
78 return ret;
81 PyRef ustring2PyString( const OUString &str )
83 OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
84 return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE );
87 OUString pyString2ustring( PyObject *pystr )
89 OUString ret;
90 if( PyUnicode_Check( pystr ) )
92 #if Py_UNICODE_SIZE == 2
93 ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
94 #else
95 PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
96 ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
97 Py_DECREF(pUtf8);
98 #endif
100 else
102 char *name = PyString_AsString(pystr );
103 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
105 return ret;
108 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
109 throw ( RuntimeException )
111 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
112 if( !object.is() )
114 OUStringBuffer buf;
115 buf.appendAscii( "couldn't find core function " );
116 buf.appendAscii( func );
117 throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
119 return object;
123 //------------------------------------------------------------------------------------
124 // Logging
125 //------------------------------------------------------------------------------------
127 bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
129 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
132 void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString )
134 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
137 void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
139 if( isLog( cargo, level ) )
141 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
143 TimeValue systemTime;
144 TimeValue localTime;
145 oslDateTime localDateTime;
147 osl_getSystemTime( &systemTime );
148 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
149 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
151 fprintf( cargo->logFile,
152 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
153 localDateTime.Year,
154 localDateTime.Month,
155 localDateTime.Day,
156 localDateTime.Hours,
157 localDateTime.Minutes,
158 localDateTime.Seconds,
159 sal::static_int_cast< unsigned long >(
160 localDateTime.NanoSeconds/1000000),
161 strLevel[level],
162 sal::static_int_cast< long >(
163 (sal_Int32) osl_getThreadIdentifier( 0)),
164 str );
168 namespace {
170 void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) {
171 buffer.append(
172 sal::static_int_cast< sal_Int64 >(
173 reinterpret_cast< sal_IntPtr >(pointer)),
174 16);
179 void logException( RuntimeCargo *cargo, const char *intro,
180 void * ptr, const rtl::OUString &aFunctionName,
181 const void * data, const com::sun::star::uno::Type & type )
183 if( isLog( cargo, LogLevel::CALL ) )
185 rtl::OUStringBuffer buf( 128 );
186 buf.appendAscii( intro );
187 appendPointer(buf, ptr);
188 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
189 buf.append( aFunctionName );
190 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) );
191 buf.append(
192 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
193 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
198 void logReply(
199 RuntimeCargo *cargo,
200 const char *intro,
201 void * ptr,
202 const rtl::OUString & aFunctionName,
203 const Any &returnValue,
204 const Sequence< Any > & aParams )
206 rtl::OUStringBuffer buf( 128 );
207 buf.appendAscii( intro );
208 appendPointer(buf, ptr);
209 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
210 buf.append( aFunctionName );
211 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") );
212 if( isLog( cargo, LogLevel::ARGS ) )
214 buf.append(
215 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
216 for( int i = 0; i < aParams.getLength() ; i ++ )
218 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
219 buf.append(
220 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
223 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
227 void logCall( RuntimeCargo *cargo, const char *intro,
228 void * ptr, const rtl::OUString & aFunctionName,
229 const Sequence< Any > & aParams )
231 rtl::OUStringBuffer buf( 128 );
232 buf.appendAscii( intro );
233 appendPointer(buf, ptr);
234 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
235 buf.append( aFunctionName );
236 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") );
237 if( isLog( cargo, LogLevel::ARGS ) )
239 for( int i = 0; i < aParams.getLength() ; i ++ )
241 if( i > 0 )
242 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
243 buf.append(
244 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
247 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
248 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );