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