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