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