找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5133|回复: 0

一个支持自动抛出异常的宏

[复制链接]
发表于 2011-2-10 15:41:09 | 显示全部楼层 |阅读模式
写了一个简单的错误捕捉的宏。用于简单的错误排查,这个类会自动输出当前出错的文件名,函数名,感觉在DEBUG的时候挺有用的。在这里记录一下,很简单的一个.h,使用很简单。windows和linux下都能运行。
建立一个define.h
  1. #ifndef _DEFIINE_H
  2. #define _DEFIINE_H
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <time.h>
  8. #define SHOW_PRINT    1
  9. #define SHOW_WRITELOG 2
  10. //nType = 1为显示在桌面上,2为记录为日志
  11. void __show__(const char* pMsg, int nType)
  12. {
  13.         if(nType == 1)
  14.         {
  15.                 printf("[Error]%s", pMsg);
  16.         }
  17.         if(nType == 2)
  18.         {
  19.                 FILE* pFile = fopen("assert.log", "a");
  20.                 if(pFile != NULL)
  21.                 {
  22.                         fwrite(pMsg, 1, strlen(pMsg), pFile);
  23.                         fwrite("\r\n", 1, 2, pFile);
  24.                         fclose(pFile);
  25.                 }
  26.                 else
  27.                 {
  28.                         printf("[Error]file open Error(%s).\n", strerror(errno));
  29.                 }
  30.         }
  31. }
  32. void sprintf_safe(char* szText, int nLen, char* fmt ...)
  33. {
  34.         if(szText == NULL)
  35.         {
  36.                 return;
  37.         }
  38.         va_list ap;
  39.         va_start(ap, fmt);
  40.         vsnprintf(szText, nLen, fmt, ap);
  41.         va_end(ap);
  42. }
  43. void __assertspecial__(const char* pFile, int nLen, const char* pFuncName, const char* pExpr, const char* pMsg)
  44. {
  45.         char szTemp[1024] = {'\0'};
  46.         char szDate[50] = {'\0'};
  47.         struct tm *local;
  48.         time_t t;
  49.         t = time(NULL);
  50.         local = localtime(&t);
  51.         sprintf_safe(szDate, 50, "%04d-%02d-%02d %02d:%02d:%02d", local->tm_year + 1900, local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec);
  52.         sprintf_safe(szTemp, 1024, "[%s](%s)(%d)(%s)(%s)(%s)\n", szDate, pFile, nLen, pFuncName, pExpr, pMsg);
  53.         __show__(szTemp, SHOW_WRITELOG);
  54. }
  55. #ifdef WIN32
  56.         #define AssertSpecial(type, msg) {if(type == false) { __assertspecial__(__FILE__, __LINE__, __FUNCTION__, #type, msg);} }
  57. #else
  58.         #define AssertSpecial(type, msg) {if(type == false) { __assertspecial__(__FILE__, __LINE__, __PRETTY_FUNCTION__, #type, msg);} }
  59. #endif
  60. #define _ENTER_FUNCTION {try{
  61. #ifdef WIN32
  62.         #define _LEAVE_FUNCTION }catch(...) { AssertSpecial(false, __FUNCTION__); }}
  63. #else
  64.         #define _LEAVE_FUNCTION }catch(...) { AssertSpecial(false, __PRETTY_FUNCTION__); }}
  65. #endif
  66. #endif
复制代码


测试一下:
  1. int main(int argc, char* argv[])
  2. {
  3. printf("[main]Begin.\n");
  4. _ENTER_FUNCTION
  5. throw 1;
  6. _LEAVE_FUNCTION
  7. return 0;
  8. }
复制代码

输出日志:
[2011-02-10 15:32:49](testerror.cpp)(14)(main)(false)(main)

其实这个可以稍微再改一下,支持一下msg,不过对于不复杂的错误检测,差不多够了,有助于查错。
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-18 18:19 , Processed in 0.061617 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表