|
我写了一个用来作LOG文件的方法,输出文件是log.html,为什么这个类在MFC下面调试通过,在directdraw下却不能写进内容?
代码如下:
头文件:
#pragma once
const char* const LOG_FILE_NAME = "Log.html";
const char* const LOG_FLAG = "</FONT></BODY></HTML>";
const int LOG_OFFSET = -21; //文件末尾21个字符,也就是-- //"</FONT></BODY></HTML>"
//html内容
const char* const LOG_CONTENT = "<HTML><HEAD><TITLE>Log Message</TITLE></HEAD>\
<BODY><Font COLOR=\"#405090\"><B>LOG<HR COLOR=\"#405090\">START ! ......<BR><BR>\
</FONT></BODY></HTML>";
class CLog
{
public:
CLog(void);
~CLog(void);
static int OutPutToLog(const char* const p_StrMsg,bool p_IsErrMsg);
};
CPP文件:
#include ".\log.h"
CLog::CLog(void)
{
}
CLog::~CLog(void)
{
}
//
// p_IsErrMsg为true时,输出红色字体,表示错误信息
//
int CLog::OutPutToLog(const char* const p_StrMsg,bool p_IsErrMsg)
{
int fh, nbytes; //fh : file handle
//nbytes : file length
time_t ltime;
ltime = time(NULL);
//=====1: Get the FileLength====================
fh = _open(LOG_FILE_NAME, _O_TEXT | _O_CREAT | _O_WRONLY);
//O_RDONLY : if the file is not exit,create the file
if( fh == -1 )
{
_close(fh);
return -1;
}
nbytes= _filelength(fh);
//if new file input the content
if (!(nbytes>0))
{
_write(fh,LOG_CONTENT,(int)strlen(LOG_CONTENT));
}
_close(fh);
//2 pen again and input the log message
fh = _open(LOG_FILE_NAME, _O_TEXT | _O_WRONLY);
if( fh == -1 )
{
_close(fh);
return -1;
}
_lseek(fh,LOG_OFFSET,SEEK_END);
if(p_IsErrMsg)
{
_write(fh,"<Font color = red>",18);
//_write(fh,strTime,(int)strlen(strTime));
_write(fh,ctime(&ltime),(int)strlen(ctime(&ltime)));
_write(fh," : ",3);
_write(fh,p_StrMsg,(int)strlen(p_StrMsg));
_write(fh,"<BR></font>",11);
_write(fh,LOG_FLAG,(int)strlen(LOG_FLAG));
}
else
{
//_write(fh,strTime,(int)strlen(strTime));
_write(fh,ctime(&ltime),(int)strlen(ctime(&ltime)));
_write(fh," : ",3);
_write(fh,p_StrMsg,(int)strlen(p_StrMsg));
_write(fh,"<BR>",4);
_write(fh,LOG_FLAG,(int)strlen(LOG_FLAG));
}
_close(fh);
return 0;
}
|
|