Commit de552c87 by 刘帅

日志标准化

去掉内容中的换行、添加交易号字段
[时间] [级别] [订单号] [日志内容]
parent a7de2ce8
......@@ -44,13 +44,13 @@ typedef QVector<DestinationPtr> DestinationList;
static const char TraceString[] = "TRACE";
static const char DebugString[] = "DEBUG";
static const char InfoString[] = "INFO ";
static const char WarnString[] = "WARN ";
static const char InfoString[] = "INFO";
static const char WarnString[] = "WARN";
static const char ErrorString[] = "ERROR";
static const char FatalString[] = "FATAL";
// not using Qt::ISODate because we need the milliseconds too
static const QString fmtDateTime("yyyy-MM-dd hh:mm:ss.zzz");
static const QString fmtDateTime("yyyy-MM-dd hh:mm:ss,zzz");
static const char* LevelToText(Level theLevel)
{
......@@ -217,9 +217,10 @@ Level Logger::loggingLevel() const
void Logger::Helper::writeToLog()
{
const char* const levelName = LevelToText(level);
const QString completeMessage(QString("%1 %2 %3")
.arg(levelName)
const QString completeMessage(QString("%1 %2 %3 %4")
.arg(QDateTime::currentDateTime().toString(fmtDateTime))
.arg(levelName)
.arg(trans_id)
.arg(buffer)
);
......
......@@ -66,7 +66,9 @@ public:
level(logLevel),
qtDebug(&buffer) {}
~Helper();
QDebug& stream(){ return qtDebug; }
QDebug& stream(QString _trans_id){
trans_id = _trans_id;
return qtDebug; }
private:
void writeToLog();
......@@ -74,6 +76,7 @@ public:
Level level;
QString buffer;
QDebug qtDebug;
QString trans_id;
};
private:
......@@ -94,24 +97,24 @@ private:
//! Logging macros: define QS_LOG_LINE_NUMBERS to get the file and line number
//! in the log output.
#ifndef QS_LOG_LINE_NUMBERS
#define QLOG_TRACE() \
#define QLOG_TRACE(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream()
#define QLOG_DEBUG() \
else QsLogging::Logger::Helper(QsLogging::TraceLevel).stream(TRANS_ID)
#define QLOG_DEBUG(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::DebugLevel) {} \
else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream()
#define QLOG_INFO() \
else QsLogging::Logger::Helper(QsLogging::DebugLevel).stream(TRANS_ID)
#define QLOG_INFO(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::InfoLevel) {} \
else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream()
#define QLOG_WARN() \
else QsLogging::Logger::Helper(QsLogging::InfoLevel).stream(TRANS_ID)
#define QLOG_WARN(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::WarnLevel) {} \
else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream()
#define QLOG_ERROR() \
else QsLogging::Logger::Helper(QsLogging::WarnLevel).stream(TRANS_ID)
#define QLOG_ERROR(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::ErrorLevel) {} \
else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream()
#define QLOG_FATAL() \
else QsLogging::Logger::Helper(QsLogging::ErrorLevel).stream(TRANS_ID)
#define QLOG_FATAL(TRANS_ID) \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::FatalLevel) {} \
else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream()
else QsLogging::Logger::Helper(QsLogging::FatalLevel).stream(TRANS_ID)
#else
#define QLOG_TRACE() \
if (QsLogging::Logger::instance().loggingLevel() > QsLogging::TraceLevel) {} \
......
......@@ -30,7 +30,7 @@ int main(int argc, char *argv[])
return 0;
}
InitLogger();
QLOG_INFO()<<"start FmclientUi...";
QLOG_INFO(DEFAULT_TRANS_ID)<<"start FmclientUi...";
Widget w;
scanner s;
QFmClient client;
......
......@@ -50,6 +50,7 @@ QFmClient::QFmClient(QObject *parent) : QThread(parent)
{
_endflag = false;
_fmMSS = 1024;
mTransId = DEFAULT_TRANS_ID;
}
bool QFmClient::Init(QString proxy_IP, unsigned short proxy_port, unsigned short listen_prot, unsigned short long_timeout, unsigned short short_timeout)
......@@ -65,7 +66,7 @@ bool QFmClient::Init(QString proxy_IP, unsigned short proxy_port, unsigned short
char clientVerInfo[32];
if(0 == cfgFile)
{
QLOG_ERROR()<<"open cfg file failed!\r\n";
QLOG_ERROR(DEFAULT_TRANS_ID)<<"open cfg file failed!";
return false;
}
......@@ -172,12 +173,12 @@ bool QFmClient::WaitForConnectReqFromPos()
_acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size);
if(-1 == _acceptedSock)
{
qDebug() << "accept socket fail!\r\n";
qDebug() << "accept socket fail!";
return false;
}
qDebug() << "accept ok, ZF Client get connection from POS!\r\n";
qDebug() << "accept ok, ZF Client get connection from POS!";
return true;
}
......@@ -301,7 +302,7 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength)
int QFmClient::WaitAndReadDataFromPos(int type, int flag)
{
qDebug() << "wait and receive data from POS...\r\n";
qDebug() << "wait and receive data from POS...";
_rcvLength = 0;
......@@ -314,9 +315,9 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
if (0 >= (recbytes = RecvSockData(_acceptedSock, _tempbuf, (MAX_BUF_LEN - 1))))
{
#ifdef WIN32
qDebug() << "recv() return : " << recbytes << ";error : " << WSAGetLastError() << ";\r\n";
qDebug() << "recv() return : " << recbytes << ";error : " << WSAGetLastError() << ";";
#endif
qDebug() << "read data from socket " << _acceptedSock << " fail!\r\n";
qDebug() << "read data from socket " << _acceptedSock << " fail!";
return -1;
}
......@@ -336,15 +337,15 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
if(1 == flag)
precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes);
qDebug() << "received data from pos " << _acceptedSock << " : \n" << precvBuf;
QLOG_INFO()<< "received data from pos " << _acceptedSock << " : \n" << precvBuf;
qDebug() << "received data from pos " << _acceptedSock << " : " << precvBuf;
QLOG_INFO(DEFAULT_TRANS_ID)<< "received data from pos " << _acceptedSock << " : " << precvBuf;
while(0 < recbytes)
{
finLen = CheckIsCompleteJsonData(&checkCount, precvBuf + prosLen);
if (0 != checkCount || ((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen))
{
qDebug() << "checkCount out!\r\n";
qDebug() << "checkCount out!";
return -1;
}
......@@ -396,7 +397,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
if(json_error.error != QJsonParseError::NoError || !doc.isObject())
{
qDebug() << "error request json data!\r\n";
qDebug() << "error request json data!";
return -1;
}
......@@ -423,6 +424,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
}
}
object["trans_id"] = trans_id;
mTransId = trans_id;
}
qDebug()<<object;
......@@ -450,7 +452,7 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
if(!object.contains("checkValue") || !object["checkValue"].isDouble())
{
qDebug() << "checkValue failed!\r\n";
qDebug() << "checkValue failed!";
flag = -1;
break;
}
......@@ -599,7 +601,7 @@ int QFmClient::SendCtrlInfo2Proxy()
if (sendRlt != MAX_CTRL_INFO_LEN)
{
#ifdef WIN32
qDebug() << "recv() return : " << sendRlt << ";error : " << WSAGetLastError() << ";\r\n";
qDebug() << "recv() return : " << sendRlt << ";error : " << WSAGetLastError() << ";";
#endif
return -1;
}
......@@ -618,9 +620,9 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
if (0 >= (recbytes = RecvSockData(sock, _tempbuf, (MAX_BUF_LEN - 1))))
{
#ifdef WIN32
qDebug() << "recv() return : " << recbytes << ";error : " << WSAGetLastError() << ";\r\n";
qDebug() << "recv() return : " << recbytes << ";error : " << WSAGetLastError() << ";";
#endif
qDebug() << "read data from socket "<< sock <<" fail!\r\n";
qDebug() << "read data from socket "<< sock <<" fail!";
return -1;
}
......@@ -642,8 +644,8 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes);
}
qDebug() << "received data from Proxy " << sock << " :\n " << precvBuf;
QLOG_INFO() << "received data from Proxy " << sock << " :\n " << precvBuf;
qDebug() << "received data from Proxy " << sock << " : " << precvBuf;
QLOG_INFO(DEFAULT_TRANS_ID) << "received data from Proxy " << sock << " : " << precvBuf;
while(0 < recbytes)
{
......@@ -651,7 +653,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
if (((0 == checkCount) && ( '}' != precvBuf[ prosLen + finLen - 1])) || ((MAX_BUF_LEN - 1) < curPos + finLen))
{
qDebug() << "checkCount out!\r\n";
qDebug() << "checkCount out!";
return -1;
}
......@@ -699,28 +701,28 @@ int QFmClient::SendData2ZhProxyAndWaitRspData()
if (rlt != 0)
{
#ifdef WIN32
qDebug() << "send data to ZhProxy return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
QLOG_ERROR()<< "send data to ZhProxy return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
qDebug() << "send data to ZhProxy return : " << rlt << ";error : " << WSAGetLastError() << ";";
QLOG_ERROR(mTransId)<< "send data to ZhProxy return : " << rlt << ";error : " << WSAGetLastError() << ";";
#endif
qDebug() << "send data to ZhProxy failed!\r\n";
qDebug() << "send data to ZhProxy failed!";
return -1;
}
qDebug() << "wait and receive data from ZH Proxy...\r\n";
qDebug() << "wait and receive data from ZH Proxy...";
#ifdef AES_ENCRYPT
if( recvAESDataFromProxy(_zhProxySock, _recvbuf, MAX_BUF_LEN) != 0)
{
qDebug() << "receive data or check data failed\r\n";
QLOG_ERROR() << "receive data or check data failed\r\n";
qDebug() << "receive data or check data failed";
QLOG_ERROR(mTransId) << "receive data or check data failed";
return -1;
}
#else
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0))
{
qDebug() << "receive data or check data failed\r\n";
QLOG_ERROR() << "receive data or check data failed\r\n";
qDebug() << "receive data or check data failed";
QLOG_ERROR(mTransId) << "receive data or check data failed";
return -1;
}
#endif
......@@ -737,7 +739,7 @@ int QFmClient::CheckRecvedData()
if(json_error.error != QJsonParseError::NoError || !doc.isObject())
{
qDebug() << "error request json data!\r\n";
qDebug() << "error request json data!";
return -1;
}
......@@ -753,7 +755,7 @@ int QFmClient::CheckRecvedData()
if(file.open(QFile::WriteOnly))
{
QString PrintInfo = json["ext"].toObject()["print"].toString();
QLOG_INFO() << "print:\r\n" <<PrintInfo << "\r\n";
QLOG_INFO(DEFAULT_TRANS_ID) << "print:" <<PrintInfo << "";
file.write(PrintInfo.toUtf8());
file.close();
}
......@@ -783,12 +785,12 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos()
if (rlt != length)
{
#ifdef WIN32
qDebug() << "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
QLOG_ERROR()<< "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";\r\n";
qDebug() << "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";";
QLOG_ERROR(mTransId)<< "send to pos return : " << rlt << ";error : " << WSAGetLastError() << ";";
#endif
return -1;
}
QLOG_INFO() <<"send to pos:\n"<<_recvbuf;
QLOG_INFO(mTransId) <<"send to pos:"<<_recvbuf;
return 0;
}
......@@ -813,7 +815,7 @@ void QFmClient::BackupPosReq(char * req)
if(toSendLength == 0)
{
OutputDebugStringA("encrpt failed!");
QLOG_ERROR() << "encrpt failed!";
QLOG_ERROR(mTransId) << "encrpt failed!";
delete [] out;
return;
}
......@@ -854,7 +856,7 @@ int QFmClient::sendAESData2Proxy(int sock, char *buf, int length)
if(toSendLength == 0)
{
OutputDebugStringA("encrpt failed!");
QLOG_ERROR() << "encrpt failed!";
QLOG_ERROR(DEFAULT_TRANS_ID) << "encrpt failed!";
delete [] out;
return -1;
}
......@@ -873,7 +875,7 @@ int QFmClient::sendAESData2Proxy(int sock, char *buf, int length)
if(res == SOCKET_ERROR)
{
int err = WSAGetLastError();
QLOG_ERROR() << "send failed! code:%d" << err;
QLOG_ERROR(DEFAULT_TRANS_ID) << "send failed! code:%d" << err;
rlt = -1;
break;
}
......@@ -882,7 +884,7 @@ int QFmClient::sendAESData2Proxy(int sock, char *buf, int length)
if(curSendLength != toSendLength)
{
rlt = -1;
QLOG_ERROR() << ("cannot send all data!");
QLOG_ERROR(DEFAULT_TRANS_ID) << ("cannot send all data!");
}
delete [] out;
return rlt;
......@@ -1002,7 +1004,7 @@ void QFmClient::run()
CloseSocket(&_acceptedSock);
CloseSocket(&_listenSock);
CheckRollBackData();
mTransId = DEFAULT_TRANS_ID;
if(!StartListenport())
{
qDebug() << "Start Listen port : " << _clientPort <<" failed";
......@@ -1014,7 +1016,7 @@ void QFmClient::run()
do
{
int rlt, needBackup;
mTransId = DEFAULT_TRANS_ID;
if (!WaitForConnectReqFromPos())
{
break;
......@@ -1061,7 +1063,7 @@ void QFmClient::run()
evt.exec();
object["fmId"] = _refundTransId;
QLOG_ERROR() << "refund fmId: " << _refundTransId;
QLOG_ERROR(mTransId) << "refund fmId: " << _refundTransId;
}
if (!object.contains("station_id")) {
//! 给到客户的文档中, 缺少 station_id 字段
......@@ -1072,7 +1074,7 @@ void QFmClient::run()
if (0 != Try2ConnectZhProxy())
{
qDebug() << "connect to proxy failed";
QLOG_ERROR() << "connect to proxy failed";
QLOG_ERROR(mTransId) << "connect to proxy failed";
CloseSocket(&_acceptedSock);
if(reqtype == 72 || reqtype == 101)
{
......@@ -1096,7 +1098,7 @@ void QFmClient::run()
QString temp = QString(QJsonDocument(object).toJson());
temp = temp.simplified();
strcpy(_sendbuf, temp.toStdString().c_str());
QLOG_INFO()<<"send to ZhProxy:\n"<<_sendbuf;
QLOG_INFO(mTransId)<<"send to ZhProxy:"<<_sendbuf;
rlt = SendData2ZhProxyAndWaitRspData();
......@@ -1129,14 +1131,14 @@ void QFmClient::run()
}while(!isInterruptionRequested());
qDebug() << "Exit FmclientUi!\r\n";
OutputDebugString(L"Exit FmclientUi!\r\n");
qDebug() << "Exit FmclientUi!";
OutputDebugString(L"Exit FmclientUi!");
#ifdef WIN32
WSACleanup();
#endif
QLOG_INFO()<< "Exit FmclientUi!\r\n\n";
QLOG_INFO(DEFAULT_TRANS_ID)<< "Exit FmclientUi!";
}
void QFmClient::quit()
......@@ -1195,7 +1197,7 @@ bool QFmClient::StartListenport()
}
qDebug()<<"listen "<<_clientPort;
QLOG_INFO()<<"listen at "<<_clientPort;
QLOG_INFO(DEFAULT_TRANS_ID)<<"listen at "<<_clientPort;
return true;
}
......@@ -1211,8 +1213,8 @@ void QFmClient::CheckRollBackData()
return;
}
qDebug() << "need to roll back data...\r\n";
QLOG_INFO() << "need to roll back data...\r\n";
qDebug() << "need to roll back data...";
QLOG_INFO(mTransId) << "need to roll back data...";
fseek(file, 0, SEEK_SET);
fseek(file, 0, SEEK_END);
......@@ -1226,7 +1228,7 @@ void QFmClient::CheckRollBackData()
if (1 != rlt || 0 >= readLen)
{
qDebug() << "roll back file error, file length error!\r\n";
qDebug() << "roll back file error, file length error!";
remove(filename.toStdString().c_str());
return ;
}
......@@ -1242,7 +1244,7 @@ void QFmClient::CheckRollBackData()
if(rlt == 0)
{
OutputDebugStringA("decrypt failed!");
QLOG_ERROR() << "decrypt failed!";
QLOG_ERROR(DEFAULT_TRANS_ID) << "decrypt failed!";
return;
}
......@@ -1253,7 +1255,7 @@ void QFmClient::CheckRollBackData()
if(json_error.error != QJsonParseError::NoError || !doc.isObject())
{
qDebug() << "error rbk json data!\r\n";
qDebug() << "error rbk json data!";
remove(filename.toStdString().c_str());
return ;
}
......@@ -1268,8 +1270,8 @@ void QFmClient::CheckRollBackData()
strcpy(_sendbuf, temp.toStdString().c_str());
qDebug() << "rollback data:" << _sendbuf;
qDebug() << "\r\n";
QLOG_INFO() << "rollback data:" << _sendbuf << "\r\n";
qDebug() << "";
QLOG_INFO(mTransId) << "rollback data:" << _sendbuf << "";
do
{
......@@ -1277,33 +1279,33 @@ void QFmClient::CheckRollBackData()
if (0 != Try2ConnectZhProxy())
{
qDebug() << "roll back data: connect ZH Proxy failed, will try after 5 seconds...\r\n";
QLOG_INFO() << "roll back data: connect ZH Proxy failed, will try after 5 seconds...\r\n";
qDebug() << "roll back data: connect ZH Proxy failed, will try after 5 seconds...";
QLOG_INFO(mTransId) << "roll back data: connect ZH Proxy failed, will try after 5 seconds...";
continue;
}
#ifndef AES_ENCRYPT
if (0 != SendCtrlInfo2Proxy())
{
qDebug() << "roll back data: send ctrl info 2 ZH Proxy failed, will try after 5 seconds...\r\n";
QLOG_INFO() << "roll back data: send ctrl info 2 ZH Proxy failed, will try after 5 seconds...\r\n";
qDebug() << "roll back data: send ctrl info 2 ZH Proxy failed, will try after 5 seconds...";
QLOG_INFO(mTransId) << "roll back data: send ctrl info 2 ZH Proxy failed, will try after 5 seconds...";
continue;
}
#endif
if (0 != SendData2ZhProxyAndWaitRspData())
{
qDebug() << "roll back data: send data to proxy fail, will try after 5 seconds...\r\n";
QLOG_INFO() << "roll back data: send data to proxy fail, will try after 5 seconds...\r\n";
qDebug() << "roll back data: send data to proxy fail, will try after 5 seconds...";
QLOG_INFO(mTransId) << "roll back data: send data to proxy fail, will try after 5 seconds...";
continue;
}
//对冲正结果进行判断
QByteArray recvdata(_recvbuf);
QLOG_INFO() << "roll back reply: " << _recvbuf;
QLOG_INFO(mTransId) << "roll back reply: " << _recvbuf;
QJsonParseError json_error;
QJsonDocument doc = QJsonDocument::fromJson(recvdata, &json_error);
if(json_error.error != QJsonParseError::NoError || !doc.isObject())
{
qDebug() << "error request json data!\r\n";
qDebug() << "error request json data!";
continue;
}
QJsonObject object = doc.object();
......@@ -1314,8 +1316,8 @@ void QFmClient::CheckRollBackData()
}
}while(1);
qDebug() << "roll back ok!\r\n";
QLOG_INFO() << "roll back ok!\r\n";
qDebug() << "roll back ok!";
QLOG_INFO(mTransId) << "roll back ok!";
CloseSocket(&_zhProxySock);
remove(filename.toStdString().c_str());
......
......@@ -22,6 +22,7 @@
#define MAX_BUF_LEN 25600
#define MAX_REQ_COUNT 25600
#define ROLL_BACK_FILE_NAME "fmclient.rbk"
#define DEFAULT_TRANS_ID "invalid"
class QFmClient : public QThread
{
......@@ -117,6 +118,7 @@ public:
QString _refundTransId;
QString mTransId; //for log
static unsigned int s_reqCount;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment