📄 fileopr.cpp
字号:
ENDMSG); return 0;}//==========================================================================//=======================写上端SMGP_Report接收日志文件======================//==========================================================================int FileOpr::WrSMGP_ReportLog(Smgp_Deliver_Log *pReportLog, char* strBuf){ char MsgId[21]; char Report_MsgId[21]; SMGP_Report *Report = (SMGP_Report *)(&pReportLog->Deliver->Deliver_Msg.Report); CFunc::HexToStr(pReportLog->Deliver->sMsgId,10,MsgId); CFunc::HexToStr(Report->MsgId,10,Report_MsgId); //sprintf(strBuf,"%s%-15s%-11d%-11d%-11d[%d:%d:%d:%d] %-2d%-4d%-15s%-22s%-22s%-4d[%d:%d:%d:%d] %-15s%-15s%-8s%-4s%s", sprintf(strBuf,"%s%-15s%-11d%-11d%-11d%-21s%-2d%-4d%-15s%-22s%-22s%-4did: %-21s%s %s", BGNMSG, \ pReportLog->RecvTime, \ ntohl(pReportLog->Deliver->msgHead.nPacketLength), \ ntohl(pReportLog->Deliver->msgHead.nRequestId), \ ntohl(pReportLog->Deliver->msgHead.nSequenceId), \ MsgId, \ pReportLog->Deliver->nIsReport,\ pReportLog->Deliver->nMsgFormat, \ pReportLog->Deliver->sRecvTime, \ pReportLog->Deliver->sSrcTermId, \ pReportLog->Deliver->sDestTermId, \ pReportLog->Deliver->nMsgLength, \ Report_MsgId, \ /*Report->sSubmitDate, \ Report->sDoneDate, \ Report->sStatus, \ Report->sErr, \ */ (char *)Report +3 +10, \ ENDMSG); return 0;}//==========================================================================//===========================下端SMGP_Submit日志============================//==========================================================================/*int FileOpr::WrSMGP_SubmitLog(Smgp_Submit_Log *pSubmitLog, char* strBuf){ char FeeType[3]; char FeeCode[7]; char FixedFee[7]; char MsgId[21]; memcpy(FeeType,pSubmitLog->Submit->sFeeCode,2); FeeType[2]='\0'; memcpy(FeeCode,pSubmitLog->Submit->sFeeCode,6); FeeCode[6]='\0'; memcpy(FixedFee,pSubmitLog->Submit->sFixedFee,6); FixedFee[6]='\0'; CFunc::HexToStr(pSubmitLog->MsgId,10,MsgId); sprintf(strBuf,"%s%-15s%-2d%-16s%-11d%-21s%-11d%-11d%-11d%-2d%-2d%-2d%-11s%-3s%-7s%-7s%-4d%-18s%-18s%-22s%-22s%-4d%-22s%-4d%-253s%s", BGNMSG, \ pSubmitLog->SendTime, \ pSubmitLog->iResent, \ pSubmitLog->SrcNode, \ pSubmitLog->Sequence_Id, \ MsgId, \ ntohl(pSubmitLog->Submit->msgHead.nPacketLength), \ ntohl(pSubmitLog->Submit->msgHead.nRequestId), \ ntohl(pSubmitLog->Submit->msgHead.nSequenceId), \ pSubmitLog->Submit->nMsgType,\ pSubmitLog->Submit->nNeedReport, \ pSubmitLog->Submit->nPriority, \ pSubmitLog->Submit->sServerId, \ FeeType, \ FeeCode, \ FixedFee, \ pSubmitLog->Submit->nMsgFormat, \ pSubmitLog->Submit->sValidTime, \ pSubmitLog->Submit->sAtTime, \ pSubmitLog->Submit->sReplyPath, \ pSubmitLog->Submit->sChargeTermId, \ pSubmitLog->Submit->nDestTermIdCount, \ pSubmitLog->Submit->sDestTermId, \ pSubmitLog->Submit->nMsgLength, \ pSubmitLog->Submit->sMsgContent, \ ENDMSG); return 0;}*/////////////////////////////////////////////////////////////////////////////////Name:RWRead//Function:Read string and return the value of the string from the file we opened with rwlock mode//Parameters:char* szBuf = the string we want to search// int* pInt = the pointer of the value we want to get//Return: 0 = success// -1 = search file failed//Author:Johnchen gzcj@21cn.com//Company:GDCN//Date:2003.04.25/////////////////////////////////////////////////////////////////////////////int FileOpr::RWRead(char *szBuf, int *pInt){ unsigned int i,j,k; int flag=0; char *szSub; char szRst[512]={0}; char szR[512]={0}; char szS[512]={0}; char chSrh = '='; char chItp = '#'; char chItp1 = ';'; //printf("\nlen=%d\n",strlen(szBuf)); //strcpy((char *)szR,szBuf); //strcat(szR," "); for(k=0;k<strlen(szBuf);k++) szR[k]=szS[k]=szBuf[k]; szR[k]=' '; szS[k]='='; szR[k+1]=szS[k+1]='\0'; /* printf("\nszR=%d\n",strlen(szR)); //strcpy((char *)szS,szBuf); //strcat(szS,"="); printf("\nszS=%d\n",strlen(szS)); printf("szS=%s",szS); printf("szR=%s",szR); */ //if this is the first time we open the file,use readline to get the string line buffer if (m_bRead) ReadLine(); m_bRead = false; //seek the string line buffer and get the value of the string we want for (i=0;i<m_nCount;i++){ if (strstr(m_szReadBuf[i],szBuf)>0&&(strstr(m_szReadBuf[i],szR)>0||strstr(m_szReadBuf[i],szS)>0)){ szSub = strchr(m_szReadBuf[i],chSrh); if (chItp != m_szReadBuf[i][0]&&chItp1 != m_szReadBuf[i][0]){ if(szSub){ flag=1; for (j=1;j < strlen(szSub);j++){ szRst[j-1] = szSub[j]; szRst[j]='\0'; *pInt = atoi(szRst); }//end for }//end if }//end if }//end if }//end for if(m_bRead) m_bRead=false; //printf("rst[%d]=%s\n",strlen(szRst),szRst); if(flag==1) { return (0); } else { printf("\n[%s] Not Found !\n",szBuf); return (1); }}////////////////////////////////////////////////////////////////////////////////Name:RWRead//Function:Read string and return the value of the string from the file we opened with rwlock mode//Parameters:char* szBuf = the string we want to search// char* szRst = the value we want to get//Return: 0 = success// -1 = search file failed//Author:Johnchen gzcj@21cn.com//Company:GDCN//Date:2003.04.25/////////////////////////////////////////////////////////////////////////////int FileOpr::RWRead(char *szBuf,char *szRst){ unsigned int i,j; int flag=0; char *szSub; char chSrh = '='; //div string char chItp = '#'; char chItp1 = ';'; //if this is the first time we open the file,use readline to get the string line buffer if(m_bRead) ReadLine(); //printf("\nchar char char\n"); //seek the string line buffer and get the value of the string we want for (i=0;i<m_nCount;i++){ //printf("\nstep1--i=%d\n",i); if (strstr(m_szReadBuf[i],szBuf)>0){ //printf("\nstep2--i=%d\n",i); if (chItp != m_szReadBuf[i][0]&&chItp1 != m_szReadBuf[i][0]){ //printf("\nstep3--i=%d\n",i); szSub = strchr(m_szReadBuf[i],chSrh); if(szSub){ //printf("\nstep4--i=%d\n",i); flag=1; for (j=1;j<strlen(szSub);j++){ //printf("\nstemp5--j=%d\n",j); szRst[j-1] = szSub[j]; szRst[j]='\0'; }//end for }//end if }//end if }//end if }//end for if(m_bRead) m_bRead=false; //printf("rst[%d]=%s\n",strlen(szRst),szRst); if(flag==1) { return (0); } else { printf("\n[%s] Not Found !\n",szBuf); return (1); }}//==========================================================================//=========================写日志文件的外部接口方法=========================//==========================================================================/*在sys_struct.h中定义:#define SYSTEMLOG 0 //系统日志(字符串)#define CMPP_DELIVER_LOG 1 //上端CMPP_Deliver接收日志文件#define CMPP_REPORT_LOG 2 //上端CMPP_Report接收日志文件#define CMPP_SUBMIT_LOG 3 //下端CMPP_Submit接收日志文件#define SMGP_DELIVER_LOG 11 //上端SMGP_Deliver接收日志文件#define SMGP_REPORT_LOG 12 //上端SMGP_Report接收日志文件 */int FileOpr::WriteLog(void *pParam, const int iType){ char strBuf[2048]={0}; sem_wait(&SemMutex); NewDateFile(); switch (iType){ case SYSTEMLOG: getTime(strBuf); strcat(strBuf," "); strcat(strBuf,(const char*)pParam); strBuf[strlen(strBuf)]='\0'; cout << strBuf << endl; break; case CMPP_DELIVER_LOG: WrCMPP_DeliverLog((Cmpp_Deliver_Log *)pParam, strBuf); break; case CMPP_REPORT_LOG: WrCMPP_ReportLog((Cmpp_Deliver_Log *)pParam, strBuf); break; case CMPP_SUBMIT_LOG: WrCMPP_SubmitLog((Cmpp_Submit_Log *)pParam, strBuf); break; case SMGP_DELIVER_LOG: WrSMGP_DeliverLog((Smgp_Deliver_Log *)pParam, strBuf); break; case SMGP_REPORT_LOG: WrSMGP_ReportLog((Smgp_Deliver_Log *)pParam, strBuf); break; /*case 4: WrCMPP_SubmitFailLog((Cmpp_Submit_Log *)pParam, strBuf); break; case 31: SMGP_Submit_Imp((SMGP_Submit *)pParam,strBuf); break;*/ default: break; }//switch outFile << strBuf << endl; sem_post(&SemMutex); return 0;}//==========================================================================//============================打开配置/日志文件=============================//==========================================================================int FileOpr::RWOpen(char* FileName,char *szMode){ char DateBuf[20]; char fileBuf[200]; if (FileName==NULL || strlen(FileName)==0) //非法文件名 { cout << "[err]=file name can not be null!" << endl; return 1; } if (strcmp(szMode,"r")==0) //只读模式打开文件 { strcpy(RWFileName,FileName); inFile.open(RWFileName, ios::in ); if (!inFile) { cout << "[err]=Cannot open input file " << FileName << endl; return 1; } } else if (strcmp(szMode,"ow")==0) //"覆盖写"的模式打开文件 { strcpy(RWFileName,FileName); outFile.open(RWFileName, ios::out ); if (!outFile) { cout << "[err]=Cannot open output file " << FileName << endl; return 1; } } else if (strcmp(szMode,"w")==0) //"追加写"模式打开文件,且自动在文件名后面加日期 { strcpy(FileForeName,FileName); getDate(DateBuf); memset(fileBuf,0,sizeof(fileBuf)); sprintf(fileBuf,"%s%s",FileForeName,DateBuf); outFile.open(fileBuf,ios::out|ios::app); if (!outFile) { cout << "[err]=Cannot open output file " << fileBuf << endl; return 1; } } else return 1; return 0; }//==========================================================================//=================================关闭文件=================================//==========================================================================int FileOpr::RWClose(){ if (outFile.is_open()){ outFile.close(); } if (inFile.is_open()){ inFile.close(); } return 0;}//=========================================================================//关闭旧文件,创建并打开新文件//=========================================================================int FileOpr::CreateFile(char* DateBuf){ char fileBuf[200]; RWClose(); //关闭当前操作的文件 memset(fileBuf,0,sizeof(fileBuf)); sprintf(fileBuf,"%s%s",FileForeName,DateBuf); //printf("file=%s",fileBuf); outFile.open(fileBuf,ios::out|ios::app); //以"追加写"模式打开新文件 if (!outFile) { cout << "[err]=Cannot open output file " << fileBuf << endl; return 1; } cout << "New File opened: " << fileBuf << endl; return 0;}//=========================================================================//从文件中一行行读取全部内容,存入二维数组//=========================================================================int FileOpr::ReadLine(){ m_nCount=0; while(inFile.getline(m_szReadBuf[m_nCount],MAXBUF,'\n')) { //printf("bufline[%d]=%s\n",m_nCount,m_szReadBuf[m_nCount]); m_nCount++; } return 0;}//=========================================================================//获取系统时间,如"20030618172219"(年月日时分秒,加上‘\0’共15个字符)//=========================================================================char* FileOpr::getTime(char* buf){ time_t tt; struct tm *now; time(&tt); now = localtime( &tt); sprintf( buf, "%d%02d%02d%02d%02d%02d", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); return buf;}//=========================================================================//=========================获取系统日期("yymmdd")========================//=========================================================================int FileOpr::getDate(char *szDate){ time_t tt; struct tm *now; time(&tt); now = localtime(&tt); sprintf(szDate, "%02d%02d%02d", \ now->tm_year-100, \ now->tm_mon + 1, \ now->tm_mday); return 0;}//=========================================================================//检查日期,若为新的一天,则创建并打开一个新文件,并返回1//=========================================================================int FileOpr::NewDateFile(){ char buf[20]; getDate(buf); long NowDate = atol(buf); if (NowDate > CurrentDate) //到了新的一天 { CreateFile(buf); //创建一个新文件 CurrentDate = NowDate; return 1; } return 0; //still current day}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -