LCOV - code coverage report
Current view: top level - libreoffice/pyuno/source/module - pyuno_util.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 81 0.0 %
Date: 2012-12-27 Functions: 0 11 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <time.h>
      23             : #include <osl/thread.h>
      24             : 
      25             : #include <typelib/typedescription.hxx>
      26             : 
      27             : #include <rtl/strbuf.hxx>
      28             : #include <rtl/ustrbuf.hxx>
      29             : #include <osl/time.h>
      30             : 
      31             : #include <com/sun/star/beans/XMaterialHolder.hpp>
      32             : 
      33             : using rtl::OUStringToOString;
      34             : using rtl::OUString;
      35             : using rtl::OString;
      36             : using rtl::OStringBuffer;
      37             : using rtl::OUStringBuffer;
      38             : 
      39             : 
      40             : using com::sun::star::uno::TypeDescription;
      41             : using com::sun::star::uno::Sequence;
      42             : using com::sun::star::uno::Reference;
      43             : using com::sun::star::uno::XInterface;
      44             : using com::sun::star::uno::Any;
      45             : using com::sun::star::uno::Type;
      46             : using com::sun::star::uno::UNO_QUERY;
      47             : using com::sun::star::uno::TypeClass;
      48             : using com::sun::star::uno::RuntimeException;
      49             : using com::sun::star::uno::XComponentContext;
      50             : using com::sun::star::lang::XSingleServiceFactory;
      51             : using com::sun::star::script::XTypeConverter;
      52             : using com::sun::star::beans::XMaterialHolder;
      53             : 
      54             : namespace pyuno
      55             : {
      56           0 : PyRef ustring2PyUnicode( const OUString & str )
      57             : {
      58           0 :     PyRef ret;
      59             : #if Py_UNICODE_SIZE == 2
      60             :     // YD force conversion since python/2 uses wchar_t
      61             :     ret = PyRef( PyUnicode_FromUnicode( (const Py_UNICODE*)str.getStr(), str.getLength() ), SAL_NO_ACQUIRE );
      62             : #else
      63           0 :     OString sUtf8(OUStringToOString(str, RTL_TEXTENCODING_UTF8));
      64           0 :     ret = PyRef( PyUnicode_DecodeUTF8( sUtf8.getStr(), sUtf8.getLength(), NULL) , SAL_NO_ACQUIRE );
      65             : #endif
      66           0 :     return ret;
      67             : }
      68             : 
      69           0 : PyRef ustring2PyString( const OUString &str )
      70             : {
      71           0 :     OString o = OUStringToOString( str, osl_getThreadTextEncoding() );
      72           0 :     return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE );
      73             : }
      74             : 
      75           0 : OUString pyString2ustring( PyObject *pystr )
      76             : {
      77           0 :     OUString ret;
      78           0 :     if( PyUnicode_Check( pystr ) )
      79             :     {
      80             : #if Py_UNICODE_SIZE == 2
      81             :     ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) );
      82             : #else
      83             : #if PY_MAJOR_VERSION >= 3
      84           0 :     Py_ssize_t size(0);
      85           0 :     char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size));
      86           0 :     ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8);
      87             : #else
      88             :     PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr);
      89             :     ret = OUString(PyStr_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8);
      90             :     Py_DECREF(pUtf8);
      91             : #endif
      92             : #endif
      93             :     }
      94             :     else
      95             :     {
      96             : #if PY_MAJOR_VERSION >= 3
      97           0 :         char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea?
      98             : #else
      99             :         char *name = PyString_AsString(pystr);
     100             : #endif
     101           0 :         ret = OUString( name, strlen(name), osl_getThreadTextEncoding() );
     102             :     }
     103           0 :     return ret;
     104             : }
     105             : 
     106           0 : PyRef getObjectFromUnoModule( const Runtime &runtime, const char * func )
     107             :     throw ( RuntimeException )
     108             : {
     109           0 :     PyRef object(PyDict_GetItemString( runtime.getImpl()->cargo->getUnoModule().get(), (char*)func ) );
     110           0 :     if( !object.is() )
     111             :     {
     112           0 :         OUStringBuffer buf;
     113           0 :         buf.appendAscii( "couldn't find core function " );
     114           0 :         buf.appendAscii( func );
     115           0 :         throw RuntimeException(buf.makeStringAndClear(),Reference< XInterface >());
     116             :     }
     117           0 :     return object;
     118             : }
     119             : 
     120             : 
     121             : //------------------------------------------------------------------------------------
     122             : // Logging
     123             : //------------------------------------------------------------------------------------
     124             : 
     125           0 : bool isLog( RuntimeCargo * cargo, sal_Int32 loglevel )
     126             : {
     127           0 :     return cargo && cargo->logFile && loglevel <= cargo->logLevel;
     128             : }
     129             : 
     130           0 : void log( RuntimeCargo * cargo, sal_Int32 level, const rtl::OUString &logString )
     131             : {
     132           0 :     log( cargo, level, OUStringToOString( logString, osl_getThreadTextEncoding() ).getStr() );
     133           0 : }
     134             : 
     135           0 : void log( RuntimeCargo * cargo, sal_Int32 level, const char *str )
     136             : {
     137           0 :     if( isLog( cargo, level ) )
     138             :     {
     139             :         static const char *strLevel[] = { "NONE", "CALL", "ARGS" };
     140             : 
     141             :         TimeValue systemTime;
     142             :         TimeValue localTime;
     143             :         oslDateTime localDateTime;
     144             : 
     145           0 :         osl_getSystemTime( &systemTime );
     146           0 :         osl_getLocalTimeFromSystemTime( &systemTime, &localTime );
     147           0 :         osl_getDateTimeFromTimeValue( &localTime, &localDateTime );
     148             : 
     149             :         fprintf( cargo->logFile,
     150             :                  "%4i-%02i-%02i %02i:%02i:%02i,%03lu [%s,tid %ld]: %s\n",
     151             :                  localDateTime.Year,
     152             :                  localDateTime.Month,
     153             :                  localDateTime.Day,
     154             :                  localDateTime.Hours,
     155             :                  localDateTime.Minutes,
     156             :                  localDateTime.Seconds,
     157             :                  sal::static_int_cast< unsigned long >(
     158             :                      localDateTime.NanoSeconds/1000000),
     159             :                  strLevel[level],
     160             :                  sal::static_int_cast< long >(
     161           0 :                      (sal_Int32) osl_getThreadIdentifier( 0)),
     162           0 :                  str );
     163             :     }
     164           0 : }
     165             : 
     166             : namespace {
     167             : 
     168           0 : void appendPointer(rtl::OUStringBuffer & buffer, void * pointer) {
     169             :     buffer.append(
     170             :         sal::static_int_cast< sal_Int64 >(
     171             :             reinterpret_cast< sal_IntPtr >(pointer)),
     172           0 :         16);
     173           0 : }
     174             : 
     175             : }
     176             : 
     177           0 : void logException( RuntimeCargo *cargo, const char *intro,
     178             :                    void * ptr, const rtl::OUString &aFunctionName,
     179             :                    const void * data, const com::sun::star::uno::Type & type )
     180             : {
     181           0 :     if( isLog( cargo, LogLevel::CALL ) )
     182             :     {
     183           0 :         rtl::OUStringBuffer buf( 128 );
     184           0 :         buf.appendAscii( intro );
     185           0 :         appendPointer(buf, ptr);
     186           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
     187           0 :         buf.append( aFunctionName );
     188           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " = " ) );
     189             :         buf.append(
     190           0 :             val2str( data, type.getTypeLibType(), VAL2STR_MODE_SHALLOW ) );
     191           0 :         log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
     192             :     }
     193             : 
     194           0 : }
     195             : 
     196           0 : void logReply(
     197             :     RuntimeCargo *cargo,
     198             :     const char *intro,
     199             :     void * ptr,
     200             :     const rtl::OUString & aFunctionName,
     201             :     const Any &returnValue,
     202             :     const Sequence< Any > & aParams )
     203             : {
     204           0 :     rtl::OUStringBuffer buf( 128 );
     205           0 :     buf.appendAscii( intro );
     206           0 :     appendPointer(buf, ptr);
     207           0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
     208           0 :     buf.append( aFunctionName );
     209           0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("()=") );
     210           0 :     if( isLog( cargo, LogLevel::ARGS ) )
     211             :     {
     212             :         buf.append(
     213           0 :             val2str( returnValue.getValue(), returnValue.getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
     214           0 :         for( int i = 0; i < aParams.getLength() ; i ++ )
     215             :         {
     216           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
     217             :             buf.append(
     218           0 :                 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
     219             :         }
     220             :     }
     221           0 :     log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
     222             : 
     223           0 : }
     224             : 
     225           0 : void logCall( RuntimeCargo *cargo, const char *intro,
     226             :               void * ptr, const rtl::OUString & aFunctionName,
     227             :               const Sequence< Any > & aParams )
     228             : {
     229           0 :     rtl::OUStringBuffer buf( 128 );
     230           0 :     buf.appendAscii( intro );
     231           0 :     appendPointer(buf, ptr);
     232           0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("].") );
     233           0 :     buf.append( aFunctionName );
     234           0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("(") );
     235           0 :     if( isLog( cargo, LogLevel::ARGS ) )
     236             :     {
     237           0 :         for( int i = 0; i < aParams.getLength() ; i ++ )
     238             :         {
     239           0 :             if( i > 0 )
     240           0 :                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", " ) );
     241             :             buf.append(
     242           0 :                 val2str( aParams[i].getValue(), aParams[i].getValueTypeRef(), VAL2STR_MODE_SHALLOW) );
     243             :         }
     244             :     }
     245           0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
     246           0 :     log( cargo,LogLevel::CALL, buf.makeStringAndClear() );
     247           0 : }
     248             : 
     249             : 
     250             : }
     251             : 
     252             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10