📄 logthread.cpp
字号:
{
DBG("%s ", CLSUtil::GetLevelString(*iterlvl).c_str());
}
DBG(",%s): %s\n",
CLSUtil::GetLevelString(Item.LogLevel).c_str(),
bRet?"Yes":"No");
return bRet;
}
/**************************************************************************
* 函数名称: IsMatchedRemarkItem()
* 功能描述: 检查日志记录的备注是否匹配
* 输入参数: const LogItem& Item : 日志记录项
* 输出参数: 无
* 返 回 值: true : 该日志项的备注和本线程的备注匹配, 可以被记录
* false : 该日志项的备注和本线程的备注匹配不匹配, 不可以被记录
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
bool CLogThread::IsMatchedRemarkItem(const LogItem& Item)
{
bool bRet = false;
if (m_RemarkItem.Remark == ""
|| CLSUtil::IsNoCaseStrEqual(m_RemarkItem.Remark,Item.Remark))
{
bRet = true;
}
DBG("(%d)检查日志记录的备注是否匹配(%s, %s): %s\n", m_tid,
m_RemarkItem.Remark.c_str(),
Item.Remark.c_str(),
bRet?"Yes":"No");
return bRet;
}
/**************************************************************************
* 函数名称: FormatItem()
* 功能描述: 格式化日志记录
* 输入参数: const LogItem& Item : 日志记录项
* 输出参数: 无
* 返 回 值: 被格式化后的日志记录, 是一个字符串
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
string CLogThread::FormatItem(const LogItem& Item)
{
stringstream strItem;
strItem << FormatTime(Item.Time) << " ";
strItem << CLSUtil::GetLevelString(Item.LogLevel) << " ";
strItem << Item.Message << " ";
strItem << Item.Remark << " ";
strItem << Item.FileName << " ";
strItem << Item.line;
DBG("(%d)格式化日志记录:%s\n", m_tid, strItem.str().c_str());
return strItem.str();
}
/**************************************************************************
* 函数名称: FormatTime()
* 功能描述: 格式化日志记录时间
* 输入参数: const time_t& ltime : 日志的时间
* 输出参数: 无
* 返 回 值: 被格式化后的日志时间, 是一个字符串
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
/*
const string CLogThread::FormatTime(const time_t& ltime)
{
char tmpbuf[128];
struct tm *logTime = localtime(<ime);
if (0 != strftime( tmpbuf, 128, "%Y/%m/%d %H:%M:%S", logTime))
{
return string(tmpbuf);
}
return "";
}
*/
const string CLogThread::FormatTime(const SYSTEMTIME& ltime)
{
char tmpbuf[128];
if( 0 != sprintf(tmpbuf,"%u/%u/%u %u:%u:%u.%u",ltime.wYear,ltime.wMonth,ltime.wDay,ltime.wHour
,ltime.wMinute,ltime.wSecond,ltime.wMilliseconds))
{
return string(tmpbuf);
}
return "";
}
/**************************************************************************
* 函数名称: ReArrangeLogFiles()
* 功能描述: 更新历史日志文件
* 输入参数: 无
* 输出参数: 无
* 返 回 值: true : 调整成功
* false : 调整失败
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
bool CLogThread::ReArrangeLogFiles(void)
{
bool bRet = false;
//检查文件是否已经打开
if (!m_LogFile.is_open()) //文件未打开
{
DBG("(%d)日志文件未打开\n", m_tid);
//打开已经存在的日志文件
m_LogFile.open(m_strLogFile.c_str(), ios::out |ios::app);
}
if(m_LogFile.is_open())//文件已经打开
{
DBG("(%d)日志文件已经打开\n", m_tid);
//检查文件是否满
if (!IsFileFull())
{
//文件未满,可以写入
bRet = true;
}
else
{
//
//以下处理文件满的情况
//
DBG("(%d)以下处理文件满的情况\n", m_tid);
m_LogFile.close(); //关闭日志文件
//////////////////////////////////////////////////////////////////
// 这里是日志历史文件调整逻辑,重点注意
// 功能:调整日志文件
m_iCurHistoryLogFile++; // 历史日志文件数目 +1
// 如果还没有达到允许的最大历史日志文件数量
// 则,可以向历史日志文件名向量数组中追加新的历史日志文件名
// 否则,直接进行日志文件的调整,不再向历史日志文件名向量数组中追加新的历史日志文件名
if (m_iCurHistoryLogFile <= m_siHistoryAmnt)
{
string strCurLogFileName;
CreateLogFileName(m_iCurHistoryLogFile, strCurLogFileName);
m_vstrHistoryLogFiles.push_back(strCurLogFileName);
}
// 调整历史日志文件
// 策略就是,从 历史日志文件名向量数组 中取文件名,进行文件替换,即用文件N-1更新为文件N...
bRet = true;
if (m_iCurHistoryLogFile == 1) // 第一次
{
}
else // 第二次...
{
vector<string>::iterator iter = m_vstrHistoryLogFiles.end() - 1;
for (; iter != m_vstrHistoryLogFiles.begin(); iter--)
{
string strTempLogFileNameN = *iter;
string strTempLogFileNameNSubOne = *(iter-1);
if (!CLSUtil::MoveFileabc(strTempLogFileNameNSubOne.c_str(), strTempLogFileNameN.c_str()))
{
bRet = false;
}
}
}
// 调整当前日志满的情况
string strFirstLogFile = *(m_vstrHistoryLogFiles.begin());
if (!CLSUtil::MoveFileabc(m_strLogFile, strFirstLogFile.c_str()))
{
bRet = false;
}
// 如果历史日志文件调整成功,则重新打开当前日常日志,进行后续的日志处理
if (bRet)
{
DBG("(%d)重命名日志文件: 成功\n", m_tid);
m_LogFile.open(m_strLogFile.c_str(), ios::out | ios::trunc);
m_iTotalLines = 0;
}
else
{
DBG("(%d)重命名日志文件: 失败\n", m_tid);
}
//
////////////////////////////////////////////////////////////////
}
}
DBG("(%d)调整日志文件:%s\n", m_tid, bRet?"Yes":"No");
return bRet;
}
/**************************************************************************
* 函数名称: IsFileFull()
* 功能描述: 检查文件是否满
* 输入参数: 无
* 输出参数: 无
* 返 回 值: true : 日志文件满
* false : 日志文件未满
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
bool CLogThread::IsFileFull(void)
{
bool bRet = (m_iTotalLines >= m_siMaxLineNum);
DBG("(%d)日志文件是否满:%s\n", m_tid, bRet?"Yes":"No");
return bRet;
}
/**************************************************************************
* 函数名称: itoaWithSpecifiedWidth()
* 功能描述: 将一个整型转换为指定宽度的字符串
* 例如:如果指定宽度为3位,则将 1 ----> "001"
* 输入参数: int iNum : 待转换的整数
* int iWidth : 指定的宽度
* 输出参数: string& strNum : 转换后的结果
* 返 回 值: 无
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
void CLogThread::itoaWithSpecifiedWidth(int iNum, string& strNum, int iWidth)
{
char chWidth[20] = {0};
_itoa(iWidth, chWidth, 10);
string strFormat("%0d");
strFormat.insert(2, chWidth);
char chNum[100] = {0};
sprintf(chNum, strFormat.c_str(), iNum);
strNum = chNum;
}
/**************************************************************************
* 函数名称: CreateLogFileName()
* 功能描述: 生成历史日志文件名称
* 输入参数: int iLogFileNo : 历史日志文件编号
* 输出参数: string& strLogFileName : 历史日志文件名称
* 返 回 值: 无
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
void CLogThread::CreateLogFileName(int iLogFileNo, string& strLogFileName)
{
string strLogFileNo;
char chTemp[100] = {0};
_itoa(m_siHistoryAmnt, chTemp, 10);
itoaWithSpecifiedWidth(iLogFileNo, strLogFileNo, (int)strlen(chTemp));
strLogFileName = m_sstrLogPath + m_sstrLogFileNamePre
+ m_RemarkItem.Remark
+ "~" + strLogFileNo + m_sstrLogFileNameSuf;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -