Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / pyuno / source / module / pyuno_util.cxx
bloba8da336c7314d52d5097ce6e7dbd884ef2e02b57
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>
24 #include <osl/thread.hxx>
26 #include <typelib/typedescription.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/time.h>
32 #include <com/sun/star/beans/XMaterialHolder.hpp>
36 using com::sun::star::uno::TypeDescription;
37 using com::sun::star::uno::Sequence;
38 using com::sun::star::uno::Reference;
39 using com::sun::star::uno::XInterface;
40 using com::sun::star::uno::Any;
41 using com::sun::star::uno::Type;
42 using com::sun::star::uno::UNO_QUERY;
43 using com::sun::star::uno::TypeClass;
44 using com::sun::star::uno::RuntimeException;
45 using com::sun::star::uno::XComponentContext;
46 using com::sun::star::lang::XSingleServiceFactory;
47 using com::sun::star::script::XTypeConverter;
48 using com::sun::star::beans::XMaterialHolder;
50 namespace pyuno
52 PyRef ustring2PyUnicode( const OUString & str )
54 PyRef ret;
55 #if Py_UNICODE_SIZE == 2
56 // YD force conversion since python/2 uses wchar_t
57 ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
58 #else
59 OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
60 ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
61 #endif
62 return ret;
65 PyRef ustring2PyString( const OUString &str )
67 OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
68 return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE );
71 OUString pyString2ustring( PyObject *pystr )
73 OUString ret;
74 if( PyUnicode_Check( pystr ) )
76 #if Py_UNICODE_SIZE == 2
77 ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
78 #else
79 #if PY_MAJOR_VERSION >= 3
80 Py_ssize_t size(0);
81 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
82 ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8);
83 #else
84 PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
85 ret = OUString(PyStr_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
86 Py_DECREF(pUtf8);
87 #endif
88 #endif
90 else
92 #if PY_MAJOR_VERSION >= 3
93 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
94 #else
95 char *name = PyString_AsString(pystr);
96 #endif
97 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
99 return ret;
102 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
103 throw ( RuntimeException )
105 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
106 if( !object.is() )
108 OUStringBuffer buf;
109 buf.appendAscii( "couldn't find core function " );
110 buf.appendAscii( func );
111 throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
113 return object;
117 //------------------------------------------------------------------------------------
118 // Logging
119 //------------------------------------------------------------------------------------
121 bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
123 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
126 void log( RuntimeCargo * cargo, sal_Int32 level, const OUString &logString )
128 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
131 void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
133 if( isLog( cargo, level ) )
135 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
137 TimeValue systemTime;
138 TimeValue localTime;
139 oslDateTime localDateTime;
141 osl_getSystemTime( &systemTime );
142 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
143 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
145 fprintf( cargo->logFile,
146 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
147 localDateTime.Year,
148 localDateTime.Month,
149 localDateTime.Day,
150 localDateTime.Hours,
151 localDateTime.Minutes,
152 localDateTime.Seconds,
153 sal::static_int_cast< unsigned long >(
154 localDateTime.NanoSeconds/1000000),
155 strLevel[level],
156 sal::static_int_cast< long >(
157 (sal_Int32) osl::Thread::getCurrentIdentifier()),
158 str );
162 namespace {
164 void appendPointer(OUStringBuffer & buffer, void * pointer) {
165 buffer.append(
166 sal::static_int_cast< sal_Int64 >(
167 reinterpret_cast< sal_IntPtr >(pointer)),
168 16);
173 void logException( RuntimeCargo *cargo, const char *intro,
174 void * ptr, const OUString &aFunctionName,
175 const void * data, const com::sun::star::uno::Type & type )
177 if( isLog( cargo, LogLevel::CALL ) )
179 OUStringBuffer buf( 128 );
180 buf.appendAscii( intro );
181 appendPointer(buf, ptr);
182 buf.append( "]." );
183 buf.append( aFunctionName );
184 buf.append( " = " );
185 buf.append(
186 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
187 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
192 void logReply(
193 RuntimeCargo *cargo,
194 const char *intro,
195 void * ptr,
196 const OUString & aFunctionName,
197 const Any &returnValue,
198 const Sequence< Any > & aParams )
200 OUStringBuffer buf( 128 );
201 buf.appendAscii( intro );
202 appendPointer(buf, ptr);
203 buf.append( "]." );
204 buf.append( aFunctionName );
205 buf.append( "()=" );
206 if( isLog( cargo, LogLevel::ARGS ) )
208 buf.append(
209 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
210 for( int i = 0; i < aParams.getLength() ; i ++ )
212 buf.append( ", " );
213 buf.append(
214 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
217 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
221 void logCall( RuntimeCargo *cargo, const char *intro,
222 void * ptr, const OUString & aFunctionName,
223 const Sequence< Any > & aParams )
225 OUStringBuffer buf( 128 );
226 buf.appendAscii( intro );
227 appendPointer(buf, ptr);
228 buf.append( "]." );
229 buf.append( aFunctionName );
230 buf.append( "(" );
231 if( isLog( cargo, LogLevel::ARGS ) )
233 for( int i = 0; i < aParams.getLength() ; i ++ )
235 if( i > 0 )
236 buf.append( ", " );
237 buf.append(
238 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
241 buf.append( ")" );
242 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */