From 61ac290c2897f69db9d879ea4968304ddcd23d40 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期五, 01 八月 2025 19:22:54 +0800
Subject: [PATCH] 1. 添加日志文件

---
 SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp |  278 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 177 insertions(+), 101 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index d6bc177..eb946f3 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
@@ -8,6 +8,7 @@
 #include "SGMeasurementDlg.h"
 #include "afxdialogex.h"
 #include "SGIF.h"
+#include "Logger.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -175,7 +176,7 @@
 	m_editLog.ReplaceSel(_T(""));   // 鍒犻櫎
 }
 
-void CSGMeasurementDlg::AppendLogLineRichStyled(const CString& content, COLORREF color /*= RGB(0, 0, 0)*/)
+void CSGMeasurementDlg::AppendLogLineRichStyled(const CString& strContent, COLORREF color /*= RGB(0, 0, 0)*/)
 {
 	if (!::IsWindow(GetSafeHwnd()) || !::IsWindow(m_editLog.GetSafeHwnd())) {
 		return;
@@ -197,16 +198,41 @@
 	m_editLog.SetSelectionCharFormat(cfTime);
 	m_editLog.ReplaceSel(strTimestamp);
 
+	// 鐢熸垚鏃ュ織绾у埆鏍囩
+	CString strLevel;
+	if (color == LOG_COLOR_WARNING) {
+		strLevel = _T("[璀﹀憡]");
+	}
+	else if (color == LOG_COLOR_ERROR) {
+		strLevel = _T("[閿欒]");
+	}
+	else if (color == LOG_COLOR_NORMAL) {
+		strLevel = _T("[淇℃伅]");
+	}
+	else if (color == LOG_COLOR_SUCCESS) {
+		strLevel = _T("[鎴愬姛]");
+	}
+	else {
+		strLevel = _T("[鏈煡]");
+	}
+
 	// 鎻掑叆鏃ュ織姝f枃锛堜紶鍏ラ鑹诧級
 	CHARFORMAT2 cfMsg = {};
 	cfMsg.cbSize = sizeof(cfMsg);
 	cfMsg.dwMask = CFM_COLOR;
 	cfMsg.crTextColor = color;
 	m_editLog.SetSelectionCharFormat(cfMsg);
-	m_editLog.ReplaceSel(content + _T("\r\n"));
+	m_editLog.ReplaceSel(strLevel + strContent + _T("\r\n"));
 
 	// 闄愬埗鏈�澶ц鏁�
 	TrimRichEditLineLimit(100);
+
+	// 鎷兼帴瀹屾暣鏃ュ織琛�
+	CString strFullLogLine;
+	strFullLogLine.Format(_T("%s %s"), strLevel, strContent);
+
+	// 鍐欏叆鏃ュ織鏂囦欢
+	LOG_LINE(strFullLogLine);
 }
 
 void CSGMeasurementDlg::HighlightAllMatches(const CString& strSearch, COLORREF clrHighlight/* = RGB(255, 165, 0)*/)
@@ -520,6 +546,146 @@
 	return true;
 }
 
+bool CSGMeasurementDlg::InitDataStorage()
+{
+	if (!m_bConnected) {
+		AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
+		return false;
+	}
+
+	if (m_bSaving) {
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍姝e湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
+		return false;
+	}
+
+	RC nRet = SGIF_DataStorageInit(DeviceID);
+	if (nRet == RC_OK) {
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸叉竻闄ゃ��"), LOG_COLOR_SUCCESS);
+		return true;
+	}
+
+	CString strError;
+	strError.Format(_T("娓呴櫎鏁版嵁瀛樺偍澶辫触锛岄敊璇爜锛�%#X"), nRet);
+	AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
+	return false;
+}
+
+bool CSGMeasurementDlg::StartDataStorage()
+{
+	if (m_bSaving) {
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
+		return false;
+	}
+
+	RC nRet = SGIF_DataStorageStart(DeviceID);
+	if (nRet == RC_OK) {
+		m_bSaving = TRUE;
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插紑濮嬨��"), LOG_COLOR_SUCCESS);
+		return true;
+	}
+
+	CString strError;
+	strError.Format(_T("寮�濮嬫暟鎹瓨鍌ㄥけ璐ワ紝閿欒鐮侊細%#X"), nRet);
+	AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
+	return false;
+}
+
+bool CSGMeasurementDlg::StopDataStorage()
+{
+	if (!m_bSaving) {
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍鏈湪杩涜涓紝璇峰厛寮�濮嬪瓨鍌ㄣ��"), LOG_COLOR_WARNING);
+		return false;
+	}
+
+	RC nRet = SGIF_DataStorageStop(DeviceID);
+	if (nRet == RC_OK) {
+		m_bSaving = FALSE;
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插仠姝€��"), LOG_COLOR_SUCCESS);
+		return true;
+	}
+
+	CString strError;
+	strError.Format(_T("鍋滄鏁版嵁瀛樺偍澶辫触锛岄敊璇爜锛�%#X"), nRet);
+	AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
+	return false;
+}
+
+float CSGMeasurementDlg::AnalyzeStoredData(int nOutNo)
+{
+	UpdateData(TRUE);
+
+	if (m_nUseTrigger) {
+		UpdateControlStatus(m_bConnected, m_bSaving);
+		AfxMessageBox(_T("褰撳墠鏄‖瑙﹀彂妯″紡锛岃妫�鏌ヨЕ鍙戝櫒鐘舵�併��"), MB_ICONINFORMATION);
+		return -1.0f;
+	}
+
+	if (!m_bConnected) {
+		AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
+		return -1.0f;
+	}
+
+	if (m_bSaving) {
+		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍姝e湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
+		return -1.0f;
+	}
+
+	if (nOutNo < 1 || nOutNo > 4) {
+		AppendLogLineRichStyled(_T("杈撳嚭绔彛缂栧彿鏃犳晥锛屽繀椤诲湪 1 鍒� 4 涔嬮棿銆�"), LOG_COLOR_ERROR);
+		return -1.0f;
+	}
+
+	if (m_nSavePointCount < 0) {
+		AppendLogLineRichStyled(_T("鏁版嵁鐐规暟蹇呴』澶т簬 0銆�"), LOG_COLOR_ERROR);
+		return -1.0f;
+	}
+
+	clock_t startClock = clock();  // 璁板綍寮�濮嬫椂闂�
+
+	std::vector<float> vecBuffer(m_nSavePointCount, 0.0f);
+	int nReceived = 0;
+
+	RC nRet = SGIF_DataStorageGetData(DeviceID, nOutNo, m_nSavePointCount, vecBuffer.data(), &nReceived);
+	if (nRet != RC_OK) {
+		CString strError;
+		strError.Format(_T("璇诲彇 OUT%d 鏁版嵁澶辫触锛岄敊璇爜锛�%#X"), nOutNo, nRet);
+		AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
+		return -1.0f;
+	}
+
+	vecBuffer.resize(nReceived);
+	CleanInvalidValuesInPlace(nOutNo, vecBuffer);
+
+	std::vector<float> vecGlass1, vecGlass2;
+	if (!SplitGlassSegments(nOutNo, vecBuffer, vecGlass1, vecGlass2, m_fJumpThreshold, m_nJumpWindow, m_nValleyMargin, m_nMinGlass1Count)) {
+		AppendLogLineRichStyled(_T("鏈兘璇嗗埆鍑轰袱鐗囩幓鐠冪殑鏁版嵁銆�"), LOG_COLOR_WARNING);
+		return -1.0f;
+	}
+
+	std::vector<float> vecStable1, vecStable2;
+	bool bStable1 = ExtractStableRegionFixed(nOutNo, vecGlass1, vecStable1, m_nFixedCount, m_fMaxDelta);
+	bool bStable2 = ExtractStableRegionFixed(nOutNo, vecGlass2, vecStable2, m_nFixedCount, m_fMaxDelta);
+
+	float fAvg1 = 0.0f, fAvg2 = 0.0f, fOffset = 0.0f;
+	if (bStable1 && bStable2) {
+		AppendLogLineRichStyled(_T("鎴愬姛鎻愬彇鍒颁袱鐗囩幓鐠冪殑绋冲畾鍖烘暟鎹��"), LOG_COLOR_SUCCESS);
+		CalcGlassOffset(vecStable1, vecStable2, fAvg1, fAvg2, fOffset);
+	}
+	else {
+		AppendLogLineRichStyled(_T("鏈兘鎻愬彇鍒扮ǔ瀹氬尯鏁版嵁锛屽皾璇曚娇鐢ㄥ師濮嬪垎娈垫暟鎹绠楀亸绉汇��"), LOG_COLOR_WARNING);
+		CalcGlassOffset(vecGlass1, vecGlass2, fAvg1, fAvg2, fOffset);
+	}
+
+	clock_t endClock = clock();  // 璁板綍缁撴潫鏃堕棿
+	double dElapsedMs = 1000.0 * (endClock - startClock) / CLOCKS_PER_SEC;
+
+	CString strElapsed;
+	strElapsed.Format(_T("AnalyzeStoredData 鎵ц鑰楁椂锛�%.1f ms"), dElapsedMs);
+	AppendLogLineRichStyled(strElapsed, LOG_COLOR_SUCCESS);
+
+	return fOffset;
+}
+
 BEGIN_MESSAGE_MAP(CSGMeasurementDlg, CDialogEx)
 	ON_WM_SYSCOMMAND()
 	ON_WM_PAINT()
@@ -827,120 +993,30 @@
 void CSGMeasurementDlg::OnBnClickedButtonClearStore()
 {
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
-	if (!m_bConnected) {
-		AppendLogLineRichStyled(_T("璁惧鏈繛鎺ワ紝璇峰厛杩炴帴璁惧銆�"), LOG_COLOR_WARNING);
-		return;
-	}
-
-	if (m_bSaving) {
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍姝e湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
-		return;
-	}
-
-	RC nRet = SGIF_DataStorageInit(DeviceID);
-	if (nRet == RC_OK) {
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸叉竻闄ゃ��"), LOG_COLOR_SUCCESS);
-	}
-	else {
-		CString strError;
-		strError.Format(_T("娓呴櫎鏁版嵁瀛樺偍澶辫触锛岄敊璇爜锛�%#X"), nRet);
-		AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
-	}
+	InitDataStorage();
 }
 
 void CSGMeasurementDlg::OnBnClickedButtonStartStore()
 {
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
-	if (m_bSaving) {
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插湪杩涜涓紝璇峰厛鍋滄瀛樺偍銆�"), LOG_COLOR_WARNING);
-		return;
-	}
-
-	RC nRet = SGIF_DataStorageStart(DeviceID);
-	if (nRet == RC_OK) {
-		m_bSaving = TRUE;
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插紑濮嬨��"), LOG_COLOR_SUCCESS);
-	}
-	else {
-		CString strError;
-		strError.Format(_T("寮�濮嬫暟鎹瓨鍌ㄥけ璐ワ紝閿欒鐮侊細%#X"), nRet);
-		AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
-	}
-
+	StartDataStorage();
 	UpdateControlStatus(m_bConnected, m_bSaving);
 }
 
 void CSGMeasurementDlg::OnBnClickedButtonStopStore()
 {
 	// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
-	UpdateData(TRUE);
+	StopDataStorage();
+	UpdateControlStatus(m_bConnected, m_bSaving);
 
-	if (!m_bSaving) {
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍鏈湪杩涜涓紝璇峰厛寮�濮嬪瓨鍌ㄣ��"), LOG_COLOR_WARNING);
+	int nSel = m_comboOutputPort.GetCurSel();
+	if (CB_ERR == nSel) {
+		AppendLogLineRichStyled(_T("璇烽�夋嫨涓�涓湁鏁堢殑杈撳嚭绔彛銆�"), LOG_COLOR_WARNING);
 		return;
 	}
 
-	RC nRet = SGIF_DataStorageStop(DeviceID);
-	if (nRet == RC_OK) {
-		m_bSaving = FALSE;
-		AppendLogLineRichStyled(_T("鏁版嵁瀛樺偍宸插仠姝€��"), LOG_COLOR_SUCCESS);
-
-		if (m_nUseTrigger) {
-			UpdateControlStatus(m_bConnected, m_bSaving);
-			AfxMessageBox(_T("褰撳墠鏄‖瑙﹀彂妯″紡锛岃妫�鏌ヨЕ鍙戝櫒鐘舵�併��"), MB_ICONINFORMATION);
-			return;
-		}
-
-		int nReceived = 0;
-		std::vector<float> vecBuffer(m_nSavePointCount, 0.0f);
-
-		int nSel = m_comboOutputPort.GetCurSel();
-		if (CB_ERR == nSel) {
-			AppendLogLineRichStyled(_T("璇烽�夋嫨涓�涓湁鏁堢殑杈撳嚭绔彛銆�"), LOG_COLOR_WARNING);
-			return;
-		}
-
-		int nOutNo = nSel + 1;
-		nRet = SGIF_DataStorageGetData(DeviceID, nOutNo, m_nSavePointCount, vecBuffer.data(), &nReceived);
-
-		CString strLog;
-		if (nRet == RC_OK) {
-			vecBuffer.resize(nReceived);
-			CleanInvalidValuesInPlace(nOutNo, vecBuffer);
-
-			std::vector<float> vecGlass1, vecGlass2;
-			if (SplitGlassSegments(nOutNo, vecBuffer, vecGlass1, vecGlass2, m_fJumpThreshold, m_nJumpWindow, m_nValleyMargin, m_nMinGlass1Count)) {
-
-				std::vector<float> vecStableGlass1, vecStableGlass2;
-				bool bStable1 = ExtractStableRegionFixed(nOutNo, vecGlass1, vecStableGlass1, m_nFixedCount, m_fMaxDelta);
-				bool bStable2 = ExtractStableRegionFixed(nOutNo, vecGlass2, vecStableGlass2, m_nFixedCount, m_fMaxDelta);
-
-				float fAvg1 = 0.0f, fAvg2 = 0.0f, fOffset = 0.0f;
-				if (bStable1 && bStable2) {
-					AppendLogLineRichStyled(_T("鎴愬姛鎻愬彇鍒颁袱鐗囩幓鐠冪殑绋冲畾鍖烘暟鎹��"), LOG_COLOR_SUCCESS);
-					CalcGlassOffset(vecStableGlass1, vecStableGlass2, fAvg1, fAvg2, fOffset);
-				}
-				else {
-					AppendLogLineRichStyled(_T("鏈兘鎻愬彇鍒扮ǔ瀹氬尯鏁版嵁锛屾棤娉曟甯歌绠楀亸绉汇��"), LOG_COLOR_WARNING);
-					CalcGlassOffset(vecGlass1, vecGlass2, fAvg1, fAvg2, fOffset);
-				}
-			}
-			else {
-				AppendLogLineRichStyled(_T("鏈兘璇嗗埆鍑轰袱鐗囩幓鐠冪殑鏁版嵁銆�"), LOG_COLOR_WARNING);
-			}
-		}
-		else {
-			strLog.Format(_T("璇诲彇 OUT%d 鏁版嵁澶辫触锛岄敊璇爜锛�%#X"), nOutNo, nRet);
-			AppendLogLineRichStyled(strLog, LOG_COLOR_ERROR);
-		}
-	}
-	else {
-		CString strError;
-		strError.Format(_T("鍋滄鏁版嵁瀛樺偍澶辫触锛岄敊璇爜锛�%#X"), nRet);
-		AppendLogLineRichStyled(strError, LOG_COLOR_ERROR);
-	}
-
-	UpdateControlStatus(m_bConnected, m_bSaving);
+	int nOutNo = nSel + 1;
+	AnalyzeStoredData(nOutNo);
 }
 
 void CSGMeasurementDlg::OnBnClickedButtonClearLog()

--
Gitblit v1.9.3