From e51c6d1360f9679dd8e4dd3379ce0db1886badbf Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 28 七月 2025 17:36:57 +0800
Subject: [PATCH] Merge branch 'EAPSimulator' into clh

---
 SourceCode/Bond/EAPSimulator/Log.cpp |  214 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 214 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/EAPSimulator/Log.cpp b/SourceCode/Bond/EAPSimulator/Log.cpp
new file mode 100644
index 0000000..ed48f5e
--- /dev/null
+++ b/SourceCode/Bond/EAPSimulator/Log.cpp
@@ -0,0 +1,214 @@
+#include "pch.h"
+#include "Log.h"
+
+
+static const char* pszLevel[] = {" [Debug] ", " [Info] ", " [Warn] ", " [Error] "};
+
+CLog::CLog()
+{
+	m_nLevel = 0;
+	m_nOutputTarget = OT_TRACE;
+	m_bAutoAppendTime = TRUE;
+	m_strEquipmentId = _T("Unknown");
+	m_nDay = 0;
+	m_funOnLog = nullptr;
+	InitializeCriticalSection(&m_criticalSection);
+}
+
+
+CLog::~CLog()
+{
+	DeleteCriticalSection(&m_criticalSection);
+}
+
+CLog *CLog::GetLog(void)
+{
+	static CLog* pLog = NULL;
+	if (pLog == NULL) {
+		static CLog log;
+		pLog = &log;
+	}
+
+	return pLog;
+}
+
+void CLog::SetOnLogCallback(ONLOG funOnLog)
+{
+	m_funOnLog = funOnLog;
+}
+
+void CLog::SetOutputTarget(int flag)
+{
+	m_nOutputTarget = flag;
+}
+
+void CLog::SetEquipmentId(const char* pszEquipmentId)
+{
+	m_strEquipmentId = pszEquipmentId;
+}
+
+void CLog::SetAutoAppendTimeString(BOOL bAutoAppendTime)
+{
+	m_bAutoAppendTime = bAutoAppendTime;
+}
+
+void CLog::Batch()
+{
+	if (m_file.m_hFile != CFile::hFileNull) {
+		m_file.Close();
+	}
+}
+
+BOOL CLog::BatchAndNew(int& nDay)
+{
+	Batch();
+	if ( (m_nOutputTarget & OT_FILE) && m_file.m_hFile == CFile::hFileNull) {
+		CString strFilepath;
+		BOOL bRet = m_file.Open(MakeFilepathD(strFilepath, nDay), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareDenyWrite);
+		if (bRet) {
+			m_file.SeekToEnd();
+		}
+		return bRet;
+	}
+
+	return FALSE;
+}
+
+#define BUFFERSIZE		1024*10
+void CLog::LogFormat(int nLevel, const char* pszTag, char* szMessage, ...)
+{
+	// 检查日期是否有变化,有变化则结批
+	Lock();
+	_SYSTEMTIME sysTime;
+	GetLocalTime(&sysTime);
+	if(m_nDay != sysTime.wDay) {
+		int nDay = 0;
+		if (BatchAndNew(nDay)) {
+			m_nDay = nDay;
+		}
+	}
+	Unlock();
+
+	if (nLevel < m_nLevel) {
+		return;
+	}
+
+	char szFullMessage[BUFFERSIZE];
+	char szFormatMessage[BUFFERSIZE];
+
+	// format message
+	va_list ap;
+	va_start(ap, szMessage);
+	_vsnprintf_s(szFormatMessage, BUFFERSIZE, szMessage, ap);
+	va_end(ap);
+
+	if (m_bAutoAppendTime) {
+		CString strTime;
+		strcpy_s(szFullMessage, BUFFERSIZE, (LPTSTR)(LPCTSTR)GetCurTime(strTime));
+	}
+	strcat_s(szFullMessage, BUFFERSIZE, pszLevel[nLevel]);
+	strcat_s(szFullMessage, szFormatMessage);
+	strcat_s(szFullMessage, BUFFERSIZE, "\n");
+
+	if (m_nOutputTarget & OT_FILE) {
+		Lock();
+		if (m_file.m_hFile != CFile::hFileNull) {
+			m_file.WriteString(szFullMessage);
+		}
+		Unlock();
+	}
+	if (m_nOutputTarget & OT_ODSTRING) {
+		OutputDebugStringA(szFullMessage);
+	}
+	else if(m_nOutputTarget & OT_TRACE) {
+		TRACE(szFormatMessage);
+	}
+
+	if (m_funOnLog != nullptr) {
+		m_funOnLog(nLevel, szFullMessage);
+	}
+}
+
+void CLog::Log(int nLevel, const char* pszTag, const char* szMessage)
+{
+	// 检查日期是否有变化,有变化则结批
+	Lock();
+	_SYSTEMTIME sysTime;
+	GetLocalTime(&sysTime);
+	if (m_nDay != sysTime.wDay) {
+		int nDay = 0;
+		if (BatchAndNew(nDay)) {
+			m_nDay = nDay;
+		}
+	}
+	Unlock();
+
+	if (nLevel < m_nLevel) {
+		return;
+	}
+
+	CString strMsg;
+	if (m_bAutoAppendTime) {
+		CString strTime;
+		GetCurTime(strTime);
+		strMsg.Append(strTime);
+	}
+	strMsg.Append(pszTag);
+	strMsg.Append(szMessage);
+	strMsg.Append("\n");
+
+	if (m_nOutputTarget & OT_FILE) {
+		Lock();
+		if (m_file.m_hFile != CFile::hFileNull) {
+			m_file.WriteString(strMsg);
+		}
+		Unlock();
+	}
+	if (m_nOutputTarget & OT_ODSTRING) {
+		OutputDebugStringA(strMsg);
+	}
+	else if (m_nOutputTarget & OT_TRACE) {
+		TRACE(strMsg);
+	}
+
+	if (m_funOnLog != nullptr) {
+		m_funOnLog(nLevel, strMsg);
+	}
+}
+
+CString& CLog::GetCurTime(CString& strTime)
+{
+	_SYSTEMTIME sysTime;
+	GetLocalTime(&sysTime);
+	strTime.Format(_T("%d/%02d/%02d %02d:%02d:%02d.%03d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay,
+		sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
+	return strTime;
+}
+
+CString& CLog::MakeFilepath(CString& strFilepath)
+{
+	_SYSTEMTIME sysTime;
+	GetLocalTime(&sysTime);
+	strFilepath.Format(_T("%s\\Log(%s)_%d_%02d_%02d.log"), (LPTSTR)(LPCTSTR)m_strLogsDir,
+		(LPTSTR)(LPCTSTR)m_strEquipmentId,
+		sysTime.wYear, sysTime.wMonth, sysTime.wDay);
+
+	return strFilepath;
+}
+
+CString& CLog::MakeFilepathD(CString& strFilepath, int& day)
+{
+	_SYSTEMTIME sysTime;
+	GetLocalTime(&sysTime);
+	strFilepath.Format(_T("%s\\Log(%s)_%d_%02d_%02d.log"), (LPTSTR)(LPCTSTR)m_strLogsDir,
+		(LPTSTR)(LPCTSTR)m_strEquipmentId,
+		sysTime.wYear, sysTime.wMonth, sysTime.wDay);
+	day = sysTime.wDay;
+
+	return strFilepath;
+}
+
+void CLog::SetLogsDir(CString strDir)
+{
+	m_strLogsDir = strDir;
+}

--
Gitblit v1.9.3