关闭特定函数的 DEBUG 宏 (NDEBUG)
我正在使用以下宏来打印我在网上找到的调试信息。效果很好。 但是,我想在调试调用函数 A 的函数 B 时关闭函数 A 的调试打印。我尝试了 #define NDEBUG
function A
#undef NDEBUG
但未能成功抑制函数 A 中的打印。
任何帮助将不胜感激。 也欢迎任何关于完成任务的替代方法的建议。
谢谢~RT
#ifdef NDEBUG
/*
If not debugging, DEBUGPRINT NOTHING.
*/
#define DEBUGPRINT2(...)
#define DEBUGPRINT(_fmt,G ...)
#else
/*
Debugging enabled:
*/
#define WHERESTR "[file %s, line %d]: "
#define WHEREARG __FILE__, __LINE__
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
#endif /* NDEBUG */
I am using the following macro for printing debug information that I found on the web. It works great.
However, I would like to turn-off debug printing for function A when debugging function B, which calls function A. I tried #define NDEBUG
function A
#undef NDEBUG
but haven't managed to suppress printing in function A.
Any help will be greatly appreciated.
Any suggestions for alternative ways of accomplishing the task is also welcome.
Thanks ~RT
#ifdef NDEBUG
/*
If not debugging, DEBUGPRINT NOTHING.
*/
#define DEBUGPRINT2(...)
#define DEBUGPRINT(_fmt,G ...)
#else
/*
Debugging enabled:
*/
#define WHERESTR "[file %s, line %d]: "
#define WHEREARG __FILE__, __LINE__
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)
#endif /* NDEBUG */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该将跟踪包装到一个模块中,以便您可以在运行时动态打开/关闭跟踪,这样您就可以专门针对函数调用将其关闭。在发布模式下,您可以用空语句替换所有跟踪,但根据我的经验,我发现在发布模式下保持跟踪也很好 - 以防万一。
maybe you should wrap the trace into a module so that you can turn on/off the tracing dynamically in run-time and in that way you can specifically turn it off for a function call. In release mode you could replace all tracing with empty statements although in my experience I find it good to keep tracing in release mode as well - just in case.
NDEBUG
在包含assert.h
时很有用,因此#define NDEBUG
/#undef NDEBUG
稍后将不做任何事。不过,您可以这样做:
然后,在函数 A() 中:
当从
A()
之外的任何地方调用B()
时,都会对其进行调试。要进行调试,您需要定义MY_DEBUG
并取消定义NDEBUG
。编辑:当您想要通过调试进行编译时,您需要定义
MY_DEBUG
,但希望您使用make
或其他构建工具,所以这应该很容易。NDEBUG
is useful at the timeassert.h
is included, so#define NDEBUG
/#undef NDEBUG
later will not do anything.You can do something like this though:
Then, in function A():
This will debug
B()
when it's called from anywhere except fromA()
. To get debugging, you will needMY_DEBUG
to be defined andNDEBUG
to be undefined.Edit: You will need to define
MY_DEBUG
when you want to compile with debugging, but hopefully you're usingmake
or some other build tool, so this should be easy.