Archive

Posts Tagged ‘Develop’

simple logging in C programming

March 18th, 2009 BianJiang No comments
近来在调试C的时候,经常要打印些调试信息,如果都统一使用printf来调试,有些太累了, 如果调试完成还要去掉, 就写了个简单的宏来进行定义, 主要的代码如下:
[code lang="c"]
#include

#undef PDEBUG             /* undef it, just in case */
#ifdef SCULL_DEBUG
#   define PDEBUG(fmt, args…) printf(”[%s-%s:%d]-[Debug] ” fmt “\n”, __FILE__, __func__, __LINE__, ## args)
#else
#   define PDEBUG(fmt, args…) /* not debugging: nothing */
#endif

int main(void)
{
char * val = “wifihack.net”;
PDEBUG(”Hello, %s”, val);
return 0;
}
[/code]

为了减少修改头文件,我们可以通过Makefile来控制Debug信息的输出:

[code lang="c"]
# Comment/uncomment the following line to disable/enable debugging
DEBUG = y

# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # “-O” is needed to expand inlines
else
DEBFLAGS = -O2
endif

CFLAGS += $(DEBFLAGS)

ALL:
gcc $(CFLAGS) -o hello hello.c
[/code]

参考: http://www.makelinux.net/ldd3/chp-4-sect-2.shtml


Bian Jiang

Categories: Tech.Notes Tags: , , ,