LibreOffice Module pyuno (master) 1
pyuno_util.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "pyuno_impl.hxx"
21
22#include <osl/thread.h>
23#include <osl/thread.hxx>
24
25#include <rtl/ustrbuf.hxx>
26#include <osl/time.h>
27
29using com::sun::star::uno::Any;
30using com::sun::star::uno::RuntimeException;
31
32namespace pyuno
33{
34PyRef ustring2PyUnicode( const OUString & str )
35{
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;
49}
50
51PyRef ustring2PyString( std::u16string_view str )
52{
53 OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
54 return PyRef( PyUnicode_FromString( o.getStr() ), SAL_NO_ACQUIRE );
55}
56
57OUString pyString2ustring( PyObject *pystr )
58{
59 OUString ret;
60 if( PyUnicode_Check( pystr ) )
61 {
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
70 }
71 else
72 {
73 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
74 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
75 }
76 return ret;
77}
78
79PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
80{
81 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), func ) );
82 if( !object.is() )
83 {
84 throw RuntimeException("couldn't find core function " + OUString::createFromAscii(func));
85 }
86 return object;
87}
88
89
90// Logging
91
92
93bool isLog( RuntimeCargo const * cargo, sal_Int32 loglevel )
94{
95 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
96}
97
98void log( RuntimeCargo * cargo, sal_Int32 level, std::u16string_view logString )
99{
100 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
101}
102
103void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
104{
105 if( !isLog( cargo, level ) )
106 return;
107
108 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
109
110 TimeValue systemTime;
111 TimeValue localTime;
112 oslDateTime localDateTime;
113
114 osl_getSystemTime( &systemTime );
115 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
116 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
117
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 );
132}
133
134namespace {
135
136void appendPointer(OUStringBuffer & buffer, void * pointer) {
137 buffer.append(
138 sal::static_int_cast< sal_Int64 >(
139 reinterpret_cast< sal_IntPtr >(pointer)),
140 16);
141}
142
143}
144
145void logException( RuntimeCargo *cargo, const char *intro,
146 void * ptr, std::u16string_view aFunctionName,
147 const void * data, const css::uno::Type & type )
148{
149 if( isLog( cargo, LogLevel::CALL ) )
150 {
151 OUStringBuffer buf( 128 );
152 buf.appendAscii( intro );
153 appendPointer(buf, ptr);
154 buf.append( OUString::Concat("].") + aFunctionName + " = " );
155 buf.append(
156 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
157 log( cargo,LogLevel::CALL, buf );
158 }
159
160}
161
163 RuntimeCargo *cargo,
164 const char *intro,
165 void * ptr,
166 std::u16string_view aFunctionName,
167 const Any &returnValue,
168 const Sequence< Any > & aParams )
169{
170 OUStringBuffer buf( 128 );
171 buf.appendAscii( intro );
172 appendPointer(buf, ptr);
173 buf.append( OUString::Concat("].") + aFunctionName + "()=" );
174 if( isLog( cargo, LogLevel::ARGS ) )
175 {
176 buf.append(
177 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
178 for( const auto & p : aParams )
179 {
180 buf.append( ", " + val2str( p.getValue(), p.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
181 }
182 }
183 log( cargo,LogLevel::CALL, buf );
184
185}
186
187void logCall( RuntimeCargo *cargo, const char *intro,
188 void * ptr, std::u16string_view aFunctionName,
189 const Sequence< Any > & aParams )
190{
191 OUStringBuffer buf( 128 );
192 buf.appendAscii( intro );
193 appendPointer(buf, ptr);
194 buf.append( OUString::Concat("].") + aFunctionName + "(" );
195 if( isLog( cargo, LogLevel::ARGS ) )
196 {
197 for( int i = 0; i < aParams.getLength() ; i ++ )
198 {
199 if( i > 0 )
200 buf.append( ", " );
201 buf.append(
202 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
203 }
204 }
205 buf.append( ")" );
206 log( cargo,LogLevel::CALL, buf );
207}
208
209
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class for keeping references to python objects.
Definition: pyuno.hxx:80
PyObject * get() const noexcept
Definition: pyuno.hxx:99
The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use p...
Definition: pyuno.hxx:164
RuntimeImpl * getImpl() const
Returns the internal handle.
Definition: pyuno.hxx:242
const char * name
void * p
size
int i
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
const sal_Int32 CALL
Definition: pyuno_impl.hxx:74
const sal_Int32 ARGS
Definition: pyuno_impl.hxx:75
Definition: pyuno.cxx:72
void log(RuntimeCargo *cargo, sal_Int32 level, std::u16string_view logString)
Definition: pyuno_util.cxx:98
bool isLog(RuntimeCargo const *cargo, sal_Int32 loglevel)
Definition: pyuno_util.cxx:93
void logCall(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Sequence< css::uno::Any > &args)
PyRef ustring2PyString(std::u16string_view source)
Definition: pyuno_util.cxx:51
OUString val2str(const void *pVal, typelib_TypeDescriptionReference *pTypeRef, sal_Int32 mode)
Definition: pyuno.cxx:87
void logException(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const void *data, const css::uno::Type &type)
Definition: pyuno_util.cxx:145
OUString pyString2ustring(PyObject *str)
Definition: pyuno_util.cxx:57
PyRef getObjectFromUnoModule(const Runtime &runtime, const char *object)
Definition: pyuno_util.cxx:79
PyRef ustring2PyUnicode(const OUString &source)
Definition: pyuno_util.cxx:34
void logReply(RuntimeCargo *cargo, const char *intro, void *ptr, std::u16string_view aFunctionName, const css::uno::Any &returnValue, const css::uno::Sequence< css::uno::Any > &args)
const sal_Int32 VAL2STR_MODE_SHALLOW
Definition: pyuno_impl.hxx:92
PyRef const & getUnoModule()
PyObject_HEAD struct RuntimeCargo * cargo
Definition: pyuno_impl.hxx:238
sal_uInt16 sal_Unicode
ResultType type