Skip to content

Commit

Permalink
v1.0.1.8发布
Browse files Browse the repository at this point in the history
  • Loading branch information
ccpwcn committed Mar 7, 2023
1 parent 7625ea9 commit b087e4e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
22 changes: 10 additions & 12 deletions LogUtil/CLogImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <exception> // std::exception
#include "Common.h"

// 本文件内全局变量初始化
// 本文件内全局变量初始化,之所以要添加为全局锁而不是放在类中,是为了防止调用DLL的客户端初始化多个日志实例而导致错误
CLock g_Lock;
BOOL g_quitNow = FALSE;
std::queue<LPTSTR> g_myLogQueue;
Expand All @@ -15,33 +15,31 @@ std::queue<LPTSTR> g_myLogQueue;
CLog::CLog(const LPCTSTR lpszLogFilename, const BOOL bPrintQueueSize)
{
// 初始化的时候,先预置值,防止意外重复初始化
ZeroMemory(m_szFilename, MAX_PATH);
ZeroMemory(m_szFilename, BUFSIZ);
m_fpLog = NULL;
m_bPrintQueueSize = FALSE;
m_hWriteThread = NULL;
m_hWriteThreadEvent = NULL;

g_quitNow = FALSE;

assert(_T("日志文件名无效"), lpszLogFilename != NULL && lpszLogFilename[0] != _T('\n'));
StringCchCopy(m_szFilename, MAX_PATH, lpszLogFilename);
assert(lpszLogFilename != NULL && lpszLogFilename[0] != _T('\n') && _T("日志文件名无效"));
StringCchCopy(m_szFilename, BUFSIZ, lpszLogFilename);

_tfopen_s(&m_fpLog, m_szFilename, _T("a"));
assert(_T("无法打开日志文件"), m_fpLog != NULL);
assert(m_fpLog != NULL && _T("无法打开日志文件"));

m_bPrintQueueSize = bPrintQueueSize;
DWORD dwThread = 0;
m_hWriteThread = CreateThread(NULL, 0, m_fnWriteThread, this, CREATE_SUSPENDED, &dwThread);
assert(_T("创建日志线程失败"), m_hWriteThread != NULL);
assert(m_hWriteThread != NULL && _T("创建日志线程失败"));
// 降低日志写入线程的优先级
SetThreadPriority(m_hWriteThread, THREAD_PRIORITY_LOWEST);
ResumeThread(m_hWriteThread);

m_hWriteThreadEvent = NULL;

// 自动复原,初始状态为无信号状态,无信号就代表无日志
m_hWriteThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
assert(_T("线程通信机制异常"), m_hWriteThreadEvent != NULL);
assert(m_hWriteThreadEvent != NULL && _T("线程通信机制异常"));
}

CLog::~CLog()
Expand Down Expand Up @@ -202,10 +200,10 @@ DWORD CLog::m_fnWriteThread(LPVOID lpParam)
continue;
}

int count = g_myLogQueue.size();
size_t count = g_myLogQueue.size();
if (pLogInstance->m_bPrintQueueSize)
{
_ftprintf_s(pLogInstance->m_fpLog, _T("queue size:%u\n"), count);
_ftprintf_s(pLogInstance->m_fpLog, _T("queue size:%zu\n"), count);
}

LPTSTR lpszMsg = NULL;
Expand All @@ -217,7 +215,7 @@ DWORD CLog::m_fnWriteThread(LPVOID lpParam)
g_Lock.Unlock();

if (lpszMsg != NULL) {
int len = _tcslen(lpszMsg);
size_t len = _tcslen(lpszMsg);
_ftprintf_s(pLogInstance->m_fpLog, lpszMsg);
if (lpszMsg[len - 1] != _T('\n'))
_ftprintf_s(pLogInstance->m_fpLog, _T("\n"));
Expand Down
2 changes: 1 addition & 1 deletion LogUtil/CLogImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CLog : public ILog
size_t warning(__in_opt const TCHAR *fmt, ...);
void print_queue_size(__in const BOOL bPrintQueueSize);
private:
TCHAR m_szFilename[MAX_PATH];
TCHAR m_szFilename[BUFSIZ];
FILE * m_fpLog;
BOOL m_bPrintQueueSize;
HANDLE m_hWriteThread;
Expand Down
Binary file modified LogUtil/LogUtil.rc
Binary file not shown.
Binary file added LogUtilMan v1.0.1.8.rar
Binary file not shown.
20 changes: 10 additions & 10 deletions TestCase/TestCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#define REFERENCE_DLL
#include "..\LogUtil\ILog.h"

int nThreadCount = 100;
int nRunTimes = 200;
unsigned int nThreadCount = 100;
unsigned int nRunTimes = 200;
BOOL bPrintQueueSize = TRUE;

DWORD test();
Expand Down Expand Up @@ -63,13 +63,13 @@ DWORD WINAPI MyThreadFunction(LPVOID lpParam)
}

DWORD dwStart = GetTickCount();
int r = 0;
for (long i = 0; i < nRunTimes; i++)
size_t r = 0;
for (size_t i = 0; i < nRunTimes; i++)
{
r = ptd->op->info(_T("Thread %d example random log message(%d) for dll model testcase"), ptd->dwThreadId, i);
r = ptd->op->info(_T("Thread %ld example random log message(%zu) for dll model testcase"), ptd->dwThreadId, i);
if (!r)
{
printf("Thread %d of message %d error\n", ptd->dwThreadId, i);
printf("Thread %ld of message %zu error\n", ptd->dwThreadId, i);
}
}
// printf("Test Thread %d DONE!!!\n", ptd->dwThreadId);
Expand Down Expand Up @@ -117,7 +117,7 @@ DWORD test()
memset(phThreadArray, 0, sizeof(DWORD) * nThreadCount);
// 所有线程全部就绪
DWORD dwStart = GetTickCount();
for (long i = 0; i < nThreadCount; i++)
for (unsigned int i = 0; i < nThreadCount; i++)
{
ptd[i].dwThreadId = i;
ptd[i].op = fpGetClassObject(_T("text.log"));
Expand All @@ -135,7 +135,7 @@ DWORD test()
}
}
// ptd[0].op->print_queue_size(TRUE);
for (long i = 0; i < nThreadCount; i++)
for (unsigned int i = 0; i < nThreadCount; i++)
{
if (phThreadArray[i] != NULL)
{
Expand All @@ -147,7 +147,7 @@ DWORD test()
WaitForMultipleObjects(nThreadCount, phThreadArray, TRUE, INFINITE);

// 关闭句柄
for (long i = 0; i < nThreadCount; i++)
for (unsigned int i = 0; i < nThreadCount; i++)
{
if (phThreadArray[i] != NULL)
{
Expand All @@ -156,7 +156,7 @@ DWORD test()
}
// 等待日志处理任务全部完成
DWORD time = 0;
for (long i = 0; i < nThreadCount; i++)
for (unsigned int i = 0; i < nThreadCount; i++)
{
time < ptd[i].dwPushTime ? time = ptd[i].dwPushTime : 0;
fpReleaseClassObject(ptd[i].op);
Expand Down

0 comments on commit b087e4e

Please sign in to comment.