清理调试输出功能?
我想编写一个函数,它将在我的程序中打印出错误消息/警告以及文件和文件。行号。 C中有这两个宏:
__FILE__
__LINE__
但是我认为有一个问题...当我编写这样的函数时:
#include <stdio.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
void error(const char *location, const char *msg)
{
#ifdef DEBUG
printf("Error at %s: %s\n", location, msg);
#endif
}
int main(int , char**)
{
error(AT, "fake error");
return 0;
}
仍然有很多无用的函数调用&二进制文件中的垃圾(每次调用时 __FILE__
和 __LINE__
的值),即使我为发布版本取消定义 DEBUG
也是如此。 那么我怎样才能更优雅地完成这个任务呢?我想要这样的东西:
#include <stdio.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
void error(const char *location, const char *msg)
{
printf("Error at %s: %s\n", location, msg);
}
int main(int , char**)
{
#ifdef DEBUG
error(AT, "fake error");
#endif
return 0;
}
但不要在每次函数调用之前和之后编写 #ifdef DEBUG
和 #endif
- 这对于这样的任务来说太大了。并删除每个 error(AT, "fake error");
手动调用也不是很优雅......
有什么想法吗?也许内联该函数(不会有帮助,不是吗)?一些宏或这个结构的改变?
I want to write an function that will print out error messages/warnings in my program together with the file & line number. There are these two macros in C:
__FILE__
__LINE__
but there's a problem in my opinion... When I'm writing a function like this:
#include <stdio.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
void error(const char *location, const char *msg)
{
#ifdef DEBUG
printf("Error at %s: %s\n", location, msg);
#endif
}
int main(int , char**)
{
error(AT, "fake error");
return 0;
}
There are still a lot of useless function calls & trash (the values of __FILE__
and __LINE__
at every call) in the binary file, even if I undefine DEBUG
for the release build.
So how can I accomplish this more elegant? I want something like this:
#include <stdio.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
void error(const char *location, const char *msg)
{
printf("Error at %s: %s\n", location, msg);
}
int main(int , char**)
{
#ifdef DEBUG
error(AT, "fake error");
#endif
return 0;
}
But not writing #ifdef DEBUG
and #endif
before and after every function call - that would be too huge for such an task. And remove every error(AT, "fake error");
call manually isn't really elegant as well...
Any ideas? Maybe inline the function (would not help, wouldn't it)? Some macro or an change of this construct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将
error
变成一个宏:然后在你的函数中编写
当然,你也可以简化 ERROR 并去掉
AT
作为第一个参数,直接指定这些信息在宏定义中。Just turn
error
into a macro:then, in your functions, write
Of course, you could also simplify ERROR and get rid of
AT
as the first parameter, directly specifying this information in the macro definition.将
error()
包装在宏中确实是正确的方法 - 我会这样写:Wrapping
error()
in a macro is indeed the right way to go - I'd write it like so:您可以使用此可变参数宏来模拟
printf
,它会使用所需的输出写入文件和行号。编辑:根据 jcsalomon 的建议修改了 eprintf
you can use this variadic macro to mimic
printf
which writes file and line no with desired out put.edit : modified eprintf from the suggestions of jcsalomon
将错误替换为#define,
例如:
您还可以将 AT 移至错误中,而不是每次都传递它
replace error with #define
something like:
You could also move AT into error instead of passing it each time