mrDarker
2025-08-01 61ac290c2897f69db9d879ea4968304ddcd23d40
1. 添加日志文件
已添加2个文件
已修改4个文件
186 ■■■■■ 文件已修改
SourceCode/Bond/SGMeasurement/Logger.cpp 79 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/Logger.h 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/SGMeasurement.vcxproj.filters 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/SGMeasurementDlg.cpp 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/SGMeasurementDlg.h 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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();
    }
}
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)
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>
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">
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("[未知]");
    }
    // æ’入日志正文(传入颜色)
    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;
}
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 è¡¨ç¤ºå¯åŠ¨æˆåŠŸï¼›false è¡¨ç¤ºå¯åŠ¨å¤±è´¥ã€‚
     */
    bool StartDataStorage();
    /**
     * @brief åœæ­¢æ•°æ®é‡‡é›†å¹¶ä»Žè®¾å¤‡èŽ·å–å½“å‰å­˜å‚¨æ•°æ®ã€‚
     *
     * è°ƒç”¨åŽè®¾å¤‡åœæ­¢é‡‡é›†ï¼Œå¹¶å°è¯•读取指定端口的数据,供后续分析处理。
     *
     * @return true è¡¨ç¤ºåœæ­¢å¹¶è¯»å–数据成功;false è¡¨ç¤ºå¤±è´¥ï¼ˆå¦‚设备未响应或数据无效)。
     */
    bool StopDataStorage();
    /**