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"
22 #include <osl/thread.h>
23 #include <osl/thread.hxx>
25 #include <rtl/ustrbuf.hxx>
28 using com::sun::star::uno::Sequence
;
29 using com::sun::star::uno::Any
;
30 using com::sun::star::uno::RuntimeException
;
34 PyRef
ustring2PyUnicode( const OUString
& str
)
37 #if Py_UNICODE_SIZE == 2
39 ret
= PyRef( PyUnicode_FromUnicode( reinterpret_cast<const unsigned short *>(str
.getStr()), str
.getLength() ), SAL_NO_ACQUIRE
);
41 static_assert(sizeof (wchar_t) == Py_UNICODE_SIZE
, "bad assumption");
42 ret
= PyRef( PyUnicode_FromUnicode( reinterpret_cast<wchar_t const *>(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
64 reinterpret_cast<sal_Unicode
const *>(PyUnicode_AS_UNICODE( pystr
)) );
66 #if PY_MAJOR_VERSION >= 3
68 char const *pUtf8(PyUnicode_AsUTF8AndSize(pystr
, &size
));
69 ret
= OUString(pUtf8
, size
, RTL_TEXTENCODING_UTF8
);
71 PyObject
* pUtf8
= PyUnicode_AsUTF8String(pystr
);
72 ret
= OUString(PyStr_AsString(pUtf8
), PyString_Size(pUtf8
), RTL_TEXTENCODING_UTF8
);
79 #if PY_MAJOR_VERSION >= 3
80 char *name
= PyBytes_AsString(pystr
); // hmmm... is this a good idea?
82 char *name
= PyString_AsString(pystr
);
84 ret
= OUString( name
, strlen(name
), osl_getThreadTextEncoding() );
89 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * func
)
91 PyRef
object(PyDict_GetItemString( runtime
.getImpl()->cargo
->getUnoModule().get(), func
) );
94 throw RuntimeException("couldn't find core function " + OUString::createFromAscii(func
));
103 bool isLog( RuntimeCargo
const * cargo
, sal_Int32 loglevel
)
105 return cargo
&& cargo
->logFile
&& loglevel
<= cargo
->logLevel
;
108 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const OUString
&logString
)
110 log( cargo
, level
, OUStringToOString( logString
, osl_getThreadTextEncoding() ).getStr() );
113 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const char *str
)
115 if( isLog( cargo
, level
) )
117 static const char *strLevel
[] = { "NONE", "CALL", "ARGS" };
119 TimeValue systemTime
;
121 oslDateTime localDateTime
;
123 osl_getSystemTime( &systemTime
);
124 osl_getLocalTimeFromSystemTime( &systemTime
, &localTime
);
125 osl_getDateTimeFromTimeValue( &localTime
, &localDateTime
);
127 fprintf( cargo
->logFile
,
128 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
133 localDateTime
.Minutes
,
134 localDateTime
.Seconds
,
135 sal::static_int_cast
< unsigned long >(
136 localDateTime
.NanoSeconds
/1000000),
138 sal::static_int_cast
< long >(
139 static_cast<sal_Int32
>(osl::Thread::getCurrentIdentifier())),
146 void appendPointer(OUStringBuffer
& buffer
, void * pointer
) {
148 sal::static_int_cast
< sal_Int64
>(
149 reinterpret_cast< sal_IntPtr
>(pointer
)),
155 void logException( RuntimeCargo
*cargo
, const char *intro
,
156 void * ptr
, const OUString
&aFunctionName
,
157 const void * data
, const css::uno::Type
& type
)
159 if( isLog( cargo
, LogLevel::CALL
) )
161 OUStringBuffer
buf( 128 );
162 buf
.appendAscii( intro
);
163 appendPointer(buf
, ptr
);
164 buf
.append( "]." + aFunctionName
+ " = " );
166 val2str( data
, type
.getTypeLibType(), VAL2STR_MODE_SHALLOW
) );
167 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
176 const OUString
& aFunctionName
,
177 const Any
&returnValue
,
178 const Sequence
< Any
> & aParams
)
180 OUStringBuffer
buf( 128 );
181 buf
.appendAscii( intro
);
182 appendPointer(buf
, ptr
);
183 buf
.append( "]." + aFunctionName
+ "()=" );
184 if( isLog( cargo
, LogLevel::ARGS
) )
187 val2str( returnValue
.getValue(), returnValue
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
188 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
192 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
195 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
199 void logCall( RuntimeCargo
*cargo
, const char *intro
,
200 void * ptr
, const OUString
& aFunctionName
,
201 const Sequence
< Any
> & aParams
)
203 OUStringBuffer
buf( 128 );
204 buf
.appendAscii( intro
);
205 appendPointer(buf
, ptr
);
206 buf
.append( "]." + aFunctionName
+ "(" );
207 if( isLog( cargo
, LogLevel::ARGS
) )
209 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
214 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
218 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */