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( PyUnicode_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
)) );
67 char const *pUtf8(PyUnicode_AsUTF8AndSize(pystr
, &size
));
68 ret
= OUString(pUtf8
, size
, RTL_TEXTENCODING_UTF8
);
73 char *name
= PyBytes_AsString(pystr
); // hmmm... is this a good idea?
74 ret
= OUString( name
, strlen(name
), osl_getThreadTextEncoding() );
79 PyRef
getObjectFromUnoModule( const Runtime
&runtime
, const char * func
)
81 PyRef
object(PyDict_GetItemString( runtime
.getImpl()->cargo
->getUnoModule().get(), func
) );
84 throw RuntimeException("couldn't find core function " + OUString::createFromAscii(func
));
93 bool isLog( RuntimeCargo
const * cargo
, sal_Int32 loglevel
)
95 return cargo
&& cargo
->logFile
&& loglevel
<= cargo
->logLevel
;
98 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const OUString
&logString
)
100 log( cargo
, level
, OUStringToOString( logString
, osl_getThreadTextEncoding() ).getStr() );
103 void log( RuntimeCargo
* cargo
, sal_Int32 level
, const char *str
)
105 if( !isLog( cargo
, level
) )
108 static const char *strLevel
[] = { "NONE", "CALL", "ARGS" };
110 TimeValue systemTime
;
112 oslDateTime localDateTime
;
114 osl_getSystemTime( &systemTime
);
115 osl_getLocalTimeFromSystemTime( &systemTime
, &localTime
);
116 osl_getDateTimeFromTimeValue( &localTime
, &localDateTime
);
118 fprintf( cargo
->logFile
,
119 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
124 localDateTime
.Minutes
,
125 localDateTime
.Seconds
,
126 sal::static_int_cast
< unsigned long >(
127 localDateTime
.NanoSeconds
/1000000),
129 sal::static_int_cast
< long >(
130 static_cast<sal_Int32
>(osl::Thread::getCurrentIdentifier())),
136 void appendPointer(OUStringBuffer
& buffer
, void * pointer
) {
138 sal::static_int_cast
< sal_Int64
>(
139 reinterpret_cast< sal_IntPtr
>(pointer
)),
145 void logException( RuntimeCargo
*cargo
, const char *intro
,
146 void * ptr
, const OUString
&aFunctionName
,
147 const void * data
, const css::uno::Type
& type
)
149 if( isLog( cargo
, LogLevel::CALL
) )
151 OUStringBuffer
buf( 128 );
152 buf
.appendAscii( intro
);
153 appendPointer(buf
, ptr
);
154 buf
.append( "]." + aFunctionName
+ " = " );
156 val2str( data
, type
.getTypeLibType(), VAL2STR_MODE_SHALLOW
) );
157 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
166 const OUString
& aFunctionName
,
167 const Any
&returnValue
,
168 const Sequence
< Any
> & aParams
)
170 OUStringBuffer
buf( 128 );
171 buf
.appendAscii( intro
);
172 appendPointer(buf
, ptr
);
173 buf
.append( "]." + aFunctionName
+ "()=" );
174 if( isLog( cargo
, LogLevel::ARGS
) )
177 val2str( returnValue
.getValue(), returnValue
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
178 for( const auto & p
: aParams
)
181 buf
.append( val2str( p
.getValue(), p
.getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
184 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
188 void logCall( RuntimeCargo
*cargo
, const char *intro
,
189 void * ptr
, const OUString
& aFunctionName
,
190 const Sequence
< Any
> & aParams
)
192 OUStringBuffer
buf( 128 );
193 buf
.appendAscii( intro
);
194 appendPointer(buf
, ptr
);
195 buf
.append( "]." + aFunctionName
+ "(" );
196 if( isLog( cargo
, LogLevel::ARGS
) )
198 for( int i
= 0; i
< aParams
.getLength() ; i
++ )
203 val2str( aParams
[i
].getValue(), aParams
[i
].getValueTypeRef(), VAL2STR_MODE_SHALLOW
) );
207 log( cargo
,LogLevel::CALL
, buf
.makeStringAndClear() );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */