1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
23 #include <osl/thread.h>
25 #include <typelib/typedescription.hxx>
27 #include <rtl/strbuf.hxx>
28 #include <rtl/ustrbuf.hxx>
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
;
51 PyRef
ustring2PyUnicode( const OUString
& str
)
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
);
58 OString
sUtf8(OUStringToOString(str
, RTL_TEXTENCODING_UTF8
));
59 ret
= PyRef( PyUnicode_DecodeUTF8( sUtf8
.getStr(), sUtf8
.getLength(), NULL
) , SAL_NO_ACQUIRE
);
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
)
73 if( PyUnicode_Check( pystr
) )
75 #if Py_UNICODE_SIZE == 2
76 ret
= OUString( (sal_Unicode
* ) PyUnicode_AS_UNICODE( pystr
) );
78 #if PY_MAJOR_VERSION >= 3
80 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr
, &size
));
81 ret
= OUString(pUtf8
, size
, RTL_TEXTENCODING_UTF8
);
83 PyObject
* pUtf8
= PyUnicode_AsUTF8String(pystr
);
84 ret
= OUString(PyStr_AsString(pUtf8
), PyString_Size(pUtf8
), RTL_TEXTENCODING_UTF8
);
91 #if PY_MAJOR_VERSION >= 3
92 char *name
= PyBytes_AsString(pystr
); // hmmm... is this a good idea?
94 char *name
= PyString_AsString(pystr
);
96 ret
= OUString( name
, strlen(name
), osl_getThreadTextEncoding() );
101 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * func
)
102 throw ( RuntimeException
)
104 PyRef
object(PyDict_GetItemString( runtime
.getImpl()->cargo
->getUnoModule().get(), (char*)func
) );
108 buf
.appendAscii( "couldn't find core function " );
109 buf
.appendAscii( func
);
110 throw RuntimeException(buf
.makeStringAndClear(),Reference
< XInterface
>());
116 //------------------------------------------------------------------------------------
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
;
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",
150 localDateTime
.Minutes
,
151 localDateTime
.Seconds
,
152 sal::static_int_cast
< unsigned long >(
153 localDateTime
.NanoSeconds
/1000000),
155 sal::static_int_cast
< long >(
156 (sal_Int32
) osl_getThreadIdentifier( 0)),
163 void appendPointer(OUStringBuffer
& buffer
, void * pointer
) {
165 sal::static_int_cast
< sal_Int64
>(
166 reinterpret_cast< sal_IntPtr
>(pointer
)),
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
);
182 buf
.append( aFunctionName
);
185 val2str( data
, type
.getTypeLibType(), VAL2STR_MODE_SHALLOW
) );
186 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
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
);
203 buf
.append( aFunctionName
);
205 if( isLog( cargo
, LogLevel::ARGS
) )
208 val2str( returnValue
.getValue(), returnValue
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
209 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
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
);
228 buf
.append( aFunctionName
);
230 if( isLog( cargo
, LogLevel::ARGS
) )
232 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
237 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
241 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */