Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / pyuno / source / module / pyuno_util.cxx
blobede4cd6f01c0eef1a58ec6b7403fcd1deb059ec7
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 <time.h>
23 #include <osl/thread.h>
24 #include <osl/thread.hxx>
26 #include <typelib/typedescription.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/time.h>
32 using com::sun::star::uno::Sequence;
33 using com::sun::star::uno::Any;
34 using com::sun::star::uno::RuntimeException;
36 namespace pyuno
38 PyRef ustring2PyUnicode( const OUString & str )
40 PyRef ret;
41 #if Py_UNICODE_SIZE == 2
42 // YD force conversion since python/2 uses wchar_t
43 ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
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( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
64 #else
65 #if PY_MAJOR_VERSION >= 3
66 Py_ssize_t size(0);
67 char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
68 ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8);
69 #else
70 PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
71 ret = OUString(PyStr_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
72 Py_DECREF(pUtf8);
73 #endif
74 #endif
76 else
78 #if PY_MAJOR_VERSION >= 3
79 char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
80 #else
81 char *name = PyString_AsString(pystr);
82 #endif
83 ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
85 return ret;
88 PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
89 throw ( RuntimeException )
91 PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), func ) );
92 if( !object.is() )
94 OUStringBuffer buf;
95 buf.append( "couldn't find core function " );
96 buf.appendAscii( func );
97 throw RuntimeException(buf.makeStringAndClear());
99 return object;
103 // Logging
106 bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
108 return cargo && cargo->logFile && loglevel <= cargo->logLevel;
111 void log( RuntimeCargo * cargo, sal_Int32 level, const OUString &logString )
113 log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
116 void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
118 if( isLog( cargo, level ) )
120 static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
122 TimeValue systemTime;
123 TimeValue localTime;
124 oslDateTime localDateTime;
126 osl_getSystemTime( &systemTime );
127 osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
128 osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
130 fprintf( cargo->logFile,
131 "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
132 localDateTime.Year,
133 localDateTime.Month,
134 localDateTime.Day,
135 localDateTime.Hours,
136 localDateTime.Minutes,
137 localDateTime.Seconds,
138 sal::static_int_cast< unsigned long >(
139 localDateTime.NanoSeconds/1000000),
140 strLevel[level],
141 sal::static_int_cast< long >(
142 (sal_Int32) osl::Thread::getCurrentIdentifier()),
143 str );
147 namespace {
149 void appendPointer(OUStringBuffer & buffer, void * pointer) {
150 buffer.append(
151 sal::static_int_cast< sal_Int64 >(
152 reinterpret_cast< sal_IntPtr >(pointer)),
153 16);
158 void logException( RuntimeCargo *cargo, const char *intro,
159 void * ptr, const OUString &aFunctionName,
160 const void * data, const css::uno::Type & type )
162 if( isLog( cargo, LogLevel::CALL ) )
164 OUStringBuffer buf( 128 );
165 buf.appendAscii( intro );
166 appendPointer(buf, ptr);
167 buf.append( "]." );
168 buf.append( aFunctionName );
169 buf.append( " = " );
170 buf.append(
171 val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
172 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
177 void logReply(
178 RuntimeCargo *cargo,
179 const char *intro,
180 void * ptr,
181 const OUString & aFunctionName,
182 const Any &returnValue,
183 const Sequence< Any > & aParams )
185 OUStringBuffer buf( 128 );
186 buf.appendAscii( intro );
187 appendPointer(buf, ptr);
188 buf.append( "]." );
189 buf.append( aFunctionName );
190 buf.append( "()=" );
191 if( isLog( cargo, LogLevel::ARGS ) )
193 buf.append(
194 val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
195 for( int i = 0; i < aParams.getLength() ; i ++ )
197 buf.append( ", " );
198 buf.append(
199 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
202 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
206 void logCall( RuntimeCargo *cargo, const char *intro,
207 void * ptr, const OUString & aFunctionName,
208 const Sequence< Any > & aParams )
210 OUStringBuffer buf( 128 );
211 buf.appendAscii( intro );
212 appendPointer(buf, ptr);
213 buf.append( "]." );
214 buf.append( aFunctionName );
215 buf.append( "(" );
216 if( isLog( cargo, LogLevel::ARGS ) )
218 for( int i = 0; i < aParams.getLength() ; i ++ )
220 if( i > 0 )
221 buf.append( ", " );
222 buf.append(
223 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
226 buf.append( ")" );
227 log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */