Branch data 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 : : using rtl::OUStringToOString;
34 : : using rtl::OUString;
35 : : using rtl::OString;
36 : : using rtl::OStringBuffer;
37 : : using rtl::OUStringBuffer;
38 : :
39 : :
40 : : using com::sun::star::uno::TypeDescription;
41 : : using com::sun::star::uno::Sequence;
42 : : using com::sun::star::uno::Reference;
43 : : using com::sun::star::uno::XInterface;
44 : : using com::sun::star::uno::Any;
45 : : using com::sun::star::uno::Type;
46 : : using com::sun::star::uno::UNO_QUERY;
47 : : using com::sun::star::uno::TypeClass;
48 : : using com::sun::star::uno::RuntimeException;
49 : : using com::sun::star::uno::XComponentContext;
50 : : using com::sun::star::lang::XSingleServiceFactory;
51 : : using com::sun::star::script::XTypeConverter;
52 : : using com::sun::star::beans::XMaterialHolder;
53 : :
54 : : namespace pyuno
55 : : {
56 : 45543 : PyRef ustring2PyUnicode( const OUString & str )
57 : : {
58 : 45543 : PyRef ret;
59 : : #if Py_UNICODE_SIZE == 2
60 : : // YD force conversion since python/2 uses wchar_t
61 : : ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
62 : : #else
63 [ + - ]: 45543 : OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
64 [ + - ][ + - ]: 45543 : ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
[ + - ]
65 : : #endif
66 : 45543 : return ret;
67 : : }
68 : :
69 : 9186 : PyRef ustring2PyString( const OUString &str )
70 : : {
71 [ + - ][ + - ]: 9186 : OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
72 [ + - ]: 9186 : return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE );
73 : : }
74 : :
75 : 26565 : OUString pyString2ustring( PyObject *pystr )
76 : : {
77 : 26565 : OUString ret;
78 [ + + ]: 26565 : if( PyUnicode_Check( pystr ) )
79 : : {
80 : : #if Py_UNICODE_SIZE == 2
81 : : ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
82 : : #else
83 [ + - ]: 15199 : PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
84 [ + - ][ + - ]: 15199 : ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
[ + - ]
85 [ + - ][ + + ]: 15199 : Py_DECREF(pUtf8);
86 : : #endif
87 : : }
88 : : else
89 : : {
90 [ + - ]: 11366 : char *name = PyString_AsString(pystr );
91 [ + - ][ + - ]: 11366 : ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
92 : : }
93 : 26565 : return ret;
94 : : }
95 : :
96 : 3360 : PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
97 : : throw ( RuntimeException )
98 : : {
99 [ + - ][ + - ]: 3360 : PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
100 [ - + ]: 3360 : if( !object.is() )
101 : : {
102 : 0 : OUStringBuffer buf;
103 [ # # ]: 0 : buf.appendAscii( "couldn't find core function " );
104 [ # # ]: 0 : buf.appendAscii( func );
105 [ # # ][ # # ]: 0 : throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
106 : : }
107 : 3360 : return object;
108 : : }
109 : :
110 : :
111 : : //------------------------------------------------------------------------------------
112 : : // Logging
113 : : //------------------------------------------------------------------------------------
114 : :
115 : 17668 : bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
116 : : {
117 [ + - ][ - + ]: 17668 : return cargo && cargo->logFile && loglevel <= cargo->logLevel;
[ # # ]
118 : : }
119 : :
120 : 0 : void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString )
121 : : {
122 [ # # ]: 0 : log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
123 : 0 : }
124 : :
125 : 112 : void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
126 : : {
127 [ - + ]: 112 : if( isLog( cargo, level ) )
128 : : {
129 : : static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
130 : :
131 : : TimeValue systemTime;
132 : : TimeValue localTime;
133 : : oslDateTime localDateTime;
134 : :
135 [ # # ]: 0 : osl_getSystemTime( &systemTime );
136 [ # # ]: 0 : osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
137 [ # # ]: 0 : osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
138 : :
139 : : fprintf( cargo->logFile,
140 : : "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
141 : : localDateTime.Year,
142 : : localDateTime.Month,
143 : : localDateTime.Day,
144 : : localDateTime.Hours,
145 : : localDateTime.Minutes,
146 : : localDateTime.Seconds,
147 : : sal::static_int_cast< unsigned long >(
148 : : localDateTime.NanoSeconds/1000000),
149 : : strLevel[level],
150 : : sal::static_int_cast< long >(
151 [ # # ]: 0 : (sal_Int32) osl_getThreadIdentifier( 0)),
152 [ # # ]: 0 : str );
153 : : }
154 : 112 : }
155 : :
156 : : namespace {
157 : :
158 : 0 : void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) {
159 : : buffer.append(
160 : : sal::static_int_cast< sal_Int64 >(
161 : : reinterpret_cast< sal_IntPtr >(pointer)),
162 : 0 : 16);
163 : 0 : }
164 : :
165 : : }
166 : :
167 : 0 : void logException( RuntimeCargo *cargo, const char *intro,
168 : : void * ptr, const rtl::OUString &aFunctionName,
169 : : const void * data, const com::sun::star::uno::Type & type )
170 : : {
171 [ # # ]: 0 : if( isLog( cargo, LogLevel::CALL ) )
172 : : {
173 : 0 : rtl::OUStringBuffer buf( 128 );
174 [ # # ]: 0 : buf.appendAscii( intro );
175 [ # # ]: 0 : appendPointer(buf, ptr);
176 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
177 [ # # ]: 0 : buf.append( aFunctionName );
178 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) );
179 : : buf.append(
180 [ # # ][ # # ]: 0 : val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
181 [ # # ][ # # ]: 0 : log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
182 : : }
183 : :
184 : 0 : }
185 : :
186 : 0 : void logReply(
187 : : RuntimeCargo *cargo,
188 : : const char *intro,
189 : : void * ptr,
190 : : const rtl::OUString & aFunctionName,
191 : : const Any &returnValue,
192 : : const Sequence< Any > & aParams )
193 : : {
194 : 0 : rtl::OUStringBuffer buf( 128 );
195 [ # # ]: 0 : buf.appendAscii( intro );
196 [ # # ]: 0 : appendPointer(buf, ptr);
197 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
198 [ # # ]: 0 : buf.append( aFunctionName );
199 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") );
200 [ # # ]: 0 : if( isLog( cargo, LogLevel::ARGS ) )
201 : : {
202 : : buf.append(
203 [ # # ][ # # ]: 0 : val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
204 [ # # ]: 0 : for( int i = 0; i < aParams.getLength() ; i ++ )
205 : : {
206 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
207 : : buf.append(
208 [ # # ][ # # ]: 0 : val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
209 : : }
210 : : }
211 [ # # ][ # # ]: 0 : log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
212 : :
213 : 0 : }
214 : :
215 : 0 : void logCall( RuntimeCargo *cargo, const char *intro,
216 : : void * ptr, const rtl::OUString & aFunctionName,
217 : : const Sequence< Any > & aParams )
218 : : {
219 : 0 : rtl::OUStringBuffer buf( 128 );
220 [ # # ]: 0 : buf.appendAscii( intro );
221 [ # # ]: 0 : appendPointer(buf, ptr);
222 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
223 [ # # ]: 0 : buf.append( aFunctionName );
224 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") );
225 [ # # ]: 0 : if( isLog( cargo, LogLevel::ARGS ) )
226 : : {
227 [ # # ]: 0 : for( int i = 0; i < aParams.getLength() ; i ++ )
228 : : {
229 [ # # ]: 0 : if( i > 0 )
230 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
231 : : buf.append(
232 [ # # ][ # # ]: 0 : val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
233 : : }
234 : : }
235 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
236 [ # # ][ # # ]: 0 : log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
237 : 0 : }
238 : :
239 : :
240 : : }
241 : :
242 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|