logfunctions.cpp

来自「MCU Production Programmer」· C++ 代码 · 共 50 行

CPP
50
字号

#include "stdafx.h"
#include "LogFunctions.h"

void LogMessage(CRichEditCtrl* richEditCtrl, BOOL status, const char* message, BOOL logToFile, CFile* logFile)
{
	CString logMessage;
	CString timeString;
	CTime time;
	CHARFORMAT cf;

	richEditCtrl->GetDefaultCharFormat(cf);
	
	cf.dwMask = CFM_COLOR;
	cf.dwEffects = 0;

	if (status)
	{
		cf.crTextColor = RGB(0, 0, 0);
	}
	else
	{
		cf.crTextColor = RGB(255, 0, 0);
	}

	// Obtain the current time and create a string for the log
	time = CTime::GetCurrentTime();
	timeString = time.Format("%H:%M:%S");

	// Create the log string with the time, error if needed and the message
	logMessage.Format("%s - %s%s\r\n", timeString, (status ? "" : "ERROR: "), message);
	
	// Set Focus to auto scroll the Richedit window and update it
	richEditCtrl->SetFocus();
	richEditCtrl->SetSel(0xFFFF, 0xFFFF);

	richEditCtrl->HideSelection(FALSE, TRUE);

	richEditCtrl->SetSelectionCharFormat(cf);
	richEditCtrl->ReplaceSel(logMessage);

	richEditCtrl->HideSelection(TRUE, TRUE);

	// If we are logging to file, put the message in the file as well
	if (logToFile && (logFile->m_hFile != (UINT)INVALID_HANDLE_VALUE))
	{
		logFile->Write(logMessage.GetBuffer(1), logMessage.GetLength());
		logMessage.ReleaseBuffer();
	}
}

⌨️ 快捷键说明

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