⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 journal.cpp

📁 此程序是利用VC实现了USB的驱动程序。
💻 CPP
字号:
// Journal.cpp: implementation of the CJournal class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Journal.h"
#include <mmsystem.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CJournal::CJournal()
{
	logbuf=(char*)malloc(1024*200);	//200K 缓冲
	if(logbuf==NULL)
	{
		::MessageBox(NULL,"内存不足","严重错误",MB_OK|MB_ICONHAND);
	}else
	memset(logbuf,0,1024*20);
}

CJournal::~CJournal()
{
	if(logbuf!=NULL)
	{
		logfile.SeekToEnd ();
		strcat(logbuf,"\r\n");
		logfile.Write (logbuf,strlen(logbuf));
		free(logbuf);
	}
	CloseFile();
}

BOOL CJournal::StartRecord(LPTSTR szFilePath)
{
	if(!logfile) 
	{
		CloseFile();
	}
	CFileException e;
	if( !logfile.Open (szFilePath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite, &e))
	{
		MessageBox(NULL,"无法打开记录文件","错误",MB_ICONERROR);
		return FALSE;
	}
	memset(LogFileName,0,MAX_PATH);
	strcpy(LogFileName,szFilePath);
	SYSTEMTIME sTime;
	GetLocalTime(&sTime);
	CString buf;
	buf.Format ("\r\nStart Log at %d//%d//%d %d:%d:%d XTime:%X\r\n\r\n",sTime.wYear ,sTime.wMonth ,
		sTime.wDay ,sTime.wHour ,sTime.wMinute ,sTime.wSecond , timeGetTime() );
	logfile.SeekToEnd ();
	logfile.Write (buf,buf.GetLength ());
	return TRUE;
}

BOOL bJournalLoging=FALSE;

BOOL CJournal::AddJournalLog(LPTSTR szLog)
{
	if(logbuf==NULL) return FALSE;
	while(bJournalLoging){Sleep(1);}
	bJournalLoging=TRUE;
	if(strlen(logbuf)+strlen(szLog)>204000)	//200K 缓冲
	{
		logfile.SeekToEnd ();
		strcat(logbuf,"\r\n");
		logfile.Write (logbuf,strlen(logbuf));
		*logbuf =0;
		if(logfile.GetLength()>0x800000)	//当记录文件大于8M时,更换记录文件。
		{
			if(ChangLogFile()==FALSE)
				return FALSE;
		}
	}
	sprintf(logtimebuf,"%X: ",timeGetTime());
	strcat(logbuf,logtimebuf);
	strcat(logbuf,szLog);
	strcat(logbuf,"\r\n");
	bJournalLoging=FALSE;
	return TRUE;
}

void CJournal::CloseFile()
{
	SYSTEMTIME sTime;
	GetLocalTime(&sTime);
	CString buf;
	buf.Format ("\r\nClose Log at %d//%d//%d %d:%d:%d XTime:%X\r\n\0",sTime.wYear ,sTime.wMonth ,
		sTime.wDay ,sTime.wHour ,sTime.wMinute ,sTime.wSecond , timeGetTime() );
	logfile.SeekToEnd ();
	logfile.Write (buf,buf.GetLength ());
	logfile.Close ();
}

BOOL CJournal::ChangLogFile()
{
	logfile.Close ();
	SYSTEMTIME sTime;
	GetLocalTime(&sTime);
	char fnbuf[MAX_PATH];
	char renbuf[MAX_PATH];
	int filenumber=0;
	while(1)
	{
		if(filenumber>100)
		{MessageBox(NULL,"记录文件过多。无法创建新文件!","错误",MB_OK);return FALSE;}
		memset(fnbuf,0,MAX_PATH);
		sprintf (fnbuf,"%.2d%d%.2d_%d",sTime.wYear-2000,sTime.wMonth,sTime.wDay,filenumber++);
		memset(renbuf,0,MAX_PATH);
		strcpy(renbuf,LogFileName);
		char* tempchr=strchr(renbuf,'.');
		memcpy(tempchr+strlen(fnbuf),tempchr,strlen(tempchr));
		memcpy(tempchr,fnbuf,strlen(fnbuf));
		CFile f;
		CFileException e;
		if(!f.Open (renbuf, CFile::modeWrite,&e))
		{
			if( e.m_cause == CFileException::fileNotFound )
			{
				break;
			}
		}
		f.Close ();
	}
	CFile::Rename(LogFileName,renbuf);
	memset(renbuf,0,MAX_PATH);
	strcpy(renbuf,LogFileName);
	if(!StartRecord(renbuf))
		return FALSE;
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -