Chinaunix首页 | 论坛 | 博客
  • 博客访问: 945675
  • 博文数量: 116
  • 博客积分: 3923
  • 博客等级: 中校
  • 技术积分: 1337
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 01:22
文章分类

全部博文(116)

文章存档

2013年(1)

2012年(17)

2011年(69)

2009年(29)

分类: WINDOWS

2011-08-23 00:58:05

http://vincent.blog.chinaunix.net
想不到今天实现assert函数也搞了我这么久。。。郁闷。。。

file: assertx.h
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  * history:
  4.  * initial @ 2011-08-23
  5.  */

  6. #ifndef _ASSERTX_H_
  7. #define _ASSERTX_H_

  8. #include <stdio.h>
  9. #include <stdarg.h>

  10. #define OUTPUT_WHERE OUTPUT_SCREEN

  11. /* where to output */
  12. #define OUTPUT_SCREEN    1
  13. #define OUTPUT_FILE        2

  14. /* halt on */
  15. #ifndef unreachable
  16. #define unreachable() do {} while (1)
  17. #endif

  18. /* Microsoft VC TOOLS VERSION DEFINITION
  19.  * MS VC++ 10.0 _MSC_VER = 1600
  20.  * MS VC++ 9.0 _MSC_VER = 1500
  21.  * MS VC++ 8.0 _MSC_VER = 1400
  22.  * MS VC++ 7.1 _MSC_VER = 1310
  23.  * MS VC++ 7.0 _MSC_VER = 1300
  24.  * MS VC++ 6.0 _MSC_VER = 1200
  25.  * MS VC++ 5.0 _MSC_VER = 1100
  26.  */

  27. #if defined(WIN32)&&(_MSC_VER < 1300)

  28.     #define __func__ "?"
  29.     /* you need define outputx itself under VC6 */
  30.     extern void outputx(const char* fmt,...);
  31.     #if OUTPUT_WHERE!=OUTPUT_SCREEN && OUTPUT_WHERE!=OUTPUT_FILE
  32.     /* no output */
  33.     #define outputx(fmt,...)
  34.     #endif

  35. #else

  36.     #define __func__ __FUNCTION__
  37.     /* output to screen */
  38.     #if OUTPUT_WHERE==OUTPUT_SCREEN
  39.     #define outputx(fmt,...) \
  40.     do { \
  41.         printf(##fmt, ##__VA_ARGS__); \
  42.     }while(0)
  43.     /* output to file */
  44.     #elif OUTPUT_WHERE==OUTPUT_FILE
  45.     extern void outputx(const char* fmt,...);        
  46.     #else
  47.     /* no output */
  48.     #define outputx(fmt,...)
  49.     #endif

  50. #endif

  51. /* go on the next step after assert(0) */
  52. #define assertx_goon(expr) \
  53. do { \
  54.     if (!(expr)) { \
  55.         outputx("Assertion failed! %s, %s, %s, line %d\n", \
  56.                         #expr, __FILE__, __func__, __LINE__); \
  57.     } \
  58. }while(0)

  59. /* halt after assert(0) */
  60. #define assertx_halt(expr) \
  61. do { \
  62.     if (!(expr)) { \
  63.     outputx("Assertion failed! %s, %s, %s, line %d\n", \
  64.         #expr, __FILE__, __func__, __LINE__); \
  65.     unreachable();\
  66.     } \
  67. }while(0)


  68. #endif

file: main.c
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  * history:
  4.  * initial @ 2011-08-23
  5.  */

  6. #include "assertx.h"

  7. #ifdef WIN32
  8. #define vsnprintf _vsnprintf
  9. #endif

  10. #define MAX_OUTPUT 100

  11. void outputx(const char* fmt,...)
  12. {
  13.     int ret;
  14.     char buf[MAX_OUTPUT];
  15.     va_list args;
  16.     va_start(args, fmt);
  17.     ret = vsnprintf(buf, sizeof(buf), fmt, args);
  18.     buf[MAX_OUTPUT-1]=0;
  19.     if(ret<0){
  20.         printf("warning: output has been truncated!");
  21.     }
  22.     printf ("%s", buf);
  23.     va_end(args);
  24. }

  25. int main(int argc, char* argv[])
  26. {
  27.     assertx_goon(0);
  28.     assertx_halt(0);
  29.     return 0;
  30. }


附件: assertx.rar  
阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~