⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.cpp

📁 多线程下载的
💻 CPP
字号:
// Log.cpp: implementation of the CLog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Log.h"
#include <Afxmt.h>
#include "PublicFunction.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

HWND							g_hWnd_RecvLogMsg;
CStringArray					m_StrAry_LastLogText;
CUIntArray						m_UIntAry_LastLogLevel;
CCriticalSection				m_CSFor_StrAry_LastLog;
FUNC_GetHwndForMsgBox			g_Proc_GetHwndForMsgBox;

CString GetMLTextPub ( LPCTSTR lpszEnglishText, ... );
// 日志级别
const char *g_szLogLevelString[] = 
{
	"VERBOSE","NORMAL","WARNING","ERROR","DEBUG","INVALID",
};
const UINT g_nMsgBoxType[] =
{
	MB_ICONINFORMATION, MB_ICONINFORMATION, MB_ICONWARNING, MB_ICONERROR, MB_ICONERROR, 0,
};
const UINT g_nMsgTextColor[] =
{
	RGB(64,64,64), RGB(0,0,128), RGB(128,0,0), RGB(255,0,0), RGB(255,0,0), RGB(0,0,0),
};

void Set_RecvLogMsgHwnd ( HWND hWnd_RecvLogMsg )
{
	if ( ::IsWindow ( hWnd_RecvLogMsg ) )
		g_hWnd_RecvLogMsg = hWnd_RecvLogMsg;
}

//
// 输出日志信息的函数
//
void Log ( UINT nLevel, char *format, ...)
{
	char LogBuf[MAX_LOGBUF_SIZE] = {0};
	int nLen = 0;
	// 获取当前时间
	nLen += _snprintf ( LogBuf+nLen, MAX_LOGBUF_SIZE-nLen-3, "[" );
	nLen += GetCurTimeString ( LogBuf+nLen );
	nLen += _snprintf ( LogBuf+nLen, MAX_LOGBUF_SIZE-nLen-3, "] " );

	// 日志级别
	int nLogLevel = (nLevel & 0xf);
	ASSERT ( nLogLevel >= L_VERBOSE && nLogLevel < L_LOGNUM );
	nLen += _snprintf ( LogBuf+nLen, MAX_LOGBUF_SIZE-nLen-3, "<%s> ",
		g_szLogLevelString[nLogLevel] );

	// 格式化日志信息
	va_list  va;
	va_start (va, format);
	int nRet = _vsnprintf ( LogBuf+nLen, MAX_LOGBUF_SIZE-nLen-3, (const char*)format, va);
	va_end(va);
	int nLogInfoPos = nLen;
	if ( nRet > 0 ) nLen += nRet;
	else nLen = strlen(LogBuf);

	// 添加回车换行符
	strcat ( LogBuf, "\r\n" );
	nLen += 2;

	// 显示日志信息
	if ( !(nLevel&L_ONLY_LOGFILE) )
	{
		if ( !::IsWindow(g_hWnd_RecvLogMsg) || (nLevel&L_OUT_DLG) )
		{
			ASSERT ( nLogInfoPos > 2 );
			LogBuf[nLen-2] = '\0';
			if ( nLogLevel == L_WARNING || nLogLevel == L_ERROR || (nLevel&L_OUT_DLG) )
			{
				hwMsgBox ( NULL, NULL, g_nMsgBoxType[nLogLevel], LogBuf+nLogInfoPos );
			}
		}
		else
		{
			m_CSFor_StrAry_LastLog.Lock ();
			m_StrAry_LastLogText.Add ( LogBuf );
			m_UIntAry_LastLogLevel.Add ( nLevel );
			m_CSFor_StrAry_LastLog.Unlock ();
			if ( nLevel&L_BALLOON )
				::SendMessage ( g_hWnd_RecvLogMsg, WM_SHOWLOG, WPARAM(nLevel), LPARAM(NULL) );
			else
				::PostMessage ( g_hWnd_RecvLogMsg, WM_SHOWLOG, WPARAM(nLevel), LPARAM(NULL) );
		}
	}

#ifdef _DEBUG
	if ( nLevel&L_TRACE )
	{
		TRACE ( "%s", LogBuf );
	}
#endif
}

int hwMsgBox (
		HWND hWnd,
		LPCTSTR lpCaption, 
		UINT uType,
		LPCTSTR lpText,
		...
	)
{
	ASSERT ( lpText );
	char szText[255] = {0};
	// 格式化日志信息
	va_list  va;
	va_start (va, lpText);
	_vsnprintf ( szText, sizeof(szText)-1, (const char*)lpText, va);
	va_end(va);
	if ( !::IsWindow (hWnd) && g_Proc_GetHwndForMsgBox )
		hWnd = g_Proc_GetHwndForMsgBox ();
	return ::MessageBox ( hWnd, szText, lpCaption?lpCaption:AfxGetAppName(), uType );
}


void GetLastLogStrAry ( CStringArray &StrAry, CUIntArray &UIntAry )
{
	m_CSFor_StrAry_LastLog.Lock ();
	StrAry.Append ( m_StrAry_LastLogText );
	UIntAry.Append ( m_UIntAry_LastLogLevel );
	m_StrAry_LastLogText.RemoveAll ();
	m_UIntAry_LastLogLevel.RemoveAll ();
	m_CSFor_StrAry_LastLog.Unlock ();
}

void AddLogTextToEditCtrl ( CRichEditCtrl *pEditLog, LPCTSTR lpszLogText, int nLevel )
{
	int nLogLevel = (nLevel & 0xf);
	ASSERT ( nLogLevel >= 0 && nLogLevel < LENGTH(g_nMsgTextColor) );
	ASSERT ( lpszLogText );
	if ( !pEditLog || !::IsWindow ( pEditLog->m_hWnd ) )
		return;
	CString csLog;
	pEditLog->GetWindowText ( csLog );
	int nTextLen_Org = csLog.GetLength ();
	CString csAddText = lpszLogText;
	if ( ::IsWindow ( pEditLog->m_hWnd ) )
	{
		if ( nTextLen_Org < EDIT_MAX_TEXT )
		{
			pEditLog->SetSel ( nTextLen_Org, -1 );
		}
		else
		{
			pEditLog->SetSel ( 0, -1 );
			nTextLen_Org = 0;
		}
		pEditLog->ReplaceSel( csAddText );
		pEditLog->SetSel ( nTextLen_Org, nTextLen_Org+csAddText.GetLength() );

		// 设置文本颜色
		CHARFORMAT cf;
		pEditLog->GetSelectionCharFormat(cf);
		if (cf.dwEffects & CFE_AUTOCOLOR)
			cf.dwEffects -= CFE_AUTOCOLOR;
		cf.crTextColor = g_nMsgTextColor[nLogLevel];
		cf.dwMask = CFM_COLOR;	
		pEditLog->SetSelectionCharFormat(cf);
		pEditLog->HideSelection(TRUE, FALSE);

		// 滚动在最底行
		pEditLog->SendMessage ( WM_VSCROLL, (WPARAM)SB_BOTTOM, (LPARAM)NULL );
	}
}

⌨️ 快捷键说明

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