存档

文章标签 ‘日志’

Linux 下 实现软件多功能日志(log)的记录。

2010年3月2日 没有评论

    前段时间调试万兆的项目,为了调试,发现错误,我们便直接使用了printf 打印到stdout上,大家都知道,Linux 终端显示的缓冲是有限的(默认的显示几百行),所以如果我们程序printf行数较多,或者程序运行时间较长,难免有一些记录被冲掉,进而影响查看bug。特别是对随机的、少重现错错误,想通过屏幕上printf出来的数据调试,就难免困难一些,当然我们也可以使用重定向实现

程序>log.txt

但是这种方式,打印到文件上便不能显示到屏幕上了,所以有一定的局限性。

于是写了一个程序用意实现。

改程序有以下几个文件

Debug.c

Debug.h

 

改打印log程序可以关闭和打开文件输出、屏幕输出详细说明如下:

       debug_term_on();//将打印信息输出到屏幕上

       debug_term_off();//不将打印信息输出到屏幕上

       debug_file_off();//不将打印信息输出到文件中

       debug_set_dir( “./log” )  ; //设置log文件夹

       debug_file_on();//将打印信息输出到文件中

 

输出例子:


#include <stdio.h>
#include "debug.c"

int main(int argc, char *argv[])
{
	/*--------------------初始化---------------------*/
	char* log_dir = "./log";//the dir
	unsigned int  log_terminal = 1;//debug on terminal: yes or not

/* init the debug info */
	if( log_terminal )
	{
		debug_term_on();
	}
	else
	{
		debug_term_off();
	}
	if( log_dir == NULL ){
		debug_file_off();
	}else{
		debug_set_dir( log_dir );
		debug_file_on();
	}

	/*-------------------使用测试---------------------*/
  DBG("I printf on stdout and logfile...\n");
  return 0;
}

代码下载