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

📄 writeinfo.cpp

📁 cisoca_for_mysql 编码
💻 CPP
字号:
// WriteInfo.cpp : implementation file
//

#include "stdafx.h"
#include "WriteInfo.h"
#include <windows.h>
#include "Shlwapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWriteInfo

IMPLEMENT_DYNCREATE(CWriteInfo, CWinThread)

CWriteInfo::CWriteInfo()
{
	m_bAutoDelete = FALSE;
}

CWriteInfo::~CWriteInfo()
{
}

BOOL CWriteInfo::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	// 为文件操作提供冲突控制, 创建标志文件操作的事件
	m_hEventBusy = CreateEvent( 
		NULL,                       // no security attributes
		TRUE,						// manual-reset event
		TRUE,						// initial state is signaled 	
		NULL);						// event's name
	if (m_hEventBusy == NULL) 
	{
		// Check for error.
#ifdef _DEBUG
		AfxMessageBox("Create m_hEventBusy Error!");
#endif
	}
	// 以下部分打开(若不存在则创建之)约定的文件,文件名采用
	// "模块名 - .dll + .log"
	const MAX_LENGTH = 512;
	char str[MAX_LENGTH];
	CString strModuleDirName;
	GetModuleFileName(NULL,str,MAX_LENGTH);
	strModuleDirName = str;
	// 从后往前找到第一个目录分隔符'\'截掉在它之后的部分得到模块文件
	// 所在的目录名
	int i = 0;
	CString string;
	for(i=strModuleDirName.GetLength(); i>0; i--)
	{
		if(str[i] == '\\')
		{
			string = "\\Log\\" + m_strModuleName;
			memcpy(&(str[i+1]),string,string.GetLength());
			str[i+1+string.GetLength()+1] = '\0';
			string = str;
			break;
		}
	}
	// 置日志文件名为"*.log"
	for(i=string.GetLength(); i>0; i--)
	{
		if(str[i] == '.')
		{
			str[i+1] = 'l';
			str[i+2] = 'o';
			str[i+3] = 'g';
			str[i+4] = '\0';
			break;
		}
	}
	// 易用性考虑在这里添加对目录的检查
	CheckDir(str);
	// modeCreate|modeNoTruncate	-	文件存在不截断|不存在创建
	// modeReadWrite				-	为了读写打开
	UINT uFlag=CFile::modeCreate|CFile::modeNoTruncate|
		CFile::modeReadWrite|CFile::shareDenyWrite;
	if(!m_file.Open(str,uFlag))
	{
		// 打开文件错误2003-12-29修改的bug
		return FALSE;
	}
	// 将文件指针移动至末尾等待写入新信息
	m_file.SeekToEnd();

	return TRUE;
}

int CWriteInfo::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	m_file.Close();
	::CloseHandle(m_hEventBusy);

	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CWriteInfo, CWinThread)
	//{{AFX_MSG_MAP(CWriteInfo)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_THREAD_MESSAGE(TM_WRITE_INFO, OnWriteInfo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWriteInfo message handlers

void CWriteInfo::SetModuleFileName(CString strFileName)
{
	m_strModuleName = strFileName;
}

CString CWriteInfo::GetName()
{
	return m_strModuleName;
}

//////////////////////////////////////////////////////////////////
//	消息处理函数
//////////////////////////////////////////////////////////////////

//
//  FUNCTION: void OnWriteInfo(WPARAM wParam, LPARAM lParam)
//
//  PURPOSE: 将提供的信息写入到对应的日志文件中。
//
//  PARAMETERS:
//		wParam: 要写入的内容指针,类型为char*
//		lParam: 要写入的内容的长度
//
//  RETURN VALUE:
//		N/A
//
//  COMMENTS:
//
//		用该函数写入日志信息。
//
void CWriteInfo::OnWriteInfo(WPARAM wParam, LPARAM lParam)
{
	char* pChar = (char*)wParam;
	// 回车换行符
	char changeLine[2] = {'\r','\n'};
	//	::AfxMessageBox(strInfo);
	// 防止文件操作冲突
	if(WAIT_OBJECT_0 == ::WaitForSingleObject(m_hEventBusy,360))
	{
		CTime time = CTime::GetCurrentTime();
		CString strTime = time.Format("%Y-%m-%d  %H:%M:%S  ");

		::ResetEvent(m_hEventBusy);	// 进“临界区”
		
		// 写入时间信息
		m_file.Write(strTime, strTime.GetLength());
		//m_file.Write(changeLine,2);
		m_file.Write(pChar,lParam);
		// 在此处释放由发送消息TM_WRITE_INFO的函数new出的内存
		delete[] pChar;
		m_file.Write(changeLine,2);
		m_file.SeekToEnd();
		
		::SetEvent(m_hEventBusy); // 出“临界区”
	}
	else
	{
#ifdef _DEBUG
		::AfxMessageBox("File Operation Ignored!");
#endif
		return;
	}
}

// 张荣华 2003/12/29日加入
// 对目录的安全性检查函数

void CWriteInfo::CheckDir(CString strDir)
{
	int pos = strDir.ReverseFind('\\');
	CString str = strDir.Left(pos);
	if(!PathIsDirectory(str))
		CreateDirectory(str,NULL);
}

⌨️ 快捷键说明

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