重新定义fprintf以进行调试 [英] Redefine fprintf for debugging purposes

查看:88
本文介绍了重新定义fprintf以进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想重新定义fprintf以进行调试。那就是我想要那个

所有输出到stdout的输出应该记录在

文件中。


我试过像

#define fprintf(x,y,z)my_fprintf(x,y,z)


和编译器给出了大约100个错误。任何人都可以帮我这个。

我甚至尝试使用可变参数宏,但它似乎太复杂了。

任何人都可以帮助这样的定义。
< br $>
谢谢和问候,

Prayag Narula

解决方案

Prayag Narula写道:





我想重新定义fprintf以进行调试。那就是我想要那个

所有输出到stdout的输出应该记录在

文件中。



< snip>


您无法轻松重新定义标准库函数和符号。 fprintf

采用FILE *参数。为什么你觉得必须是stdout。只需传递它

a FILE *设置为一个文件,大概是由fopen打开的。


FILE * log;

if(( log = fopen(" filename"," a"))== NULL){deal_with_error(); }

/ * ... * /

if(fprintf(log," format string",...)< 0){deal_with_error(); }


7月23日下午1:23,Prayag Narula< prayag.nar ... @ gmail.comwrote:


#define fprintf(x,y,z)my_fprintf(x,y,z)



如果fprintf和amp;之间有空格(x,y,z)可能是这些错误的原因

。你需要没有空格fprintf(x,y,z)。

更好的是#define fprintf my_fprintf"如果xyz保持不变

我觉得这是不太可能的,因为你会用你的文件指针替换

stdout。


我觉得在你的场景中使用variadic宏更好,因为fprintf

有可变参数。我要感谢你向我介绍这个

概念,因为很长一段时间都在寻找这样的东西.. :)


< blockquote>

" Prayag Narula" < pr *********** @ gmail.comwrote in message

news:11 ******************* ***@i38g2000prf.googlegr oups.com ...





我想重新定义fprintf进行调试目的。那就是我想要那个

所有输出到stdout的输出应该记录在

文件中。


我试过像


#define fprintf(x,y,z)my_fprintf(x,y,z)


和编译器给出的100个错误。任何人都可以帮我这个。

我甚至尝试使用可变参数宏,但它似乎太复杂了。

任何人都可以帮助这样的定义。



如果你对stdout的所有打印都是用:

fprintf(stdout,...);

(即,使用fprintf而不是printf)

那么它真的很简单:


#ifdef DEBUG

#define OUTFILE myfile

#else

#define OUTFILE stdout

#endif


然后所有打印语句变为:

fprintf(OUTFILE,...);


确保在

执行任何fprintf语句之前成功打开myfile。

-

Fred L. Kleinschmidt

波音助理技术研究员

航空稳定性和控制计算


Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.

I tried something like
#define fprintf (x,y,z) my_fprintf(x,y,z)

and the compiler gave about 100 errors. Can anyone help me with this.
I have even tried using variadic macros but it seems too complicated.
Can anyone just help with such a defination.

Thanks and Regards,
Prayag Narula

解决方案

Prayag Narula wrote:

Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.

<snip>

You cannot portably redefine Standard library functions and symbols. fprintf
takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.

FILE *log;
if((log = fopen("filename", "a")) == NULL) { deal_with_error(); }
/* ... */
if(fprintf(log, "format string", ...) < 0) { deal_with_error(); }


On Jul 23, 1:23 pm, Prayag Narula <prayag.nar...@gmail.comwrote:

#define fprintf (x,y,z) my_fprintf(x,y,z)

If there is space between fprintf & (x,y,z) that could be the reason
for those errors. You need to have it without space "fprintf(x,y,z)".
Even better would be "#define fprintf my_fprintf" if x y z remain same
which I think is highly unlikely as u would be changing replacing
stdout with your file pointer.

I feel using variadic macros fit better in your scenario as fprintf
has variable arguments. I would to thank you to introduce me this
concept as was looking for something like this for a long time.. :)



"Prayag Narula" <pr***********@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...

Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.

I tried something like
#define fprintf (x,y,z) my_fprintf(x,y,z)

and the compiler gave about 100 errors. Can anyone help me with this.
I have even tried using variadic macros but it seems too complicated.
Can anyone just help with such a defination.

If all of your printing to stdout is done with:
fprintf( stdout, ...);
(that is, using fprintf rather than printf)
then it is really easy:

#ifdef DEBUG
#define OUTFILE myfile
#else
#define OUTFILE stdout
#endif

Then all print statements become:
fprintf( OUTFILE, ... );

Make sure you successfully open myfile before
any of the fprintf statements are executed.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


这篇关于重新定义fprintf以进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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