1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "pyuno_impl.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <time.h> 31*cdf0e10cSrcweir #include <osl/thread.h> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <typelib/typedescription.hxx> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 37*cdf0e10cSrcweir #include <osl/time.h> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <com/sun/star/beans/XMaterialHolder.hpp> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir using rtl::OUStringToOString; 42*cdf0e10cSrcweir using rtl::OUString; 43*cdf0e10cSrcweir using rtl::OString; 44*cdf0e10cSrcweir using rtl::OStringBuffer; 45*cdf0e10cSrcweir using rtl::OUStringBuffer; 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using com::sun::star::uno::TypeDescription; 49*cdf0e10cSrcweir using com::sun::star::uno::Sequence; 50*cdf0e10cSrcweir using com::sun::star::uno::Reference; 51*cdf0e10cSrcweir using com::sun::star::uno::XInterface; 52*cdf0e10cSrcweir using com::sun::star::uno::Any; 53*cdf0e10cSrcweir using com::sun::star::uno::Type; 54*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY; 55*cdf0e10cSrcweir using com::sun::star::uno::TypeClass; 56*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException; 57*cdf0e10cSrcweir using com::sun::star::uno::XComponentContext; 58*cdf0e10cSrcweir using com::sun::star::lang::XSingleServiceFactory; 59*cdf0e10cSrcweir using com::sun::star::script::XTypeConverter; 60*cdf0e10cSrcweir using com::sun::star::beans::XMaterialHolder; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir #define USTR_ASCII(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) ) 63*cdf0e10cSrcweir namespace pyuno 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir PyRef ustring2PyUnicode( const OUString & str ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir PyRef ret; 68*cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2 69*cdf0e10cSrcweir // YD force conversion since python/2 uses wchar_t 70*cdf0e10cSrcweir ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE ); 71*cdf0e10cSrcweir #else 72*cdf0e10cSrcweir OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8)); 73*cdf0e10cSrcweir ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE ); 74*cdf0e10cSrcweir #endif 75*cdf0e10cSrcweir return ret; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir PyRef ustring2PyString( const OUString &str ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir OString o = OUStringToOString( str, osl_getThreadTextEncoding() ); 81*cdf0e10cSrcweir return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE ); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir OUString pyString2ustring( PyObject *pystr ) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir OUString ret; 87*cdf0e10cSrcweir if( PyUnicode_Check( pystr ) ) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir #if Py_UNICODE_SIZE == 2 90*cdf0e10cSrcweir ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) ); 91*cdf0e10cSrcweir #else 92*cdf0e10cSrcweir PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr); 93*cdf0e10cSrcweir ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8); 94*cdf0e10cSrcweir Py_DECREF(pUtf8); 95*cdf0e10cSrcweir #endif 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir else 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir char *name = PyString_AsString(pystr ); 100*cdf0e10cSrcweir ret = OUString( name, strlen(name), osl_getThreadTextEncoding() ); 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir return ret; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func ) 106*cdf0e10cSrcweir throw ( RuntimeException ) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) ); 109*cdf0e10cSrcweir if( !object.is() ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir OUStringBuffer buf; 112*cdf0e10cSrcweir buf.appendAscii( "couldn't find core function " ); 113*cdf0e10cSrcweir buf.appendAscii( func ); 114*cdf0e10cSrcweir throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >()); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir return object; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir //------------------------------------------------------------------------------------ 121*cdf0e10cSrcweir // Logging 122*cdf0e10cSrcweir //------------------------------------------------------------------------------------ 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir return cargo && cargo->logFile && loglevel <= cargo->logLevel; 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() ); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir void log( RuntimeCargo * cargo, sal_Int32 level, const char *str ) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir if( isLog( cargo, level ) ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir static const char *strLevel[] = { "NONE", "CALL", "ARGS" }; 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir TimeValue systemTime; 141*cdf0e10cSrcweir TimeValue localTime; 142*cdf0e10cSrcweir oslDateTime localDateTime; 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir osl_getSystemTime( &systemTime ); 145*cdf0e10cSrcweir osl_getLocalTimeFromSystemTime( &systemTime, &localTime ); 146*cdf0e10cSrcweir osl_getDateTimeFromTimeValue( &localTime, &localDateTime ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir fprintf( cargo->logFile, 149*cdf0e10cSrcweir "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n", 150*cdf0e10cSrcweir localDateTime.Year, 151*cdf0e10cSrcweir localDateTime.Month, 152*cdf0e10cSrcweir localDateTime.Day, 153*cdf0e10cSrcweir localDateTime.Hours, 154*cdf0e10cSrcweir localDateTime.Minutes, 155*cdf0e10cSrcweir localDateTime.Seconds, 156*cdf0e10cSrcweir sal::static_int_cast< unsigned long >( 157*cdf0e10cSrcweir localDateTime.NanoSeconds/1000000), 158*cdf0e10cSrcweir strLevel[level], 159*cdf0e10cSrcweir sal::static_int_cast< long >( 160*cdf0e10cSrcweir (sal_Int32) osl_getThreadIdentifier( 0)), 161*cdf0e10cSrcweir str ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir namespace { 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) { 168*cdf0e10cSrcweir buffer.append( 169*cdf0e10cSrcweir sal::static_int_cast< sal_Int64 >( 170*cdf0e10cSrcweir reinterpret_cast< sal_IntPtr >(pointer)), 171*cdf0e10cSrcweir 16); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir void logException( RuntimeCargo *cargo, const char *intro, 177*cdf0e10cSrcweir void * ptr, const rtl::OUString &aFunctionName, 178*cdf0e10cSrcweir const void * data, const com::sun::star::uno::Type & type ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir if( isLog( cargo, LogLevel::CALL ) ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 183*cdf0e10cSrcweir buf.appendAscii( intro ); 184*cdf0e10cSrcweir appendPointer(buf, ptr); 185*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 186*cdf0e10cSrcweir buf.append( aFunctionName ); 187*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) ); 188*cdf0e10cSrcweir buf.append( 189*cdf0e10cSrcweir val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) ); 190*cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir void logReply( 196*cdf0e10cSrcweir RuntimeCargo *cargo, 197*cdf0e10cSrcweir const char *intro, 198*cdf0e10cSrcweir void * ptr, 199*cdf0e10cSrcweir const rtl::OUString & aFunctionName, 200*cdf0e10cSrcweir const Any &returnValue, 201*cdf0e10cSrcweir const Sequence< Any > & aParams ) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 204*cdf0e10cSrcweir buf.appendAscii( intro ); 205*cdf0e10cSrcweir appendPointer(buf, ptr); 206*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 207*cdf0e10cSrcweir buf.append( aFunctionName ); 208*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") ); 209*cdf0e10cSrcweir if( isLog( cargo, LogLevel::ARGS ) ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir buf.append( 212*cdf0e10cSrcweir val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 213*cdf0e10cSrcweir for( int i = 0; i < aParams.getLength() ; i ++ ) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) ); 216*cdf0e10cSrcweir buf.append( 217*cdf0e10cSrcweir val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir void logCall( RuntimeCargo *cargo, const char *intro, 225*cdf0e10cSrcweir void * ptr, const rtl::OUString & aFunctionName, 226*cdf0e10cSrcweir const Sequence< Any > & aParams ) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir rtl::OUStringBuffer buf( 128 ); 229*cdf0e10cSrcweir buf.appendAscii( intro ); 230*cdf0e10cSrcweir appendPointer(buf, ptr); 231*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") ); 232*cdf0e10cSrcweir buf.append( aFunctionName ); 233*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") ); 234*cdf0e10cSrcweir if( isLog( cargo, LogLevel::ARGS ) ) 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir for( int i = 0; i < aParams.getLength() ; i ++ ) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir if( i > 0 ) 239*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) ); 240*cdf0e10cSrcweir buf.append( 241*cdf0e10cSrcweir val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) ); 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") ); 245*cdf0e10cSrcweir log( cargo,LogLevel::CALL, buf.makeStringAndClear() ); 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir } 250