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 {
ustring2PyUnicode(const OUString & str)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
pyString2ustring(PyObject * pystr)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
getObjectFromUnoModule(const Runtime & runtime,const char * func)87 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
88 {
89 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
90 if( !object.is() )
91 {
92 OUStringBuffer buf;
93 buf.appendAscii( "couldn't find core function " );
94 buf.appendAscii( func );
95 throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
96 }
97 return object;
98 }
99
100
101 //------------------------------------------------------------------------------------
102 // Logging
103 //------------------------------------------------------------------------------------
104
isLog(RuntimeCargo * cargo,sal_Int32 loglevel)105 bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
106 {
107 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
108 }
109
log(RuntimeCargo * cargo,sal_Int32 level,const rtl::OUString & logString)110 void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString )
111 {
112 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
113 }
114
log(RuntimeCargo * cargo,sal_Int32 level,const char * str)115 void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
116 {
117 if( isLog( cargo, level ) )
118 {
119 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
120
121 TimeValue systemTime;
122 TimeValue localTime;
123 oslDateTime localDateTime;
124
125 osl_getSystemTime( &systemTime );
126 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
127 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
128
129 fprintf( cargo->logFile,
130 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
131 localDateTime.Year,
132 localDateTime.Month,
133 localDateTime.Day,
134 localDateTime.Hours,
135 localDateTime.Minutes,
136 localDateTime.Seconds,
137 sal::static_int_cast< unsigned long >(
138 localDateTime.NanoSeconds/1000000),
139 strLevel[level],
140 sal::static_int_cast< long >(
141 (sal_Int32) osl_getThreadIdentifier( 0)),
142 str );
143 }
144 }
145
146 namespace {
147
appendPointer(rtl::OUStringBuffer & buffer,void * pointer)148 void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) {
149 buffer.append(
150 sal::static_int_cast< sal_Int64 >(
151 reinterpret_cast< sal_IntPtr >(pointer)),
152 16);
153 }
154
155 }
156
logException(RuntimeCargo * cargo,const char * intro,void * ptr,const rtl::OUString & aFunctionName,const void * data,const com::sun::star::uno::Type & type)157 void logException( RuntimeCargo *cargo, const char *intro,
158 void * ptr, const rtl::OUString &aFunctionName,
159 const void * data, const com::sun::star::uno::Type & type )
160 {
161 if( isLog( cargo, LogLevel::CALL ) )
162 {
163 rtl::OUStringBuffer buf( 128 );
164 buf.appendAscii( intro );
165 appendPointer(buf, ptr);
166 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
167 buf.append( aFunctionName );
168 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) );
169 buf.append(
170 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
171 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
172 }
173
174 }
175
logReply(RuntimeCargo * cargo,const char * intro,void * ptr,const rtl::OUString & aFunctionName,const Any & returnValue,const Sequence<Any> & aParams)176 void logReply(
177 RuntimeCargo *cargo,
178 const char *intro,
179 void * ptr,
180 const rtl::OUString & aFunctionName,
181 const Any &returnValue,
182 const Sequence< Any > & aParams )
183 {
184 rtl::OUStringBuffer buf( 128 );
185 buf.appendAscii( intro );
186 appendPointer(buf, ptr);
187 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
188 buf.append( aFunctionName );
189 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") );
190 if( isLog( cargo, LogLevel::ARGS ) )
191 {
192 buf.append(
193 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
194 for( int i = 0; i < aParams.getLength() ; i ++ )
195 {
196 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
197 buf.append(
198 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
199 }
200 }
201 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
202
203 }
204
logCall(RuntimeCargo * cargo,const char * intro,void * ptr,const rtl::OUString & aFunctionName,const Sequence<Any> & aParams)205 void logCall( RuntimeCargo *cargo, const char *intro,
206 void * ptr, const rtl::OUString & aFunctionName,
207 const Sequence< Any > & aParams )
208 {
209 rtl::OUStringBuffer buf( 128 );
210 buf.appendAscii( intro );
211 appendPointer(buf, ptr);
212 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
213 buf.append( aFunctionName );
214 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") );
215 if( isLog( cargo, LogLevel::ARGS ) )
216 {
217 for( int i = 0; i < aParams.getLength() ; i ++ )
218 {
219 if( i > 0 )
220 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
221 buf.append(
222 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
223 }
224 }
225 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
226 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
227 }
228
229
230 }
231