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

Generated by: LCOV version 1.10