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>
24 #include <osl/thread.hxx>
26 #include <typelib/typedescription.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/ustrbuf.hxx>
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
;
52 PyRef
ustring2PyUnicode( const OUString
& str
)
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
);
59 OString
sUtf8(OUStringToOString(str
, RTL_TEXTENCODING_UTF8
));
60 ret
= PyRef( PyUnicode_DecodeUTF8( sUtf8
.getStr(), sUtf8
.getLength(), NULL
) , SAL_NO_ACQUIRE
);
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
)
74 if( PyUnicode_Check( pystr
) )
76 #if Py_UNICODE_SIZE == 2
77 ret
= OUString( (sal_Unicode
* ) PyUnicode_AS_UNICODE( pystr
) );
79 #if PY_MAJOR_VERSION >= 3
81 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr
, &size
));
82 ret
= OUString(pUtf8
, size
, RTL_TEXTENCODING_UTF8
);
84 PyObject
* pUtf8
= PyUnicode_AsUTF8String(pystr
);
85 ret
= OUString(PyStr_AsString(pUtf8
), PyString_Size(pUtf8
), RTL_TEXTENCODING_UTF8
);
92 #if PY_MAJOR_VERSION >= 3
93 char *name
= PyBytes_AsString(pystr
); // hmmm... is this a good idea?
95 char *name
= PyString_AsString(pystr
);
97 ret
= OUString( name
, strlen(name
), osl_getThreadTextEncoding() );
102 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * func
)
103 throw ( RuntimeException
)
105 PyRef
object(PyDict_GetItemString( runtime
.getImpl()->cargo
->getUnoModule().get(), (char*)func
) );
109 buf
.appendAscii( "couldn't find core function " );
110 buf
.appendAscii( func
);
111 throw RuntimeException(buf
.makeStringAndClear(),Reference
< XInterface
>());
117 //------------------------------------------------------------------------------------
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
;
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",
151 localDateTime
.Minutes
,
152 localDateTime
.Seconds
,
153 sal::static_int_cast
< unsigned long >(
154 localDateTime
.NanoSeconds
/1000000),
156 sal::static_int_cast
< long >(
157 (sal_Int32
) osl::Thread::getCurrentIdentifier()),
164 void appendPointer(OUStringBuffer
& buffer
, void * pointer
) {
166 sal::static_int_cast
< sal_Int64
>(
167 reinterpret_cast< sal_IntPtr
>(pointer
)),
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
);
183 buf
.append( aFunctionName
);
186 val2str( data
, type
.getTypeLibType(), VAL2STR_MODE_SHALLOW
) );
187 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
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
);
204 buf
.append( aFunctionName
);
206 if( isLog( cargo
, LogLevel::ARGS
) )
209 val2str( returnValue
.getValue(), returnValue
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
210 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
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
);
229 buf
.append( aFunctionName
);
231 if( isLog( cargo
, LogLevel::ARGS
) )
233 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
238 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
242 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */