http://www.amobbs.com/thread-5618306-1-1.html
我来发个代码,这个代码可以通过串口输出函数名以及行号。方便通过串口输出信号,查看哪里出现问题。
代码如下:static char output[256]={0};
void dbg_print_log(const char *msg,
const char *pccFunc,
const int ciLine,
const char *pccFmt,...)
{
va_list arglst;
va_start(arglst,pccFmt);
vsnprintf(output,sizeof(output),pccFmt,arglst);
printf("[%s][%s():%04d] %s\n",msg, pccFunc, ciLine, output);
va_end(arglst);
}
void dbg_print_buf(const char *msg, ...)
{
UINT16 i;
UINT8* buf;
UINT32 cnt;
va_list arglst;
va_start(arglst, msg);
buf = va_arg( arglst, UINT8*);
cnt = va_arg( arglst, UINT32);
printf("[%s][L:%d] ", msg,cnt);
for(i=0;i<cnt;i++)
{
printf("0x%02X,",buf[i]);
}
printf("\n");
va_end(arglst);
}
头文件:
void dbg_print_buf(const char *msg, ...);
void dbg_print_log(const char *msg,
const char *pccFunc,
const int ciLine,
const char *pccFmt,...);
#define DBG_INFO(...) dbg_print_log("INFO", __FUNCTION__, __LINE__, __VA_ARGS__)
#define DBG_BUF(...) dbg_print_buf("BUFF",__VA_ARGS__)
比如输出:
[INFO][function():1100] hello world!
function为函数名,1100为log输出的行号。
这个代码超有用,以后出问题也不用死死的去代码里找了