Python回调ctypes函数与va_list

时间:2012-03-20 07:30:16

标签: python callback ctypes variadic-functions

我在定义dll用来获取dll其他函数日志的回调函数时遇到问题。 这是c函数:

typedef void (RTMP_LogCallback)(int level, const char *fmt, va_list);
void RTMP_LogPrintf(const char *format, ...);

这是我的python代码:

dll = cdll.LoadLibrary('bin/librtmp.dll')
CMPFUNC = CFUNCTYPE(c_int, c_int, c_char_p, c_void_p)
def logCallBack(level, message, va_list):
    dll.RTMP_LogPrintf(message, va_list)
    return 0

问题是日志很脏且不可读:

    Parsed host    : ☺Parsed app     : ☺connect to stream
ÄßGj♫¨', ... connected, handshaking?ßGj♥: Type Answer   : 27DBEC?ßGjä?┼♣: Server Uptime : 2612204?ßGj♥: FMS Version   : 2612204.2612100.488283845.4882
83530?ßGj♥: Handshaking finished....ÄßGj♫¨', handshaked│°Gj♀☻: fd=2614284, size=2614180Invoking ▒?'ä°Gj♀☻: fd=2618556`÷Gjá%&: server BW = 2618572ä°Gj♀
☻: fd=2618556o÷Gjá%&: client BW = 2618572 2618468ä°Gj♀☻: fd=2618556?÷Gj, received ctrl. type: 2618572, len: 2618468?÷Gj, Stream Begin 2618572ä°Gj♀☻: f
d=26185568÷Gj, received: chunk size change to 2618572ä°Gj♀☻: fd=2618556N÷Gj♣☺, received: invoke 2618572 bytes(object begin)Property: <|±'☺>Property: <
|±'☺>Property: <|±'OBJECT>(object begin)Property: <,´'☺>Property: <,´'☺>Property: <,´'☺>(object end)Property: <|±'OBJECT>(object begin)Property: <,´'☺
>Property: <,´'☺>Property: <,´'☺>Property: <,´'OBJECT>(object begin)Property: <▄ý'☺>(object end)Property: <,´'☺>Property: <,´'☺>(object end)(object en
d)~÷Gjmt;, server invoking <☺>~÷Gj, received result for method call <☺>│°Gj♀☻: fd=2616844, size=2616740sending ctrl. type: 0x27f264│°Gj♀☻: fd=2616828,
 size=2616724│°Gj♀☻: fd=2616828, size=2616724Invoking ?‗'UsherToken: ú?B☻,☺│°Gj♀☻: fd=2617164, size=2617060Invoking )¶'ä°Gj♀☻: fd=2618556N÷Gj↔, receiv
ed: invoke 2618572 bytes(object begin)Property: <|±'☺>Property: <|±'☺>Property: NULLProperty: <|±'☺>(object end)~÷Gj?⌂;, server invoking <☺>~÷Gj8¶;, r
eceived result for method call <☺>¶÷Gj, seekTime=2617308, stopTime=2617204, sending play: â─↑?♥â╚*?e¶╔├U?ýâý►SVW?¨?w►?E*ëE??F♦┴Ó☻?┌??↨│°Gj♀☻: fd=26171
64, size=2617060Invoking )¶'sending ctrl. type: 0x27f264│°Gj♀☻: fd=2616828, size=2616724ä°Gj♀☻: fd=2618556N÷Gj%, received: invoke 2618572 bytes(object
 begin)Property: <|±'☺>Property: <|±'☺>Property: NULLProperty: <|±'☺>(object end)~÷Gj?⌂;, server invoking <☺>~÷Gj, received result id 2617308 without
matching requestä°Gj♀☻: fd=2618556?÷Gj, received ctrl. type: 2618572, len: 2618468?÷Gj☺, Stream Begin 2618572ä°Gj♀☻: fd=2618556N÷Gjö, received: invoke
 2618572 bytes(object begin)Property: <|±'☺>Property: <|±'☺>Property: NULLProperty: <|±'OBJECT>(object begin)Property: <,´'☺>Property: <,´'☺>Property:
 <,´'☺>Property: <,´'☺>(object end)(object end)~÷Gj?t;, server invoking <☺>~÷Gj┴t;, onStatus: ☺ä°Gj♀☻: fd=2618556N÷Gj?, received: invoke 2618572 bytes
(object begin)Property: <|±'☺>Property: <|±'☺>Property: NULLProperty: <|±'OBJECT>(object begin)Property: <,´'☺>Property: <,´'☺>Property: <,´'☺>Propert, ...etc

我还尝试直接打印这样的消息:

def logCallBack(level, message, va_list):
    print message
    return 0

但这一次,日志不完整:

%s, ... connected, handshaking
%s: Type Answer   : %02X
%s: Server Uptime : %d
%s: FMS Version   : %d.%d.%d.%d
%s: Handshaking finished....
%s, handshaked
%s: fd=%d, size=%d
Invoking %s
%s: fd=%d
%s: server BW = %d
%s: fd=%d
%s: client BW = %d %d
%s: fd=%d
%s, received ctrl. type: %d, len: %d
... etc

有谁知道要解决这个问题吗?

提前致谢。

问候,Gontran

编辑:

根据这个线程(http://www.digipedia.pl/usenet/thread/15901/197/),ctypes没有提供处理varargs函数的方法。我明白了这一点。但是为什么使用RTMP_LogPrintf()函数不能正常工作?我的印象是我正在传递正确的论据。

有关信息,RTMP_LogPrintf()的定义如下:

void RTMP_LogPrintf(const char *format, ...)
{
    char str[MAX_PRINT_LEN]="";
    int len;
    va_list args;
    va_start(args, format);
    len = vsnprintf(str, MAX_PRINT_LEN-1, format, args);
    va_end(args);

    if ( RTMP_debuglevel==RTMP_LOGCRIT )
        return;

    if ( !fmsg ) fmsg = stderr;

    if (neednl) {
        putc('\n', fmsg);
        neednl = 0;
    }

    if (len > MAX_PRINT_LEN-1)
          len = MAX_PRINT_LEN-1;
    fprintf(fmsg, "%s", str);
    if (str[len-1] == '\n')
        fflush(fmsg);
}

2 个答案:

答案 0 :(得分:0)

我们假设类型va_list实际上可以由c_void_p表示(这是一个可能错误的假设)。即便如此,以下呼叫也是错误的:

dll.RTMP_LogPrintf(message, va_list)

你在这里传递了两个参数,第二个参数是void *。如果您写了

,这与您在C中所做的一样
RTMP_LogPrintf(message, va_list);

无法按预期工作。这就是为什么有两个不同的函数printf()vprintf()。在C中,没有可移植的方法来构建具有可变数量参数的调用(只有接收器可以接受可变数量的参数)。在Python + ctypes中,您当然可以这样做,但是您需要将参数作为Python对象列表提供,而不仅仅是作为一个va_list对象。

答案 1 :(得分:-1)

试试这个

CMPFUNC = CFUNCTYPE(c_int, c_int, POINTER(c_char), c_void_p)