Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / pyuno / source / module / pyuno_util.cxx
blobf75533d47fdbf21572fa93bf80f6ba369b4a9f18
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #include <osl/time.h>
28 using com::sun::star::uno::Sequence;
29 using com::sun::star::uno::Any;
30 using com::sun::star::uno::RuntimeException;
32 namespace pyuno
34 PyRef ustring2PyUnicode( const OUString & str )
36 PyRef ret;
37 #if Py_UNICODE_SIZE == 2
38 #ifdef MACOSX
39 ret = PyRef( PyUnicode_FromUnicode( reinterpret_cast<const unsigned short *>(str.getStr()), str.getLength() ), SAL_NO_ACQUIRE );
40 #else
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 );
43 #endif
44 #else
45 OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
46 ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), nullptr) , SAL_NO_ACQUIRE );
47 #endif
48 return ret;
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 )
59 OUString ret;
60 if( PyUnicode_Check( pystr ) )
62 #if Py_UNICODE_SIZE == 2
63 ret = OUString(
64 reinterpret_cast<sal_Unicode const *>(PyUnicode_AS_UNICODE( pystr )) );
65 #else
66 #if PY_MAJOR_VERSION >= 3
67 Py_ssize_t size(0);
68 char const *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
69 ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8);
70 #else
71 PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
72 ret = OUString(PyStr_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
73 Py_DECREF(pUtf8);
74 #endif
75 #endif
77 else
79 #if PY_MAJOR_VERSION >= 3
80 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
81 #else
82 char *name = PyString_AsString(pystr);
83 #endif
84 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
86 return ret;
89 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
91 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), func ) );
92 if( !object.is() )
94 throw RuntimeException("couldn't find core function " + OUString::createFromAscii(func));
96 return object;
100 // Logging
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;
120 TimeValue localTime;
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",
129 localDateTime.Year,
130 localDateTime.Month,
131 localDateTime.Day,
132 localDateTime.Hours,
133 localDateTime.Minutes,
134 localDateTime.Seconds,
135 sal::static_int_cast< unsigned long >(
136 localDateTime.NanoSeconds/1000000),
137 strLevel[level],
138 sal::static_int_cast< long >(
139 static_cast<sal_Int32>(osl::Thread::getCurrentIdentifier())),
140 str );
144 namespace {
146 void appendPointer(OUStringBuffer & buffer, void * pointer) {
147 buffer.append(
148 sal::static_int_cast< sal_Int64 >(
149 reinterpret_cast< sal_IntPtr >(pointer)),
150 16);
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 + " = " );
165 buf.append(
166 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
167 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
172 void logReply(
173 RuntimeCargo *cargo,
174 const char *intro,
175 void * ptr,
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 ) )
186 buf.append(
187 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
188 for( int i = 0; i < aParams.getLength() ; i ++ )
190 buf.append( ", " );
191 buf.append(
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 ++ )
211 if( i > 0 )
212 buf.append( ", " );
213 buf.append(
214 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
217 buf.append( ")" );
218 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */