xref: /trunk/main/pyuno/source/module/pyuno_util.cxx (revision 67c7d1c1)
1*67c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*67c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*67c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*67c7d1c1SAndrew Rist  * distributed with this work for additional information
6*67c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*67c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*67c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
9*67c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*67c7d1c1SAndrew Rist  *
11*67c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*67c7d1c1SAndrew Rist  *
13*67c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*67c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
15*67c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*67c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*67c7d1c1SAndrew Rist  * specific language governing permissions and limitations
18*67c7d1c1SAndrew Rist  * under the License.
19*67c7d1c1SAndrew Rist  *
20*67c7d1c1SAndrew Rist  *************************************************************/
21*67c7d1c1SAndrew Rist 
22*67c7d1c1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "pyuno_impl.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <time.h>
27cdf0e10cSrcweir #include <osl/thread.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <typelib/typedescription.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/strbuf.hxx>
32cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33cdf0e10cSrcweir #include <osl/time.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using rtl::OUStringToOString;
38cdf0e10cSrcweir using rtl::OUString;
39cdf0e10cSrcweir using rtl::OString;
40cdf0e10cSrcweir using rtl::OStringBuffer;
41cdf0e10cSrcweir using rtl::OUStringBuffer;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using com::sun::star::uno::TypeDescription;
45cdf0e10cSrcweir using com::sun::star::uno::Sequence;
46cdf0e10cSrcweir using com::sun::star::uno::Reference;
47cdf0e10cSrcweir using com::sun::star::uno::XInterface;
48cdf0e10cSrcweir using com::sun::star::uno::Any;
49cdf0e10cSrcweir using com::sun::star::uno::Type;
50cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
51cdf0e10cSrcweir using com::sun::star::uno::TypeClass;
52cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
53cdf0e10cSrcweir using com::sun::star::uno::XComponentContext;
54cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory;
55cdf0e10cSrcweir using com::sun::star::script::XTypeConverter;
56cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir #define USTR_ASCII(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
59cdf0e10cSrcweir namespace pyuno
60cdf0e10cSrcweir {
61cdf0e10cSrcweir PyRef ustring2PyUnicode( const OUString & str )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     PyRef ret;
64cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2
65cdf0e10cSrcweir     // YD force conversion since python/2 uses wchar_t
66cdf0e10cSrcweir     ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
67cdf0e10cSrcweir #else
68cdf0e10cSrcweir     OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
69cdf0e10cSrcweir     ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir     return ret;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir PyRef ustring2PyString( const OUString &str )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
77cdf0e10cSrcweir     return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir OUString pyString2ustring( PyObject *pystr )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     OUString ret;
83cdf0e10cSrcweir     if( PyUnicode_Check( pystr ) )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2
86cdf0e10cSrcweir 	ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
87cdf0e10cSrcweir #else
88cdf0e10cSrcweir 	PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
89cdf0e10cSrcweir 	ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
90cdf0e10cSrcweir 	Py_DECREF(pUtf8);
91cdf0e10cSrcweir #endif
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir     else
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         char *name = PyString_AsString(pystr );
96cdf0e10cSrcweir         ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir     return ret;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
102cdf0e10cSrcweir     throw ( RuntimeException )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
105cdf0e10cSrcweir     if( !object.is() )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         OUStringBuffer buf;
108cdf0e10cSrcweir         buf.appendAscii( "couldn't find core function " );
109cdf0e10cSrcweir         buf.appendAscii( func );
110cdf0e10cSrcweir         throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     return object;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //------------------------------------------------------------------------------------
117cdf0e10cSrcweir // Logging
118cdf0e10cSrcweir //------------------------------------------------------------------------------------
119cdf0e10cSrcweir 
120cdf0e10cSrcweir bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     return cargo && cargo->logFile && loglevel <= cargo->logLevel;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     if( isLog( cargo, level ) )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         TimeValue systemTime;
137cdf0e10cSrcweir         TimeValue localTime;
138cdf0e10cSrcweir         oslDateTime localDateTime;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         osl_getSystemTime( &systemTime );
141cdf0e10cSrcweir         osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
142cdf0e10cSrcweir         osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         fprintf( cargo->logFile,
145cdf0e10cSrcweir                  "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
146cdf0e10cSrcweir                  localDateTime.Year,
147cdf0e10cSrcweir                  localDateTime.Month,
148cdf0e10cSrcweir                  localDateTime.Day,
149cdf0e10cSrcweir                  localDateTime.Hours,
150cdf0e10cSrcweir                  localDateTime.Minutes,
151cdf0e10cSrcweir                  localDateTime.Seconds,
152cdf0e10cSrcweir                  sal::static_int_cast< unsigned long >(
153cdf0e10cSrcweir                      localDateTime.NanoSeconds/1000000),
154cdf0e10cSrcweir                  strLevel[level],
155cdf0e10cSrcweir                  sal::static_int_cast< long >(
156cdf0e10cSrcweir                      (sal_Int32) osl_getThreadIdentifier( 0)),
157cdf0e10cSrcweir                  str );
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir namespace {
162cdf0e10cSrcweir 
163cdf0e10cSrcweir void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) {
164cdf0e10cSrcweir     buffer.append(
165cdf0e10cSrcweir         sal::static_int_cast< sal_Int64 >(
166cdf0e10cSrcweir             reinterpret_cast< sal_IntPtr >(pointer)),
167cdf0e10cSrcweir         16);
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro,
173cdf0e10cSrcweir                    void * ptr, const rtl::OUString &aFunctionName,
174cdf0e10cSrcweir                    const void * data, const com::sun::star::uno::Type & type )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     if( isLog( cargo, LogLevel::CALL ) )
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         rtl::OUStringBuffer buf( 128 );
179cdf0e10cSrcweir         buf.appendAscii( intro );
180cdf0e10cSrcweir         appendPointer(buf, ptr);
181cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
182cdf0e10cSrcweir         buf.append( aFunctionName );
183cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) );
184cdf0e10cSrcweir         buf.append(
185cdf0e10cSrcweir             val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
186cdf0e10cSrcweir         log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir void logReply(
192cdf0e10cSrcweir     RuntimeCargo *cargo,
193cdf0e10cSrcweir     const char *intro,
194cdf0e10cSrcweir     void * ptr,
195cdf0e10cSrcweir     const rtl::OUString & aFunctionName,
196cdf0e10cSrcweir     const Any &returnValue,
197cdf0e10cSrcweir     const Sequence< Any > & aParams )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     rtl::OUStringBuffer buf( 128 );
200cdf0e10cSrcweir     buf.appendAscii( intro );
201cdf0e10cSrcweir     appendPointer(buf, ptr);
202cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
203cdf0e10cSrcweir     buf.append( aFunctionName );
204cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") );
205cdf0e10cSrcweir     if( isLog( cargo, LogLevel::ARGS ) )
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         buf.append(
208cdf0e10cSrcweir             val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
209cdf0e10cSrcweir         for( int i = 0; i < aParams.getLength() ; i ++ )
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
212cdf0e10cSrcweir             buf.append(
213cdf0e10cSrcweir                 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir     log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro,
221cdf0e10cSrcweir               void * ptr, const rtl::OUString & aFunctionName,
222cdf0e10cSrcweir               const Sequence< Any > & aParams )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     rtl::OUStringBuffer buf( 128 );
225cdf0e10cSrcweir     buf.appendAscii( intro );
226cdf0e10cSrcweir     appendPointer(buf, ptr);
227cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
228cdf0e10cSrcweir     buf.append( aFunctionName );
229cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") );
230cdf0e10cSrcweir     if( isLog( cargo, LogLevel::ARGS ) )
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         for( int i = 0; i < aParams.getLength() ; i ++ )
233cdf0e10cSrcweir         {
234cdf0e10cSrcweir             if( i > 0 )
235cdf0e10cSrcweir                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
236cdf0e10cSrcweir             buf.append(
237cdf0e10cSrcweir                 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
238cdf0e10cSrcweir         }
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
241cdf0e10cSrcweir     log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245cdf0e10cSrcweir }
246