调试过程可以把过程和状态记录到系统相关信息到文件中。
下面介绍了使用一个打印函数,可以直接拷贝到代码中使用:
头文件引用:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
函数定义:
void write_message_to_log(const char *fmt, ...)
{
char time_buff[256];
struct timeval tv;
gettimeofday(&tv, NULL);
sprintf(time_buff, "%d.%6.6d: ", (int)tv.tv_sec,(int)tv.tv_usec);
FILE *fp = NULL;
fp = fopen("/mnt/media/log_test", "a+");
if(fp != NULL)
{
char write_buff[256];
va_list args;
va_start(args, fmt);
vsprintf(write_buff, fmt, args);
va_end(args);
fputs(time_buff, fp);
fputs(write_buff, fp);
fclose(fp);
}
}
函数调用方式:
write_message_to_log("file:%s, line=%d", __FILE__, __LINE__);
调试打印函数
最新推荐文章于 2023-05-12 16:36:16 发布