我是做地鐵售票機開發的,程式是跑在工控機上的,有多個執行緒,包含業務操作,硬體操作,資料庫操作,與上層通信模塊等,其中硬體操作執行緒,呼叫封裝的動態庫與硬體模塊通信,有近9個模塊,模塊與硬體使用串口通信。我是每個執行緒記錄一個日志檔案,日志類是用同一個。有一天突然發現單個執行緒的日志里混入了其他執行緒的日志,而且還是一年前的日志,可我的日志只保存近兩個月的,不知道從哪里來的,另外程式還能正常使用,但從出問題之后就再也沒有日志記錄,后續對賬才發現不對,有大神幫忙看下,是什么原因導致的。
開發語言:C/C++
開發工具:VS2010
工控機平臺:Win7 嵌入式版本
以下是日志截圖,每個檔案是一個執行緒列印的,上半部分還是正常的,到下半部分就亂了

2020-03-09 03:58:00.612 <getAllParamVersion> type '9210', current '1900010101', future '9999123199', update date '19000101023000'
2020-03-09 03:58:00.612 <getAllParamVersion> type '9220', current '1900010101', future '9999123199', update date '19000101023000'
2020-03-09 03:58:00.612 <getAllParamVersion> type '9230', current '1900010101', future '9999123199', update date '19000101023000'
2020-03-09 03:58:00.614 <getAllParamVersion> type '9240', current '1900010101', future '9999123199', update date '19000101023000'
2020-03-09 03:58:00.614 <getAllParamVersion> type '9290', current '2019100801', future '9999123199', update date '20191126145903'
-27 06:58:35.327 <TCP> Send [0x40100C0C] to SC: EB3500000003
2019-11-27 06:58:39.960 <sendToSC> message(0x00100C0C) ,m_waitAckFlag:0
2019-11-27 06:58:39.960 <TCP> Send [0x00100C0C] to SC: EB3400000003
2019-11-27 06:58:39.981 <TCP> Recv from SC: EB3500000003
2019-11-27 06:58:39.981 <TCP> SLE client: 6 byte(s) converted, serial id is 00.
2019-11-27 06:58:39.981 <SC> receive link test response from client [0]
2019-11-27 06:58:44.754 <TCP> Recv from SC: EB3400000003
2019-11-27 06:58:44.754 <TCP> SLE client: 6 byte(s) converted, serial id is 00.

2020-03-09 07:35:09.394 Get clock offset=0.811569, abs=0.811569
2020-03-09 07:35:09.394 Did not need time sync
2020-03-09 07:35:39.313 Get clock offset=0.812896, abs=0.812896
2020-03-09 07:35:39.313 Did not need time sync
R1LAST,HOPPER2LAST) values('2019-12-26 23:39:34',34,0,0);
2019-12-26 23:39:34.681 insert into SETTLE_SJTSTAT(STARTSETTLETIME,SEQNO,HOPPER1LAST,HOPPER2LAST) values('2019-12-26 23:39:34',34,0,0);
2019-12-26 23:39:34.702 insert into SETTLE_SJTSTAT(STARTSETTLETIME,SEQNO,HOPPER1LAST,HOPPER2LAST) values('2019-12-26 23:39:34',34,0,0);
2019-12-26 23:39:34.732 insert into SETTLE_CASHSTAT(STARTSETTLETIME,SEQNO,HOPPER1LAST,HOPPER2LAST,NOTE1LAST,NOTE2LAST,NOTERECYCLE,NOTEBOXREMAIN,COINBOXREMAIN) values('2019-12-26 23:39:34',34,0,0,0,0,0,0,0);
以下是日志列印代碼
bool CSleLog::openLogFile()
{
if(NULL != m_logFile)
fclose(m_logFile);
string logDate = TimeUtil::getCurrentDate();
m_logDate = logDate;
string log_base_path = m_path;
if (!File::exists(log_base_path))
{
File::makePath(log_base_path);
}
log_base_path += logDate;
if (!File::exists(log_base_path))
{
File::makePath(log_base_path);
}
string filename = log_base_path + "\\"+ m_logModule + ".log";
m_logFile = fopen(filename.c_str(), "at+");
if(NULL != m_logFile)
{
string headStr = m_logModule + " " + m_version + "\n";
fprintf(m_logFile, headStr.c_str());
fflush(m_logFile);
}
return (NULL != m_logFile);
}
void CSleLog::info(const char* format, ...)
{
lock_type lock(log_mutex);
enum{
max_buf_length = 8192,
};
// if(NULL == m_logFile)
// return;
// m_currLogCount++;
// if(m_currLogCount > max_Log_Line_Count)
// {
// openLogFile();
// m_currLogCount = 1;
// }
try
{
string logDate = TimeUtil::getCurrentDate();
if(m_logDate.compare(logDate) != 0)
{
openLogFile();
//m_currLogCount = 1;
}
if(NULL == m_logFile)
return;
char buffer[max_buf_length + 1] = {0};
va_list ap;
va_start(ap, format);
_vsnprintf(buffer, max_buf_length, format, ap);
va_end(ap);
string content(buffer);
fprintf(m_logFile, "%s %s", TimeUtil::getCurrentTimeStamp().c_str(), content.c_str());
fflush(m_logFile);
}
catch (...){}
}
uj5u.com熱心網友回復:
log push異步加鎖訊息佇列,里面干活即可uj5u.com熱心網友回復:
我想搞明白,為什么會出現這樣的問題,檔案只會被單個執行緒占用,其他內容是哪里來的,能否詳細解釋下轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/108744.html
標籤:C++ 語言
