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/SGMeasurement.vcxproj | 2
SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h | 32 +++++++
SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters | 6 +
SourceCode/Bond/SGMeasurement/Logger.cpp | 79 +++++++++++++++++++
SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp | 39 +++++++++
SourceCode/Bond/SGMeasurement/Logger.h | 28 +++++++
6 files changed, 183 insertions(+), 3 deletions(-)
diff --git a/SourceCode/Bond/SGMeasurement/Logger.cpp b/SourceCode/Bond/SGMeasurement/Logger.cpp
new file mode 100644
index 0000000..1908277
--- /dev/null
+++ b/SourceCode/Bond/SGMeasurement/Logger.cpp
@@ -0,0 +1,79 @@
+#include "pch.h"
+#include "Logger.h"
+
+CLogger& CLogger::Instance()
+{
+ static CLogger instance;
+ return instance;
+}
+
+CLogger::CLogger()
+{
+ OpenLogFile();
+}
+
+CLogger::~CLogger()
+{
+ CloseLogFile();
+}
+
+void CLogger::OpenLogFile()
+{
+ CSingleLock lock(&m_csLogLock, TRUE);
+
+ CTime now = CTime::GetCurrentTime();
+ CString strLogDir = _T("Log");
+
+ if (!PathFileExists(strLogDir)) {
+ CreateDirectory(strLogDir, NULL);
+ }
+
+ CString strNewPath;
+ strNewPath.Format(_T("%s\\SG_%04d%02d%02d.log"), strLogDir, now.GetYear(), now.GetMonth(), now.GetDay());
+
+ if (m_strCurrentLogPath.CompareNoCase(strNewPath) != 0 || m_logFile.m_pStream == nullptr) {
+ if (m_logFile.m_pStream) {
+ m_logFile.Flush();
+ m_logFile.Close();
+ }
+
+ if (m_logFile.Open(strNewPath,
+ CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeBinary)) {
+
+ if (m_logFile.GetLength() == 0) {
+ WCHAR bom = 0xFEFF;
+ m_logFile.Write(&bom, sizeof(WCHAR));
+ }
+
+ m_logFile.SeekToEnd();
+ m_strCurrentLogPath = strNewPath;
+ }
+ }
+}
+
+void CLogger::WriteLine(CString str)
+{
+ CSingleLock lock(&m_csLogLock, TRUE);
+
+ OpenLogFile();
+
+ if (m_logFile.m_pStream) {
+ CTime now = CTime::GetCurrentTime();
+ CString strTime;
+ strTime.Format(_T("[%02d:%02d:%02d]"), now.GetHour(), now.GetMinute(), now.GetSecond());
+
+ CString strLine = strTime + str + _T("\r\n");
+ m_logFile.Write((LPCTSTR)strLine, strLine.GetLength() * sizeof(WCHAR));
+ }
+}
+
+void CLogger::CloseLogFile()
+{
+ CSingleLock lock(&m_csLogLock, TRUE);
+
+ if (m_logFile.m_pStream) {
+ m_logFile.Flush();
+ m_logFile.Close();
+ m_strCurrentLogPath.Empty();
+ }
+}
\ No newline at end of file
diff --git a/SourceCode/Bond/SGMeasurement/Logger.h b/SourceCode/Bond/SGMeasurement/Logger.h
new file mode 100644
index 0000000..aec95af
--- /dev/null
+++ b/SourceCode/Bond/SGMeasurement/Logger.h
@@ -0,0 +1,28 @@
+#pragma once
+#include <afx.h>
+
+class CLogger
+{
+public:
+ static CLogger& Instance(); // 获取单例
+ void WriteLine(CString str); // 写一行日志
+ void CloseLogFile(); // 关闭日志文件
+
+private:
+ CLogger();
+ ~CLogger();
+ void OpenLogFile(); // 内部打开文件
+
+ CCriticalSection m_csLogLock;
+ CString m_strCurrentLogPath;
+ CStdioFile m_logFile;
+};
+
+#define LOG_LINE(x) CLogger::Instance().WriteLine(x)
+
+#define LOG_LINEF(fmt, ...) \
+ do { \
+ CString __log__; \
+ __log__.Format(fmt, __VA_ARGS__); \
+ CLogger::Instance().WriteLine(__log__); \
+ } while (0)
\ No newline at end of file
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
index d26ab99..5d0dadc 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj
@@ -196,6 +196,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
+ <ClInclude Include="Logger.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="SGMeasurement.h" />
@@ -203,6 +204,7 @@
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
+ <ClCompile Include="Logger.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
index 20f1cde..7bcd4fb 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters
@@ -33,6 +33,9 @@
<ClInclude Include="pch.h">
<Filter>澶存枃浠�</Filter>
</ClInclude>
+ <ClInclude Include="Logger.h">
+ <Filter>澶存枃浠�</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SGMeasurement.cpp">
@@ -44,6 +47,9 @@
<ClCompile Include="pch.cpp">
<Filter>婧愭枃浠�</Filter>
</ClCompile>
+ <ClCompile Include="Logger.cpp">
+ <Filter>婧愭枃浠�</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="SGMeasurement.rc">
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp
index b946b61..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)*/)
@@ -614,6 +640,8 @@
return -1.0f;
}
+ clock_t startClock = clock(); // 璁板綍寮�濮嬫椂闂�
+
std::vector<float> vecBuffer(m_nSavePointCount, 0.0f);
int nReceived = 0;
@@ -648,6 +676,13 @@
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;
}
diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
index 96e570b..3b8748c 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h
@@ -49,6 +49,13 @@
DECLARE_MESSAGE_MAP()
private:
+ /**
+ * @brief 閫�鍑虹▼搴忔椂鐨勬竻鐞嗘搷浣溿��
+ *
+ * 姝ゅ嚱鏁板湪绋嬪簭閫�鍑哄墠琚皟鐢紝鐢ㄤ簬鎵ц蹇呰鐨勮祫婧愰噴鏀句笌鐘舵�佹仮澶嶏紝
+ * 鍖呮嫭鏂紑璁惧杩炴帴銆佺Щ闄ゆ墭鐩樺浘鏍囩瓑銆�
+ * 鏈�缁堥攢姣佷富绐楀彛骞堕��鍑哄簲鐢ㄣ��
+ */
void ExitApplication();
/**
@@ -85,7 +92,7 @@
* @param content 鏃ュ織鍐呭銆�
* @param color 瀛椾綋棰滆壊锛岄粯璁や负榛戣壊銆�
*/
- void AppendLogLineRichStyled(const CString& content, COLORREF color = RGB(0, 0, 0));
+ void AppendLogLineRichStyled(const CString& strContent, COLORREF color = RGB(0, 0, 0));
/**
* @brief 楂樹寒鏃ュ織涓墍鏈夊尮閰嶆寚瀹氬瓧绗︿覆鐨勯儴鍒嗐��
@@ -178,10 +185,33 @@
const std::vector<float>& vecGlass2,
float& fAvg1, float& fAvg2, float& fOffset);
+ /**
+ * @brief 鍒濆鍖栬澶囩鐨勬暟鎹瓨鍌ㄧ紦鍐插尯銆�
+ *
+ * 璋冪敤搴曞眰鎺ュ彛娓呴櫎褰撳墠瀛樺偍鍖哄唴瀹癸紝涓烘柊涓�杞殑鏁版嵁閲囬泦鍋氬噯澶囥��
+ * 蹇呴』鍦ㄨ澶囪繛鎺ユ垚鍔熶笖鏈繘琛屾暟鎹瓨鍌ㄦ椂璋冪敤銆�
+ *
+ * @return true 琛ㄧず鍒濆鍖栨垚鍔燂紱false 琛ㄧず澶辫触锛堝彲鑳芥槸璁惧鏈繛鎺ユ垨璋冪敤鎺ュ彛閿欒锛夈��
+ */
bool InitDataStorage();
+ /**
+ * @brief 鍚姩璁惧绔暟鎹噰闆嗗拰瀛樺偍銆�
+ *
+ * 璋冪敤姝ゅ嚱鏁板悗璁惧寮�濮嬮噰闆嗗苟缂撳瓨鏁版嵁銆�
+ * 閫氬父閰嶅悎瑙﹀彂妯″紡杩涜閲囬泦銆�
+ *
+ * @return true 琛ㄧず鍚姩鎴愬姛锛沠alse 琛ㄧず鍚姩澶辫触銆�
+ */
bool StartDataStorage();
+ /**
+ * @brief 鍋滄鏁版嵁閲囬泦骞朵粠璁惧鑾峰彇褰撳墠瀛樺偍鏁版嵁銆�
+ *
+ * 璋冪敤鍚庤澶囧仠姝㈤噰闆嗭紝骞跺皾璇曡鍙栨寚瀹氱鍙g殑鏁版嵁锛屼緵鍚庣画鍒嗘瀽澶勭悊銆�
+ *
+ * @return true 琛ㄧず鍋滄骞惰鍙栨暟鎹垚鍔燂紱false 琛ㄧず澶辫触锛堝璁惧鏈搷搴旀垨鏁版嵁鏃犳晥锛夈��
+ */
bool StopDataStorage();
/**
--
Gitblit v1.9.3