167c7d1c1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 367c7d1c1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 467c7d1c1SAndrew Rist * or more contributor license agreements. See the NOTICE file 567c7d1c1SAndrew Rist * distributed with this work for additional information 667c7d1c1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 767c7d1c1SAndrew Rist * to you under the Apache License, Version 2.0 (the 867c7d1c1SAndrew Rist * "License"); you may not use this file except in compliance 967c7d1c1SAndrew Rist * with the License. You may obtain a copy of the License at 1067c7d1c1SAndrew Rist * 1167c7d1c1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1267c7d1c1SAndrew Rist * 1367c7d1c1SAndrew Rist * Unless required by applicable law or agreed to in writing, 1467c7d1c1SAndrew Rist * software distributed under the License is distributed on an 1567c7d1c1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1667c7d1c1SAndrew Rist * KIND, either express or implied. See the License for the 1767c7d1c1SAndrew Rist * specific language governing permissions and limitations 1867c7d1c1SAndrew Rist * under the License. 1967c7d1c1SAndrew Rist * 2067c7d1c1SAndrew Rist *************************************************************/ 2167c7d1c1SAndrew Rist 2267c7d1c1SAndrew 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; 64*77dc4149SPedro Giffuni 65cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2 66cdf0e10cSrcweir // YD force conversion since python/2 uses wchar_t 67cdf0e10cSrcweir ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE ); 68cdf0e10cSrcweir #else 69cdf0e10cSrcweir OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8)); 70cdf0e10cSrcweir ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE ); 71cdf0e10cSrcweir #endif 72cdf0e10cSrcweir return ret; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir PyRef ustring2PyString( const OUString &str ) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir OString o = OUStringToOString( str, osl_getThreadTextEncoding() ); 78564d9007SPedro Giffuni return PyRef( PyBytes_FromString( o.getStr() ), SAL_NO_ACQUIRE ); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir OUString pyString2ustring( PyObject *pystr ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir OUString ret; 84cdf0e10cSrcweir if( PyUnicode_Check( pystr ) ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2 87cdf0e10cSrcweir ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) ); 88*77dc4149SPedro Giffuni #else 89*77dc4149SPedro Giffuni #if PY_VERSION_HEX >= 0x03030000 90*77dc4149SPedro Giffuni Py_ssize_t size; 91*77dc4149SPedro Giffuni char *pUtf8 = PyUnicode_AsUTF8AndSize(pystr, &size); 92*77dc4149SPedro Giffuni ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8); 93cdf0e10cSrcweir #else 94cdf0e10cSrcweir PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr); 95564d9007SPedro Giffuni ret = OUString(PyBytes_AsString(pUtf8), PyBytes_Size(pUtf8), RTL_TEXTENCODING_UTF8); 96cdf0e10cSrcweir Py_DECREF(pUtf8); 97*77dc4149SPedro Giffuni #endif 98cdf0e10cSrcweir #endif 99cdf0e10cSrcweir } 100cdf0e10cSrcweir else 101cdf0e10cSrcweir { 102564d9007SPedro Giffuni char *name = PyBytes_AsString(pystr ); 103cdf0e10cSrcweir ret = OUString( name, strlen(name), osl_getThreadTextEncoding() ); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir return ret; 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func ) 109cdf0e10cSrcweir throw ( RuntimeException ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) ); 112cdf0e10cSrcweir if( !object.is() ) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir OUStringBuffer buf; 115cdf0e10cSrcweir buf.appendAscii( "couldn't find core function " ); 116cdf0e10cSrcweir buf.appendAscii( func ); 117cdf0e10cSrcweir throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >()); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir return object; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir 123cdf0e10cSrcweir //------------------------------------------------------------------------------------ 124cdf0e10cSrcweir // Logging 125cdf0e10cSrcweir //------------------------------------------------------------------------------------ 126cdf0e10cSrcweir 127cdf0e10cSrcweir bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir return cargo && cargo->logFile && loglevel <= cargo->logLevel; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() ); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const char *str ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir if( isLog( cargo, level ) ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir static const char *strLevel[] = { "NONE", "CALL", "ARGS" }; 142cdf0e10cSrcweir 143cdf0e10cSrcweir TimeValue systemTime; 144cdf0e10cSrcweir TimeValue localTime; 145cdf0e10cSrcweir oslDateTime localDateTime; 146cdf0e10cSrcweir 147cdf0e10cSrcweir osl_getSystemTime( &systemTime ); 148cdf0e10cSrcweir osl_getLocalTimeFromSystemTime( &systemTime, &localTime ); 149cdf0e10cSrcweir osl_getDateTimeFromTimeValue( &localTime, &localDateTime ); 150cdf0e10cSrcweir 151cdf0e10cSrcweir fprintf( cargo->logFile, 152cdf0e10cSrcweir "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n", 153cdf0e10cSrcweir localDateTime.Year, 154cdf0e10cSrcweir localDateTime.Month, 155cdf0e10cSrcweir localDateTime.Day, 156cdf0e10cSrcweir localDateTime.Hours, 157cdf0e10cSrcweir localDateTime.Minutes, 158cdf0e10cSrcweir localDateTime.Seconds, 159cdf0e10cSrcweir sal::static_int_cast< unsigned long >( 160cdf0e10cSrcweir localDateTime.NanoSeconds/1000000), 161cdf0e10cSrcweir strLevel[level], 162cdf0e10cSrcweir sal::static_int_cast< long >( 163cdf0e10cSrcweir (sal_Int32) osl_getThreadIdentifier( 0)), 164cdf0e10cSrcweir str ); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir namespace { 169cdf0e10cSrcweir 170cdf0e10cSrcweir void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) { 171cdf0e10cSrcweir buffer.append( 172cdf0e10cSrcweir sal::static_int_cast< sal_Int64 >( 173cdf0e10cSrcweir reinterpret_cast< sal_IntPtr >(pointer)), 174cdf0e10cSrcweir 16); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro, 180cdf0e10cSrcweir void * ptr, const rtl::OUString &aFunctionName, 181cdf0e10cSrcweir const void * data, const com::sun::star::uno::Type & type ) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 186cdf0e10cSrcweir buf.appendAscii( intro ); 187cdf0e10cSrcweir appendPointer(buf, ptr); 188cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 189cdf0e10cSrcweir buf.append( aFunctionName ); 190cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) ); 191cdf0e10cSrcweir buf.append( 192cdf0e10cSrcweir val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) ); 193cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir void logReply( 199cdf0e10cSrcweir RuntimeCargo *cargo, 200cdf0e10cSrcweir const char *intro, 201cdf0e10cSrcweir void * ptr, 202cdf0e10cSrcweir const rtl::OUString & aFunctionName, 203cdf0e10cSrcweir const Any &returnValue, 204cdf0e10cSrcweir const Sequence< Any > & aParams ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 207cdf0e10cSrcweir buf.appendAscii( intro ); 208cdf0e10cSrcweir appendPointer(buf, ptr); 209cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 210cdf0e10cSrcweir buf.append( aFunctionName ); 211cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") ); 212cdf0e10cSrcweir if( isLog( cargo, LogLevel::ARGS ) ) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir buf.append( 215cdf0e10cSrcweir val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 216cdf0e10cSrcweir for( int i = 0; i < aParams.getLength() ; i ++ ) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) ); 219cdf0e10cSrcweir buf.append( 220cdf0e10cSrcweir val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 221cdf0e10cSrcweir } 222cdf0e10cSrcweir } 223cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 224cdf0e10cSrcweir 225cdf0e10cSrcweir } 226cdf0e10cSrcweir 227cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro, 228cdf0e10cSrcweir void * ptr, const rtl::OUString & aFunctionName, 229cdf0e10cSrcweir const Sequence< Any > & aParams ) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 232cdf0e10cSrcweir buf.appendAscii( intro ); 233cdf0e10cSrcweir appendPointer(buf, ptr); 234cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 235cdf0e10cSrcweir buf.append( aFunctionName ); 236cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") ); 237cdf0e10cSrcweir if( isLog( cargo, LogLevel::ARGS ) ) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir for( int i = 0; i < aParams.getLength() ; i ++ ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir if( i > 0 ) 242cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) ); 243cdf0e10cSrcweir buf.append( 244cdf0e10cSrcweir val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 245cdf0e10cSrcweir } 246cdf0e10cSrcweir } 247cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") ); 248cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir 252cdf0e10cSrcweir } 253