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 using com::sun::star::uno::Sequence
;
33 using com::sun::star::uno::Any
;
34 using com::sun::star::uno::RuntimeException
;
38 PyRef
ustring2PyUnicode( const OUString
& str
)
41 #if Py_UNICODE_SIZE == 2
42 // YD force conversion since python/2 uses wchar_t
43 ret
= PyRef( PyUnicode_FromUnicode( (const Py_UNICODE
*)str
.getStr(), str
.getLength() ), SAL_NO_ACQUIRE
);
45 OString
sUtf8(OUStringToOString(str
, RTL_TEXTENCODING_UTF8
));
46 ret
= PyRef( PyUnicode_DecodeUTF8( sUtf8
.getStr(), sUtf8
.getLength(), nullptr) , SAL_NO_ACQUIRE
);
51 PyRef
ustring2PyString( const OUString
&str
)
53 OString o
= OUStringToOString( str
, osl_getThreadTextEncoding() );
54 return PyRef( PyStr_FromString( o
.getStr() ), SAL_NO_ACQUIRE
);
57 OUString
pyString2ustring( PyObject
*pystr
)
60 if( PyUnicode_Check( pystr
) )
62 #if Py_UNICODE_SIZE == 2
63 ret
= OUString( (sal_Unicode
* ) PyUnicode_AS_UNICODE( pystr
) );
65 #if PY_MAJOR_VERSION >= 3
67 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr
, &size
));
68 ret
= OUString(pUtf8
, size
, RTL_TEXTENCODING_UTF8
);
70 PyObject
* pUtf8
= PyUnicode_AsUTF8String(pystr
);
71 ret
= OUString(PyStr_AsString(pUtf8
), PyString_Size(pUtf8
), RTL_TEXTENCODING_UTF8
);
78 #if PY_MAJOR_VERSION >= 3
79 char *name
= PyBytes_AsString(pystr
); // hmmm... is this a good idea?
81 char *name
= PyString_AsString(pystr
);
83 ret
= OUString( name
, strlen(name
), osl_getThreadTextEncoding() );
88 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * func
)
89 throw ( RuntimeException
)
91 PyRef
object(PyDict_GetItemString( runtime
.getImpl()->cargo
->getUnoModule().get(), func
) );
95 buf
.append( "couldn't find core function " );
96 buf
.appendAscii( func
);
97 throw RuntimeException(buf
.makeStringAndClear());
106 bool isLog( RuntimeCargo
* cargo
, sal_Int32 loglevel
)
108 return cargo
&& cargo
->logFile
&& loglevel
<= cargo
->logLevel
;
111 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const OUString
&logString
)
113 log( cargo
, level
, OUStringToOString( logString
, osl_getThreadTextEncoding() ).getStr() );
116 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const char *str
)
118 if( isLog( cargo
, level
) )
120 static const char *strLevel
[] = { "NONE", "CALL", "ARGS" };
122 TimeValue systemTime
;
124 oslDateTime localDateTime
;
126 osl_getSystemTime( &systemTime
);
127 osl_getLocalTimeFromSystemTime( &systemTime
, &localTime
);
128 osl_getDateTimeFromTimeValue( &localTime
, &localDateTime
);
130 fprintf( cargo
->logFile
,
131 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
136 localDateTime
.Minutes
,
137 localDateTime
.Seconds
,
138 sal::static_int_cast
< unsigned long >(
139 localDateTime
.NanoSeconds
/1000000),
141 sal::static_int_cast
< long >(
142 (sal_Int32
) osl::Thread::getCurrentIdentifier()),
149 void appendPointer(OUStringBuffer
& buffer
, void * pointer
) {
151 sal::static_int_cast
< sal_Int64
>(
152 reinterpret_cast< sal_IntPtr
>(pointer
)),
158 void logException( RuntimeCargo
*cargo
, const char *intro
,
159 void * ptr
, const OUString
&aFunctionName
,
160 const void * data
, const css::uno::Type
& type
)
162 if( isLog( cargo
, LogLevel::CALL
) )
164 OUStringBuffer
buf( 128 );
165 buf
.appendAscii( intro
);
166 appendPointer(buf
, ptr
);
168 buf
.append( aFunctionName
);
171 val2str( data
, type
.getTypeLibType(), VAL2STR_MODE_SHALLOW
) );
172 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
181 const OUString
& aFunctionName
,
182 const Any
&returnValue
,
183 const Sequence
< Any
> & aParams
)
185 OUStringBuffer
buf( 128 );
186 buf
.appendAscii( intro
);
187 appendPointer(buf
, ptr
);
189 buf
.append( aFunctionName
);
191 if( isLog( cargo
, LogLevel::ARGS
) )
194 val2str( returnValue
.getValue(), returnValue
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
195 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
199 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
202 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
206 void logCall( RuntimeCargo
*cargo
, const char *intro
,
207 void * ptr
, const OUString
& aFunctionName
,
208 const Sequence
< Any
> & aParams
)
210 OUStringBuffer
buf( 128 );
211 buf
.appendAscii( intro
);
212 appendPointer(buf
, ptr
);
214 buf
.append( aFunctionName
);
216 if( isLog( cargo
, LogLevel::ARGS
) )
218 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
223 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
227 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */