bump product version to 4.1.6.2
[LibreOffice.git] / pyuno / source / module / pyuno_util.cxx
blob1cf1e40b343c859fe4e9f1bc04e4d5eaf3c1080f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "pyuno_impl.hxx"
22 #include <time.h>
23 #include <osl/thread.h>
25 #include <typelib/typedescription.hxx>
27 #include <rtl/strbuf.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <osl/time.h>
31 #include <com/sun/star/beans/XMaterialHolder.hpp>
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;
49 namespace pyuno
51 PyRef ustring2PyUnicode( const OUString & str )
53 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 OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
59 ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
60 #endif
61 return ret;
64 PyRef ustring2PyString( const OUString &str )
66 OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
67 return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE );
70 OUString pyString2ustring( PyObject *pystr )
72 OUString ret;
73 if( PyUnicode_Check( pystr ) )
75 #if Py_UNICODE_SIZE == 2
76 ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
77 #else
78 #if PY_MAJOR_VERSION >= 3
79 Py_ssize_t size(0);
80 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
81 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
89 else
91 #if PY_MAJOR_VERSION >= 3
92 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
93 #else
94 char *name = PyString_AsString(pystr);
95 #endif
96 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
98 return ret;
101 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
102 throw ( RuntimeException )
104 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
105 if( !object.is() )
107 OUStringBuffer buf;
108 buf.appendAscii( "couldn't find core function " );
109 buf.appendAscii( func );
110 throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
112 return object;
116 //------------------------------------------------------------------------------------
117 // Logging
118 //------------------------------------------------------------------------------------
120 bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
122 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
125 void log( RuntimeCargo * cargo, sal_Int32 level, const OUString &logString )
127 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
130 void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
132 if( isLog( cargo, level ) )
134 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
136 TimeValue systemTime;
137 TimeValue localTime;
138 oslDateTime localDateTime;
140 osl_getSystemTime( &systemTime );
141 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
142 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
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 (sal_Int32) osl_getThreadIdentifier( 0)),
157 str );
161 namespace {
163 void appendPointer(OUStringBuffer & buffer, void * pointer) {
164 buffer.append(
165 sal::static_int_cast< sal_Int64 >(
166 reinterpret_cast< sal_IntPtr >(pointer)),
167 16);
172 void logException( RuntimeCargo *cargo, const char *intro,
173 void * ptr, const OUString &aFunctionName,
174 const void * data, const com::sun::star::uno::Type & type )
176 if( isLog( cargo, LogLevel::CALL ) )
178 OUStringBuffer buf( 128 );
179 buf.appendAscii( intro );
180 appendPointer(buf, ptr);
181 buf.append( "]." );
182 buf.append( aFunctionName );
183 buf.append( " = " );
184 buf.append(
185 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
186 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
191 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 )
199 OUStringBuffer buf( 128 );
200 buf.appendAscii( intro );
201 appendPointer(buf, ptr);
202 buf.append( "]." );
203 buf.append( aFunctionName );
204 buf.append( "()=" );
205 if( isLog( cargo, LogLevel::ARGS ) )
207 buf.append(
208 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
209 for( int i = 0; i < aParams.getLength() ; i ++ )
211 buf.append( ", " );
212 buf.append(
213 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
216 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
220 void logCall( RuntimeCargo *cargo, const char *intro,
221 void * ptr, const OUString & aFunctionName,
222 const Sequence< Any > & aParams )
224 OUStringBuffer buf( 128 );
225 buf.appendAscii( intro );
226 appendPointer(buf, ptr);
227 buf.append( "]." );
228 buf.append( aFunctionName );
229 buf.append( "(" );
230 if( isLog( cargo, LogLevel::ARGS ) )
232 for( int i = 0; i < aParams.getLength() ; i ++ )
234 if( i > 0 )
235 buf.append( ", " );
236 buf.append(
237 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
240 buf.append( ")" );
241 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */