Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / pyuno / source / module / pyuno_util.cxx
blob77006c33acac7b3e7513979229e9c69dec5c65d8
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( PyUnicode_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 Py_ssize_t size(0);
67 char const *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
68 ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8);
69 #endif
71 else
73 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
74 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
76 return ret;
79 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
81 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), func ) );
82 if( !object.is() )
84 throw RuntimeException("couldn't find core function " + OUString::createFromAscii(func));
86 return object;
90 // Logging
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 ) )
106 return;
108 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
110 TimeValue systemTime;
111 TimeValue localTime;
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",
120 localDateTime.Year,
121 localDateTime.Month,
122 localDateTime.Day,
123 localDateTime.Hours,
124 localDateTime.Minutes,
125 localDateTime.Seconds,
126 sal::static_int_cast< unsigned long >(
127 localDateTime.NanoSeconds/1000000),
128 strLevel[level],
129 sal::static_int_cast< long >(
130 static_cast<sal_Int32>(osl::Thread::getCurrentIdentifier())),
131 str );
134 namespace {
136 void appendPointer(OUStringBuffer & buffer, void * pointer) {
137 buffer.append(
138 sal::static_int_cast< sal_Int64 >(
139 reinterpret_cast< sal_IntPtr >(pointer)),
140 16);
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 + " = " );
155 buf.append(
156 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
157 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
162 void logReply(
163 RuntimeCargo *cargo,
164 const char *intro,
165 void * ptr,
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 ) )
176 buf.append(
177 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
178 for( const auto & p : aParams )
180 buf.append( ", " );
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 ++ )
200 if( i > 0 )
201 buf.append( ", " );
202 buf.append(
203 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
206 buf.append( ")" );
207 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */