如何在包装函数调用的顺序中正确使用va_list? [英] How to use va_list correctly in a sequence of wrapper functions calls?

查看:177
本文介绍了如何在包装函数调用的顺序中正确使用va_list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于微控制器的以太网库(keil rl-tcpnet for lpc2478)。
库在调试输出函数中使用va_list(在stdarg.h中定义的arg数组宏的标准指针):

  void __debug__(const char * fmt,...)
{
va_list args;
va_start(args,fmt);
vprintf(fmt,args);
va_end(args);
}

然而,vprintf将数据发送到不正确的流,我需要重定向库的调试输出纠正串口。
库具有.c配置文件,我编写c ++代码,所以我使用包装函数指针从c代码调用c ++样式代码。所以我重写这个函数:

  extern void(* printDebugMsg)(const char * fmt,...); 
void __debug__(const char * fmt,...)
{
va_list args;
printDebugMsg(fmt,args);
}

其他cpp文件:

  void printDebugMsgImplementation(const char * fmt,...)
{
va_list args;

char buffer [100] = {0};

sprintf(buffer,fmt,args);

debugUart-> write(buffer);
}
void(* printDebugMsg)(const char * fmt,...)=& printDebugMsgImplementation;

结果调试输出如下所示:

  TCP:Init 0 Sockets 
IP:Src。 IP:0.0.14.1668436768
ETH:Dest.MAC:A1E05E10:A0002B44:5F9F:01:5FF8:33

文本和一些数字(如0)似乎是正确的,但大多数其他数字似乎格式错误(但也可能是内部库问题)。
我试图使用va_list重写我的代码,通常是在例子中进行的:

  void __debug__(const char * fmt,...)
{
va_list args;
va_start(args,fmt);
printDebugMsg(fmt,args);
va_end(args);
}

和.cpp文件:

  void printDebugMsgImplementation(const char * fmt,...)
{
va_list args;

char buffer [100] = {0};

va_start(args,fmt);
vsprintf(buffer,fmt,args);
va_end(args);

debugUart-> write(buffer);
}

然而结果更糟糕的是:

  TCP:Init -1579131344套接字
ARP:Dest.IP:-1579131400.-1610601474.24805.4
ARP:Src.MAC :A1E05DF8:A0002BFE:60FF:04:616C:20

我做错了什么?在我的包装函数调用的情况下如何处理va_list?

解决方案

你不能通过 va_list 到一个期望 ... 的函数,除非该函数(当然)期望 va_list 在变量参数部分。您的代码显然不会。



您需要明确接受 va_list 参数到 printDebugMsg (),因为那是你所经历的。参见例如 vprintf() 它解决了 printf()家族的这个问题。


I have an ethernet library for microcontroller (keil rl-tcpnet for lpc2478). Library uses va_list (standard pointer to arg array macro defined in stdarg.h) in debug output function in such a way:

void __debug__ (const char *fmt, ...)
{
  va_list args;
  va_start (args,fmt);
  vprintf (fmt,args);
  va_end (args);
}

However vprintf sends data to incorrect stream and I need to redirect debug output of library to correct serial port. Library has .c cofigure files and I write c++ code so I use wrapper function pointers to call c++ style code from c code. So I rewrite this function:

extern void (* printDebugMsg)(const char * fmt, ...);    
void __debug__ (const char *fmt, ...)
{
  va_list args;
  printDebugMsg(fmt, args);
}

other cpp file:

void printDebugMsgImplementation(const char * fmt, ...)
{
  va_list args;

  char buffer[100] = { 0 };

  sprintf(buffer, fmt, args);

  debugUart->write(buffer);
}
void (* printDebugMsg)(const char * fmt, ...) = &printDebugMsgImplementation;

The result debug output looks like:

TCP: Init 0 Sockets
IP :  Src. IP : 0.0.14.1668436768
ETH:  Dest.MAC: A1E05E10:A0002B44:5F9F:01:5FF8:33

The text and some numbers (like 0) seems to be correct but most other ones seems to be wrongly formatted (however it can be internal library problem also). I tried to rewrite my code using va_list as it is usually made in examples:

void __debug__ (const char *fmt, ...)
{
  va_list args;
  va_start (args,fmt);
  printDebugMsg(fmt, args);
  va_end (args);
}

and .cpp file:

void printDebugMsgImplementation(const char * fmt, ...)
{
  va_list args;

  char buffer[100] = { 0 };

  va_start(args, fmt);
  vsprintf(buffer, fmt, args);
  va_end(args);

  debugUart->write(buffer);
}

However the result is definetely worse that it was:

TCP: Init -1579131344 Sockets
ARP:  Dest.IP: -1579131400.-1610601474.24805.4
ARP:  Src.MAC: A1E05DF8:A0002BFE:60FF:04:616C:20

What I'm doing wrong? And how to deal with va_list in my case of wrapper functions calls?

解决方案

You can't pass va_list to a function expecting ..., unless that function (of course) expects a va_list in the variable-arguments part. Your code clearly does not.

You need to explicitly accept a va_list argument into printDebugMsg(), since that's what you're passing. See for instance vprintf() which solves this problem for the printf() family.

这篇关于如何在包装函数调用的顺序中正确使用va_list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆