dll 写日志 互斥同步无效
dll 被加载到不同的进程中,日志文件是同一个
用互斥量 进行同步,结果是无法同步,日志信息总是会相互覆盖
CMutex g_Mutex(FALSE,_T("Mutex1"));
/*
* 日志初始化
*/
void LogInit()
{
if (g_logFile.Open(g_strAppPath + _T("\\ABPlugin_log.txt"), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyNone))
{
ULONGLONG len = g_logFile.GetLength();
if (len >= 10000000)
{
char *buf = (char *)malloc(65536);
UINT bufLen = g_logFile.Read(buf, 65536);
g_logFile.Close();
if (g_logFile.Open(g_strAppPath + _T("\\ABPlugin_log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
{
g_logFile.Write("\xFF\xFE", 2);
if (bufLen > 0)
{
g_logFile.Write(buf, bufLen);
}
}
free(buf);
}
else
{
if (len == 0)
{
g_logFile.Write("\xFF\xFE", 2);
}
g_logFile.SeekToEnd();
}
}
}
/*
* 消息日志
* @param t_fileName: 文件名
* @param t_line: 行号
* @param t_log: 日志信息
*/
void LogInfo(const TCHAR *t_fileName, unsigned long t_line, const TCHAR *t_log)
{
if (t_log == NULL)
{
return;
}
g_Mutex.Lock();
if (g_logFile.m_hFile != CFile::hFileNull)
{
CString logStr;
CTime ctm = CTime::GetCurrentTime();
logStr.Format(_T("info %04d-%02d-%02d %02d:%02d:%02d "),
ctm.GetYear(), ctm.GetMonth(), ctm.GetDay(), ctm.GetHour(), ctm.GetMinute(), ctm.GetSecond());
g_logFile.Write(logStr, logStr.GetLength() * sizeof(TCHAR));
g_logFile.Write(t_log, _tcslen(t_log) * sizeof(TCHAR));
}
g_Mutex.Unlock();
}