免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7768 | 回复: 8
打印 上一主题 下一主题

[函数] 如何编写debug,和trace函数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-22 12:38 |只看该作者 |倒序浏览
debug(format...);
trace();
debug函数是用来调试的函数,可以通过宏定义在编译是去掉。Trace函数是跟踪函数没有参数。打印当前的时间、文件名、函数名、行号
要实现以上饿功能的

论坛徽章:
0
2 [报告]
发表于 2009-08-22 12:44 |只看该作者
trace函数不大可能吧。

论坛徽章:
0
3 [报告]
发表于 2009-08-22 12:49 |只看该作者
可以
需要定义成宏

论坛徽章:
0
4 [报告]
发表于 2009-08-22 12:50 |只看该作者
自己想想啥  这也不是什么难题^_^



  1. #ifdef DEBUG_ON
  2.   #define DEBUG(fmt, args...) fprintf(stderr, fmt, ##args)
  3. #else
  4.   #define DEBUG(fmt, args...)
  5. #endif        
  6.   
  7. #ifdef DEBUG_ON
  8.   #define TRACE() fprintf(stderr,"%s\t%s\t%d\t%s\n",__FILE__,__FUNCTION__,__LINE__,__TIME__)
  9. #else
  10.   #define TRACE()
  11. #endif   
复制代码


我这里是宏,你也可以用函数

论坛徽章:
0
5 [报告]
发表于 2009-08-22 12:50 |只看该作者
哦 还有个函数名 这个就不知道怎么实现了 

不过以前看到CU有人搞了这个

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
6 [报告]
发表于 2009-08-22 12:55 |只看该作者

回复 #2 emacsnw 的帖子

哈哈   我也想复杂了

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
7 [报告]
发表于 2009-08-22 12:58 |只看该作者

回复 #4 ubuntuer 的帖子

原帖由 ubuntuer 于 2009-8-22 12:50 发表
#define TRACE() fprintf(stderr,"%s\t%s\t%d\t%s\n",__FILE__,__FUNCTION__,__LINE__,__TIME__)
我这里是宏,你也可以用函数


不能用函数吧?

论坛徽章:
0
8 [报告]
发表于 2009-08-22 13:52 |只看该作者

回复 #4 ubuntuer 的帖子

恩,很适用,刚才我也去问了一下老师,也是用宏

论坛徽章:
0
9 [报告]
发表于 2009-08-22 13:53 |只看该作者
#include <stdio.h>
#include <termio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include  <sys/times.h>

//#define CONSOLE
/* the debug information will print on console */
#ifdef   CONSOLE
#define DBFILE  "/dev/console"
#define OPEN_FLAG "w"
#else
/* the debug information will write into file DBFILE */
#define DBFILE  "./DB"
//#define DBFILE  STDOUT
#define OPEN_FLAG "a+"
#endif



//Wed May 21 20:12:33 2008
#define DB(form_str,myargs...) \
        do{\
                if (1)\
                { \
                        char _buf[2048]={0};\
                        char _str_time[512];\
                        FILE *fFd=NULL;\
                        time_t time_file;\
                        time (&time_file); \
                        sscanf(ctime(&time_file),"%*s %*s %*s %s %*s",_str_time);\
                        snprintf (_buf,2048-1,form_str,##myargs);\
                        if(_buf[strlen(_buf)-1] == '\n') _buf[strlen(_buf)-1] ='\0';\
                        fFd=fopen(DBFILE,OPEN_FLAG); \
                        if ( fFd!=NULL )\
                        {\
                                fprintf(fFd,"%s: %s<== %s %s:%d\n",_str_time,_buf,__FILE__, __FUNCTION__,__LINE__);\
                                fclose(fFd);\
                                fFd=NULL;\
                        } \
                }\
        }while(0)
       
#define DB1() \
        do{\
                        char _str_time[512];\
                        FILE *fFd=NULL;\
                        time_t time_file;\
                        time (&time_file); \
                        sscanf(ctime(&time_file),"%*s %*s %*s %s %*s",_str_time);\
                        fFd=fopen(DBFILE,OPEN_FLAG); \
                        if ( fFd!=NULL )\
                        {\
                                fprintf(fFd,"%s: %s %s:%d\n",_str_time,__FILE__, __FUNCTION__,__LINE__);\
                                fclose(fFd);\
                                fFd=NULL;\
                        }\
        }while(0)



int main()
{
               
        DB("test!");
        DB1();
        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP