C++写错误日志函数
来源:互联网 发布:淘宝入驻费用多少钱 编辑:程序博客网 时间:2024/04/28 06:09
在编写程序,有些问题借助错误日志更好跟踪。
UNICODE代码:
DWORD WriteErrLog ( CString strError)
{
DWORD nWrite=0, bytesWritten;
HANDLE hFile;
BOOL res;
CString strErrorLog, strTime ,strLogName;
SYSTEMTIME dt;//错误信息 +日期
GetLocalTime(&dt);
strTime.Format(" %04d/%02d/%02d %02d:%02d:%02d\r\n",dt.wYear, dt.wMonth, dt.wDay, dt.wHour, dt.wMinute, dt.wSecond );
TCHAR tstrCurrentPath[MAX_PATH];
TCHAR g_pInfPath[MAX_PATH];
memset(tstrCurrentPath, 0, MAX_PATH);
memset(g_pInfPath, 0, MAX_PATH);
//Get
GetCurrentDirectory(MAX_PATH, tstrCurrentPath);
CString strPath;
strPath.Format(_T("%s"),tstrCurrentPath);
strcpy(g_pInfPath,tstrCurrentPath);
strcat(g_pInfPath,_T("\\错误日志.ini"));
strLogName= g_pInfPath;
hFile = CreateFile( strLogName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ) ;
if( ERROR_ALREADY_EXISTS == GetLastError() )
{
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_END);
if ( INVALID_SET_FILE_POINTER == dwPtr )
{
CloseHandle(hFile);
return -1;
}
strErrorLog=strError+strTime;
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(WCHAR),&nWrite,NULL);
}
else if( 0x0 == GetLastError() )
{
//LPCVOID
BYTE head[2];
head[0] = 0xff;
head[1] = 0xfe;
//Unicode 格式文本,需要写入文件头两个字节 0xff 0xfe
WriteFile(hFile, (BYTE*)head, 2, &bytesWritten, NULL);
//再写入内容
DWORD lDistance =2;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
if ( INVALID_SET_FILE_POINTER == dwPtr )
{
CloseHandle(hFile);
return -1;
}
strErrorLog=strError+strTime;
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(WCHAR),&nWrite,NULL);
}
CloseHandle(hFile);
return (DWORD)res;
}
多字节代码
DWORD WriteErrLog ( CString strError)
{
DWORD nWrite=0, bytesWritten;
HANDLE hFile;
BOOL res;
CString strErrorLog, strTime ,strLogName;
SYSTEMTIME dt;//错误信息 +日期
GetLocalTime(&dt);
strTime.Format(" %04d/%02d/%02d %02d:%02d:%02d\r\n",dt.wYear, dt.wMonth, dt.wDay, dt.wHour, dt.wMinute, dt.wSecond );
TCHAR tstrCurrentPath[MAX_PATH];
TCHAR g_pInfPath[MAX_PATH];
memset(tstrCurrentPath, 0, MAX_PATH);
memset(g_pInfPath, 0, MAX_PATH);
//Get
GetCurrentDirectory(MAX_PATH, tstrCurrentPath);
CString strPath;
strPath.Format(_T("%s"),tstrCurrentPath);
strcpy(g_pInfPath,tstrCurrentPath);
strcat(g_pInfPath,_T("\\错误日志.ini"));
strLogName= g_pInfPath;
hFile = CreateFile( strLogName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ) ;
if( ERROR_ALREADY_EXISTS == GetLastError() )
{
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_END);
if ( INVALID_SET_FILE_POINTER == dwPtr )
{
CloseHandle(hFile);
return -1;
}
strErrorLog=strError+strTime;
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(CHAR),&nWrite,NULL);
}
else if( 0x0 == GetLastError() )
{
//再写入内容
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
if ( INVALID_SET_FILE_POINTER == dwPtr )
{
CloseHandle(hFile);
return -1;
}
strErrorLog=strError+strTime;
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(CHAR),&nWrite,NULL);
}
CloseHandle(hFile);
return (DWORD)res;
}
- C++写错误日志函数
- C语言简单写日志函数
- php写错误日志
- Silverlight写错误日志
- Linux C语言自己动手写日志生成函数
- 写日志函数
- shell 写日志函数
- C++写日志函数
- C写日志文件
- 【C#】写日志方法
- 系统错误日志函数
- C++使用c写的函数,出现连接错误.
- 日志文件系统的写日志函数
- 自己写的日志函数
- Windows 写系统日志函数
- C/C++写日志代码
- C语言写 系统日志
- 【C++】添加写日志类
- oracle 查看执行计划方法(SQL developer,sqlplus)
- Android快速开发系列 10个常用工具类
- su普通用户切换root用户失败
- jQuery 取值、赋值的基本方法整理
- 一个开发者账号,多台MAC同时使用
- C++写错误日志函数
- IOS添加pch全局引用文件(宏定义)
- 机场三字代码和四字代码的资料汇编
- python 字符编码
- Mysql 从零开始(五)函数之字符串函数
- 使用对象集合(通过Foundation框架使用OC处理字典,集合)
- VLFeat + VS2013+opencv 配置
- Multiple ways to access QEMU Machine Protocol (QMP)
- Java实现多线程多节点下载