Alex

Faith First

常用链接

统计

资料

最新评论

自定义的一个Windows出错消息宏

 

#pragma comment( lib, "USER32" )

#include <crtdbg.h>
#define MTASSERT(a) _ASSERTE(a)


#define MTVERIFY(a) /*if (!(a))*/ PrintError(#a,__FILE__,__LINE__,GetLastError())

#define ERR_MESSAGE() PrintError("",__FILE__,__LINE__,GetLastError())

__inline void PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
{
    LPSTR lpBuffer;
    char errbuf[256] = {0};

#ifdef _WINDOWS
    char modulename[MAX_PATH];
#else // _WINDOWS

    DWORD numread;
#endif // _WINDOWS

   
    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,errnum,LANG_NEUTRAL,(LPTSTR)&lpBuffer,0,NULL);
   
    //wsprintf(errbuf, "\nThe following call failed at line %d in %s:\n\n"
        //" %s\n\nReason: %s\n", lineno, filename, linedesc, lpBuffer);
 wsprintf(errbuf,"´íÎó:%s",lpBuffer);

#ifdef _WINDOWS
    GetModuleFileName(NULL, modulename, MAX_PATH);
    MessageBox(NULL, errbuf, modulename, MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
#else
    WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, strlen(errbuf), &numread, FALSE );
    Sleep(3000);   
#endif

    exit(EXIT_FAILURE);
}

#include<fstream>

using namespace std;

#define SYS_LOG(var) PrintLog(__FILE__,__LINE__,var)

template <class T>
__inline void PrintLog(T var){
 ofstream out("log.txt",ios::app);
 if(!out)
  return;
 out<<var<<endl;
 out.close();
 return;
}

template <class T>
__inline void PrintLog(char *filename,int linenum,T var){
 ofstream out("log.txt",ios::app);
 if(!out)
  return;
 out<<filename<<endl<<linenum<<" : "<<var<<endl<<endl;
 out.close();
 return;
}

posted on 2009-04-30 17:34 alexhappy 阅读(260) 评论(0)  编辑 收藏 引用 所属分类: 调试技巧


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理