UpLogFSM.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include "stdafx.h"
  2. #include "UpLogFSM.h"
  3. #include "Event.h"
  4. UpLogFSM::UpLogFSM() : m_pConnection(NULL),m_isSendLog(false),m_branchSendLog(false),m_logTypeList("")
  5. {
  6. InitializeCriticalSection(&cs);
  7. InitializeCriticalSection(&csRec);
  8. m_SendMode=0;//默认是单条传送
  9. m_iSub=0;//订阅总数
  10. m_iRec=0;//队列接收总数
  11. m_iSend=0;//队列发送总数
  12. m_iThrow=0;//队列丢弃总数
  13. m_iFail=0;//发送失败总次数
  14. m_iSucc=0;//发送成功总次数
  15. m_iEachSend=0;//每次连接发送的数量
  16. m_iEachMaxSend=100;//每次连接最大发送次数
  17. }
  18. UpLogFSM::~UpLogFSM()
  19. {
  20. EnterCriticalSection(&cs);
  21. while(true){
  22. if(m_logList.empty()){
  23. break;
  24. }else{
  25. log_t* dLog = (log_t*)(*m_logList.begin());//集合首个元素
  26. m_logList.erase(m_logList.begin());//集合删除首元素
  27. delLog(dLog);//释放内存
  28. }
  29. }
  30. LeaveCriticalSection(&cs);
  31. DeleteCriticalSection(&cs);
  32. DeleteCriticalSection(&csRec);
  33. }
  34. void UpLogFSM::OnStateTrans( int iSrcState, int iDstState )
  35. {
  36. printDebugLog(CSimpleStringA::Format("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState)));
  37. //Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  38. }
  39. void UpLogFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  40. {
  41. }
  42. ErrorCodeEnum UpLogFSM::OnInit()
  43. {
  44. AddStateHooker(this);
  45. //......
  46. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  47. CSmartPointer<IConfigInfo> spConfig;
  48. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  49. if (Error_Succeed == Error)
  50. {
  51. int branchSendLog = 0;
  52. Error = spConfig->ReadConfigValueInt("UpLog", "BranchSendLog", branchSendLog);//全分行是否上传日志
  53. if (Error_Succeed == Error)
  54. {
  55. //Dbg("get BranchSendLog=%d from CenterSetting.ini", branchSendLog);
  56. printDebugLog(CSimpleStringA::Format("get BranchSendLog=%d from CenterSetting.ini", branchSendLog));
  57. m_branchSendLog = branchSendLog==1 ? true : false ;//整个分行所有终端是否需要上传日志标志位
  58. }
  59. else
  60. {
  61. //Dbg("get [UpLog].BranchSendLog from CenterSetting.ini failed");
  62. printDebugLog(CSimpleStringA::Format("get [UpLog].BranchSendLog from CenterSetting.ini failed"));
  63. }
  64. if(m_branchSendLog){
  65. m_isSendLog=true;
  66. }else{
  67. CSimpleStringA TerminalList ="";
  68. Error = spConfig->ReadConfigValue("UpLog", "TerminalList", TerminalList);//某些终端是否上传日志
  69. if (Error_Succeed == Error)
  70. {
  71. //Dbg("get TerminalList=%s from CenterSetting.ini", TerminalList);
  72. printDebugLog(CSimpleStringA::Format("get TerminalList=%s from CenterSetting.ini", TerminalList));
  73. CSystemStaticInfo systemStaticInfo;
  74. auto rc = this->m_pEntity->GetFunction()->GetSystemStaticInfo(systemStaticInfo);
  75. assert(rc == Error_Succeed);
  76. CAutoArray<CSimpleStringA> tList = TerminalList.Split('|');
  77. for(int i=0; i<tList.GetCount(); i++){
  78. if(tList[i]==systemStaticInfo.strTerminalID){
  79. m_isSendLog=true;
  80. break;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. //Dbg("get [UpLog].TerminalList from CenterSetting.ini failed");
  87. printDebugLog(CSimpleStringA::Format("get [UpLog].TerminalList from CenterSetting.ini failed"));
  88. }
  89. }
  90. //Dbg("m_isSendLog = %d ",m_isSendLog);
  91. printDebugLog(CSimpleStringA::Format("m_isSendLog = %d ",m_isSendLog));
  92. CSimpleStringA LogTypeList ="";
  93. Error = spConfig->ReadConfigValue("UpLog", "LogType", LogTypeList); //某些日志类型是否上传
  94. if (Error_Succeed == Error)
  95. {
  96. //Dbg("get LogTypeList=%s from CenterSetting.ini", LogTypeList);
  97. printDebugLog(CSimpleStringA::Format("get LogTypeList=%s from CenterSetting.ini", LogTypeList));
  98. CSimpleStringA temp = "";
  99. if(LogTypeList==""){
  100. //空值默认全部类型上传
  101. char buff[10] ={0} ;
  102. sprintf(buff,"%d;%d;%d;%d;",(int)Log_Event,(int)Log_Warning,(int)Log_Error,(int)Log_Debug);
  103. temp +=buff;
  104. }else{
  105. //根据类型值组合上传类型Event|Warn|Debug|Error
  106. CAutoArray<CSimpleStringA> tList = LogTypeList.Split('|');
  107. for(int i=0; i<tList.GetCount(); i++){
  108. if(tList[i]=="Event"){
  109. char buff[4] ={0} ;
  110. sprintf(buff,"%d;",(int)Log_Event);
  111. temp +=buff;
  112. }else if(tList[i]=="Warn"){
  113. char buff[4] ={0} ;
  114. sprintf(buff,"%d;",(int)Log_Warning);
  115. temp +=buff;
  116. }else if(tList[i]=="Debug"){
  117. char buff[4] ={0} ;
  118. sprintf(buff,"%d;",(int)Log_Debug);
  119. temp +=buff;
  120. }else if(tList[i]=="Error"){
  121. char buff[4] ={0} ;
  122. sprintf(buff,"%d;",(int)Log_Error);
  123. temp +=buff;
  124. }
  125. }
  126. }
  127. m_logTypeList = temp;
  128. //Dbg("m_logTypeList =%s", m_logTypeList);
  129. printDebugLog(CSimpleStringA::Format("m_logTypeList =%s", m_logTypeList));
  130. }
  131. else
  132. {
  133. //Dbg("get [UpLog].LogTypeList from CenterSetting.ini failed");
  134. printDebugLog(CSimpleStringA::Format("get [UpLog].LogTypeList from CenterSetting.ini failed"));
  135. }
  136. int SendMode = 0;
  137. Error = spConfig->ReadConfigValueInt("UpLog", "SendMode", SendMode); //发送模式:0 单条发送 1 批量发送(最大32k)
  138. if (Error_Succeed == Error){
  139. m_SendMode = SendMode;
  140. //Dbg("get m_SendMode = %d", SendMode);
  141. printDebugLog(CSimpleStringA::Format("m_SendMode = %d", SendMode));
  142. }else{
  143. //Dbg("get [UpLog].SendMode from CenterSetting.ini failed");
  144. printDebugLog(CSimpleStringA::Format("get [UpLog].SendMode from CenterSetting.ini failed"));
  145. }
  146. if(m_SendMode==0){
  147. m_iEachMaxSend =EACH_SEND_COUNT_SINGLE;
  148. }else{
  149. m_iEachMaxSend =EACH_SEND_COUNT_MULTI;
  150. }
  151. //Dbg("m_iEachConnSum = %d",m_iEachConnSum);
  152. printDebugLog(CSimpleStringA::Format("m_iEachMaxSend = %d",m_iEachMaxSend));
  153. }else{
  154. //Dbg("open CenterSetting.ini failed");
  155. printDebugLog(CSimpleStringA::Format("open CenterSetting.ini failed"));
  156. return Error_Exception;
  157. }
  158. return Error_Succeed;
  159. }
  160. ErrorCodeEnum UpLogFSM::OnExit()
  161. {
  162. return Error_Succeed;
  163. }
  164. void UpLogFSM::s0_on_entry()
  165. {
  166. if(m_isSendLog){
  167. PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));//跳转到S1状态
  168. }else{
  169. Dbg("不需要上收日志数据,等待60s");
  170. ScheduleTimer(1, 60000);
  171. }
  172. }
  173. void UpLogFSM::s0_on_exit()
  174. {
  175. CancelTimer(1);
  176. }
  177. unsigned int UpLogFSM::s0_on_event( FSMEvent* event )
  178. {
  179. if (event->iEvt == EVT_TIMER)
  180. {
  181. if(m_isSendLog){
  182. PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));//跳转到S1状态
  183. }else{
  184. Dbg("不需要上收日志数据,等待60s");
  185. ScheduleTimer(1, 60000);
  186. }
  187. }
  188. return 0;
  189. }
  190. void UpLogFSM::s1_on_entry()
  191. {
  192. if (!m_pConnection) {
  193. m_pConnection = new UpLogConnection(m_pEntity, this);
  194. if(m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK()){
  195. //创建成功
  196. }else{
  197. //创建失败
  198. printDebugLog(CSimpleStringA::Format("连接日志上收分行服务失败"));
  199. m_pConnection->Close();
  200. m_pConnection->DecRefCount();
  201. m_pConnection = NULL;
  202. }
  203. }
  204. if (!m_pConnection)
  205. {
  206. ScheduleTimer(2, 10000); // try 10 seconds later
  207. }
  208. else
  209. {
  210. PostEventLIFO(new FSMEvent(USER_EVT_JMP_SENDLOG));//跳转S2状态
  211. }
  212. }
  213. void UpLogFSM::s1_on_exit()
  214. {
  215. CancelTimer(2);
  216. }
  217. unsigned int UpLogFSM::s1_on_event(FSMEvent* event)
  218. {
  219. if (event->iEvt == EVT_TIMER)
  220. {
  221. m_pConnection = new UpLogConnection(m_pEntity, this);
  222. if(m_pConnection->ConnectFromCentralSetting()&&m_pConnection->IsConnectionOK()){
  223. //创建成功
  224. }else{
  225. printDebugLog(CSimpleStringA::Format("连接日志上收分行服务失败"));
  226. m_pConnection->Close();
  227. m_pConnection->DecRefCount();
  228. m_pConnection = NULL;
  229. }
  230. if (!m_pConnection)
  231. {
  232. ScheduleTimer(2, 10000); // try 10 seconds later
  233. }
  234. else {
  235. PostEventLIFO(new FSMEvent(USER_EVT_JMP_SENDLOG));//跳转S2状态
  236. }
  237. }
  238. return 0;
  239. }
  240. void UpLogFSM::s2_on_entry()
  241. {
  242. if(m_isSendLog){
  243. if(m_logList.empty()){
  244. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_FAIL));//为空时,等待10s再传
  245. }else{
  246. m_iEachSend = 0;
  247. //打印每次发送统计记录;
  248. //Dbg("send log statistic m_iRec=%d , m_iThrow=%d , m_iSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,m_iSend,m_iSucc,m_iFail);
  249. int unSend = m_logList.size();
  250. printDebugLog(CSimpleStringA::Format("send log statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,unSend,m_iSend,m_iSucc,m_iFail));
  251. m_pConnection->SendLog();
  252. m_iEachSend++;
  253. }
  254. }else{
  255. PostEventLIFO(new FSMEvent(USER_EVT_JMP_START));//跳转S0状态
  256. }
  257. }
  258. void UpLogFSM::s2_on_exit()
  259. {
  260. CancelTimer(3);
  261. }
  262. unsigned int UpLogFSM::s2_on_event(FSMEvent* event)
  263. {
  264. if (event->iEvt == EVT_TIMER){
  265. PostEventLIFO(new FSMEvent(USER_EVT_JMP_CONNECT));//跳转S1状态
  266. }else if (event->iEvt == USER_EVT_UPLOG_ANS){
  267. UpLogAnsEvent *ans = (UpLogAnsEvent *)event;
  268. //发送个数控制长连接时间
  269. if (ans->m_reply.ResultCode == UpLogCtlCode::Finish)
  270. {
  271. m_iSucc++;//成功
  272. }else if (ans->m_reply.ResultCode == UpLogCtlCode::Error)
  273. {
  274. m_iFail++;//失败
  275. }
  276. if(m_iEachSend<m_iEachMaxSend){
  277. if(m_logList.empty()){
  278. //Dbg("返回报文结果:%d",ans->m_reply.ResultCode);
  279. Dbg("队列已空");
  280. //printDebugLog(CSimpleStringA::Format("返回报文结果:%d",ans->m_reply.ResultCode));//不能加入队列,否则不断循环
  281. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_FAIL));//为空时,等待10s再传
  282. }else{
  283. m_pConnection->SendLog();
  284. m_iEachSend++;
  285. }
  286. }else{
  287. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_SUCC));//正常退出
  288. }
  289. }else if (event->iEvt == USER_EVT_DISCONNECT_SUCC) {
  290. //正常退出,等待3s时间返回S1
  291. if(m_pConnection!=NULL){
  292. m_pConnection->Close();
  293. m_pConnection->DecRefCount();
  294. m_pConnection = NULL;
  295. }
  296. ScheduleTimer(3, CONNECT_SUCC_WAIT_TIME); // try 3 seconds later
  297. }else if (event->iEvt == USER_EVT_DISCONNECT_FAIL) {
  298. //异常退出,等待10s时间返回S1
  299. if(m_pConnection!=NULL){
  300. m_pConnection->Close();
  301. m_pConnection->DecRefCount();
  302. m_pConnection = NULL;
  303. }
  304. ScheduleTimer(3, CONNECT_FAIL_WAIT_TIME); // try 10 seconds later
  305. }
  306. return 0;
  307. }
  308. void UpLogFSM::addUplog( log_t* logt )
  309. { //加锁,队列满了,把前面的丢弃,新的补充进来,注意释放内存空间
  310. EnterCriticalSection(&cs);
  311. if(m_logList.size()>=UPLOG_MAX_COUNT){
  312. //Dbg("queue is full,delete log,EntityName=%s,Msg=%s,SN=%d",dLog->EntityName,dLog->Msg,dLog->SN);
  313. log_t* dLog = (log_t*)(*m_logList.begin());//集合首个元素
  314. m_logList.erase(m_logList.begin());//集合删除首元素
  315. delLog(dLog);//释放内存
  316. m_iThrow++;
  317. }
  318. m_logList.push_back(logt);//加入队列
  319. LeaveCriticalSection(&cs);
  320. }
  321. log_t* UpLogFSM::removeUplog()
  322. {//加锁,取出先进的队列日志,注意释放内存空间
  323. log_t* dLog = NULL;
  324. EnterCriticalSection(&cs);
  325. if(m_logList.empty()){
  326. }else{
  327. dLog = (log_t*)(*m_logList.begin());//集合首个元素
  328. m_logList.erase(m_logList.begin());//集合删除首元素
  329. }
  330. LeaveCriticalSection(&cs);
  331. return dLog;//返回
  332. }
  333. bool UpLogFSM::isType(const LogTypeEnum eLogTyp)
  334. {
  335. char buff[4] ={0} ;
  336. sprintf(buff,"%d",(int)eLogTyp);
  337. if(m_logTypeList.IndexOf(buff)==-1){
  338. return false;
  339. }else{
  340. return true;
  341. }
  342. }
  343. void UpLogFSM::delLog( log_t* logt )
  344. {
  345. if(logt==NULL){
  346. return;
  347. }
  348. if(logt->EntityName!=NULL){
  349. free(logt->EntityName);
  350. logt->EntityName=NULL;
  351. }
  352. if(logt->Msg!=NULL){
  353. free(logt->Msg);
  354. logt->Msg=NULL;
  355. }
  356. if(logt->TerminalNo!=NULL){
  357. free(logt->TerminalNo);
  358. logt->TerminalNo=NULL;
  359. }
  360. if(logt->LogTime!=NULL){
  361. free(logt->LogTime);
  362. logt->LogTime=NULL;
  363. }
  364. if(logt->Item!=NULL){
  365. free(logt->Item);
  366. logt->Item=NULL;
  367. }
  368. if(logt->SysCode!=NULL){
  369. free(logt->SysCode);
  370. logt->SysCode=NULL;
  371. }
  372. if(logt->UserCode!=NULL){
  373. free(logt->UserCode);
  374. logt->UserCode=NULL;
  375. }
  376. delete logt;
  377. logt=NULL;
  378. return;
  379. }
  380. char* UpLogFSM::mallocStr( const char* s)
  381. {
  382. char* temp =NULL;
  383. if(s && (temp=(char*)malloc(strlen(s)+1))){
  384. memset(temp,0,strlen(s)+1);
  385. strcpy(temp,s);
  386. }
  387. return temp;
  388. }
  389. int UpLogFSM::getEntityRow( const char* entityName )
  390. {
  391. if(entityName==NULL){
  392. return 0;
  393. }
  394. CSimpleStringA findstr = entityName;
  395. map<CSimpleString,int>::iterator iter;
  396. iter = m_entityRowSet.find(findstr);
  397. if(iter!=m_entityRowSet.end()){
  398. (iter->second)++;
  399. return iter->second;
  400. }else{
  401. m_entityRowSet.insert(make_pair(findstr,1));
  402. return 1;
  403. }
  404. }
  405. bool UpLogFSM::getJsonStr( UpLogReq1* req )
  406. {
  407. //单条获取
  408. //Dbg("队列消息数目=%d",m_logList.size());
  409. //printDebugLog(CSimpleStringA::Format("队列消息数目=%d",m_logList.size()));
  410. log_t* logt = removeUplog();//注意释放内存
  411. if(logt==NULL){
  412. return false;
  413. }else{
  414. //单条记录转换为json报文
  415. Json::Value root;
  416. Json::Value arraylist;//消息列表
  417. Json::Value MsgObject;//消息对象
  418. Json::FastWriter fw;//写入对象
  419. root["interface"]="singleLog";
  420. MsgObject["TerminalNo"]= logt->TerminalNo;
  421. MsgObject["EntityName"]= logt->EntityName;
  422. MsgObject["Item"]= logt->Item;
  423. MsgObject["Time"]= logt->LogTime;
  424. MsgObject["TimeSn"]= logt->SN;
  425. MsgObject["SysCode"]= logt->SysCode;
  426. MsgObject["UserCode"]= logt->UserCode;
  427. MsgObject["LogType"]= logt->LogType;
  428. MsgObject["Level"]= logt->Level;
  429. MsgObject["msg"]= logt->Msg;
  430. int i=0;
  431. arraylist[i]= MsgObject;
  432. root["msgList"] = arraylist;
  433. string jsonStr = fw.write(root);
  434. strncpy(req->TerminalNo,logt->TerminalNo,sizeof(req->TerminalNo)-1);//拷贝终端号
  435. delLog(logt);//释放内存
  436. //Dbg("转换的json报文,len=%d,内容=%s",jsonStr.length(),jsonStr.c_str());
  437. if(!jsonStr.empty()){
  438. req->logLen= jsonStr.length();
  439. strncpy(req->logdata,jsonStr.c_str(),sizeof(req->logdata)-1);//最大不超过2k
  440. return true;
  441. }else{
  442. return false;
  443. }
  444. }
  445. }
  446. bool UpLogFSM::getJsonStr( UpLogReq2* req )
  447. {
  448. //动态根据接口长度组装json报文
  449. int i = 0;
  450. Json::Value root;
  451. Json::Value arraylist;//消息列表
  452. Json::FastWriter fw;//写入对象
  453. root["interface"]="multiLog";
  454. string jsonStr="" ;
  455. while(true){
  456. log_t* logt = removeUplog();//注意释放内存
  457. if(logt==NULL){
  458. break;
  459. }else{
  460. Json::Value MsgObject;//消息对象
  461. MsgObject["TerminalNo"]= logt->TerminalNo;
  462. MsgObject["EntityName"]= logt->EntityName;
  463. MsgObject["Item"]= logt->Item;
  464. MsgObject["Time"]= logt->LogTime;
  465. MsgObject["TimeSn"]= logt->SN;
  466. MsgObject["SysCode"]= logt->SysCode;
  467. MsgObject["UserCode"]= logt->UserCode;
  468. MsgObject["LogType"]= logt->LogType;
  469. MsgObject["Level"]= logt->Level;
  470. MsgObject["msg"]= logt->Msg;
  471. arraylist[i]=MsgObject;
  472. root["msgList"] = arraylist;
  473. jsonStr = fw.write(root);//动态增长
  474. //Dbg("jsonStr lenth is %d",jsonStr.length());
  475. strncpy(req->TerminalNo,logt->TerminalNo,sizeof(req->TerminalNo)-1);//拷贝终端号
  476. delLog(logt);//释放内存
  477. //Dbg("转换的json报文,len=%d,内容=%s",jsonStr.length(),jsonStr.c_str());
  478. if(jsonStr.length()>14*1024){
  479. break;
  480. }else{
  481. i++;//加一
  482. }
  483. }
  484. }
  485. Dbg("一次发送=%d",i);
  486. //printDebugLog(CSimpleStringA::Format("一次发送条数=%d",i));//不能加入发送队列,否则不断循环
  487. if(!jsonStr.empty()){
  488. //Dbg("转换的json报文:%s",jsonStr.c_str());
  489. req->logLen = jsonStr.length();
  490. strncpy(req->logdata,jsonStr.c_str(),sizeof(req->logdata)-1);//最大不超过16k
  491. return true;
  492. }else{
  493. return false;
  494. }
  495. }
  496. //使用此方法注意不要陷入死循环加入日志到队列中
  497. void UpLogFSM::printDebugLog( const char* debugStr )
  498. {
  499. Dbg("uplog:%s",debugStr);
  500. //主动添加到消息队列中
  501. string entityName = this->m_pEntity->GetEntityName();
  502. log_t* logt = new log_t();
  503. memset(logt,0,sizeof(log_t));
  504. bool succ = true;
  505. CSystemStaticInfo systemStaticInfo;
  506. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  507. ErrorCodeEnum Error = spFunction->GetSystemStaticInfo(systemStaticInfo);
  508. char* cTerminalNo=NULL;
  509. if(Error_Succeed == Error){
  510. cTerminalNo = mallocStr(systemStaticInfo.strTerminalID.GetData());
  511. }else{
  512. string temp = "0000000000";
  513. cTerminalNo = mallocStr(temp.c_str());
  514. }
  515. if(cTerminalNo!=NULL){
  516. logt->TerminalNo = cTerminalNo;//终端号,记得释放
  517. }else{
  518. succ=false;
  519. }
  520. char* cEntityName = mallocStr(entityName.c_str());
  521. if(cEntityName!=NULL){
  522. logt->EntityName = cEntityName;//实体名,记得释放
  523. }else{
  524. succ=false;
  525. }
  526. char* clogTime = (char*)malloc(30);
  527. if(clogTime!=NULL){
  528. memset(clogTime,0,30);//2020-20-20 12:12:12.100 日志时间,记得释放
  529. SYSTEMTIME st ;
  530. GetLocalTime(&st);
  531. sprintf(clogTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  532. logt->LogTime =clogTime;
  533. }else{
  534. succ=false;
  535. }
  536. char* cMsg = mallocStr(debugStr);
  537. if(cMsg!=NULL){
  538. logt->Msg = cMsg;//日志消息,记得释放
  539. }else{
  540. succ=false;
  541. }
  542. char* cEntityInstanceID = (char*)malloc(20);
  543. if (cEntityInstanceID!=NULL)
  544. {
  545. memset(cEntityInstanceID,0,20);
  546. CEntityRunInfo eri = {0};
  547. ErrorCodeEnum ec = GetEntityBase()->GetFunction()->GetSelfEntityRunInfo(eri);
  548. if(ec == Error_Succeed)
  549. {
  550. ultoa(eri.dwEntityInstanceID,cEntityInstanceID,10);//实体ID,记得释放
  551. logt->Item = cEntityInstanceID;
  552. }else{
  553. logt->Item = cEntityInstanceID;//实体ID,记得释放
  554. }
  555. }else{
  556. succ=false;
  557. }
  558. char* cSysError = (char*)malloc(20);
  559. if (cSysError!=NULL)
  560. {
  561. memset(cSysError,0,20);
  562. ultoa(0xffff,cSysError,16);//系统errorcode,记得释放
  563. logt->SysCode=cSysError;
  564. }else{
  565. succ=false;
  566. }
  567. char* cUserCode = (char*)malloc(20);
  568. if (cUserCode!=NULL)
  569. {
  570. memset(cUserCode,0,20);
  571. ultoa(0xffff,cUserCode,16);//用户定义Usercode,记得释放
  572. logt->UserCode = cUserCode;
  573. }else{
  574. succ=false;
  575. }
  576. logt->LogType = (int)LogTypeEnum::Log_Debug;
  577. logt->Level = (int)SeverityLevelEnum::Severity_Low;
  578. int iRow = getEntityRow(entityName.c_str());
  579. logt->SN =iRow;
  580. addRecSum();
  581. if(!succ){
  582. delLog(logt);//删除元素内存
  583. return;
  584. }else{
  585. addUplog(logt);//加入日志到列表
  586. }
  587. }
  588. void UpLogFSM::addRecSum()
  589. {
  590. EnterCriticalSection(&csRec);
  591. m_iRec++;
  592. LeaveCriticalSection(&csRec);
  593. }