123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "includes.h"
- #define APP_LOG_MAX_SIZE (3*1024)
- #define APP_LOG "applog.txt"
- void takeNoteOfSerType(void){
- sutApp.serverDef=newPara.serList.defaultuse;
- MSG_INFO(1, "pri server option:%d\n", sutApp.serverDef);
- }
- typedef struct{
- int fd;
- MY_CLOCK myclock;
- LSAPI_FS_Stat_info_t pbuf;
- char info[20+20+10];
- }LOG_PARADEF;
- LOG_PARADEF logPara={0};
- void takeNoteFile(char *reason){
- int offset,i,j,k;
- int extneedsize;
- if(sutApp.serverDef==newPara.serList.defaultuse) return;
- ohpocGetClock(&logPara.myclock);
- snprintf(logPara.info, sizeof(logPara.info),"*[%04d-%02d-%02d,%02d:%02d:%02d,%d,%d,%s]\n",
- logPara.myclock.year,
- logPara.myclock.month,
- logPara.myclock.day,
- logPara.myclock.hour,
- logPara.myclock.min,
- logPara.myclock.sec,
- sutApp.serverDef,newPara.serList.defaultuse,
- reason);
- MSG_WARN(1, logPara.info);
- logPara.fd=LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDWR|LSAPI_FS_O_CREAT,0x0);
- if(logPara.fd<0){
- MSG_ERR(1,"%s open failed",APP_LOG);
- return;
- }
- LSAPI_FS_Fstat(logPara.fd, &logPara.pbuf);
- //检测是否够写,如果不够写,则将第一条删除
- if((logPara.pbuf.st_size+strlen(logPara.info))>APP_LOG_MAX_SIZE){
- extneedsize=strlen(logPara.info)-(APP_LOG_MAX_SIZE-logPara.pbuf.st_size);//至少还需要这么多
- unsigned char *ptr=(unsigned char *)malloc(logPara.pbuf.st_size*sizeof(unsigned char));
- if(ptr==NULL){//如果申请失败,那这一条信息就直接覆盖第一条好了
- MSG_WARN(1, "malloc failed, save to 0");
- offset=0;
- }else{//将文件所有数据先读出来
- LSAPI_FS_Read(logPara.fd, ptr, logPara.pbuf.st_size);
- //然后删除数据直到够存本条信息
- j=0;
- for(i=0;i<logPara.pbuf.st_size;i++){
- if(ptr[i]=='\n'){
- j=i+1;
- if(j>=extneedsize) break;//够数据了
- }
- }
- //整体数据移动j
- for(i=j;i<logPara.pbuf.st_size;i++) ptr[i-j]=ptr[i];
- //将新数据填充在最后
- k=logPara.pbuf.st_size-j;
- for(i=0;i<strlen(logPara.info);i++) ptr[k+i]=logPara.info[i];
- //先关掉本文件
- LSAPI_FS_Close(logPara.fd);
- //再将它打开并截断清0
- logPara.fd=LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDWR|LSAPI_FS_O_CREAT|LSAPI_FS_O_TRUNC,0x0);
- if(logPara.fd<0){//那就扔掉吧
- MSG_ERR(1,"%s open and trunc failed",APP_LOG);
- free(ptr);
- return;
- }
- //回写
- j=k+strlen(logPara.info);
- LSAPI_FS_Write(logPara.fd, ptr, j);
- LSAPI_FS_Close(logPara.fd);
- free(ptr);
- return;
- }
- }else offset=logPara.pbuf.st_size;
- LSAPI_FS_Seek(logPara.fd, offset, LSAPI_FS_SEEK_SET);
- LSAPI_FS_Write(logPara.fd, logPara.info, strlen(logPara.info));
- LSAPI_FS_Close(logPara.fd);
- }
- unsigned char reachFull=0;
- unsigned char* takeNoteRead(int *msglen){
- int len;
- reachFull=0;
- int fd = LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDONLY, 0);
- if(fd<0){
- MSG_WARN(1, "%s open err", APP_LOG);
- goto FAILED;
- }
- LSAPI_FS_Stat_info_t pbuf;
- LSAPI_FS_Fstat(fd, &pbuf);
- if(pbuf.st_size>(APP_LOG_MAX_SIZE-30)) reachFull=1;//基础每一条长度30左右,提示已慢,提示可删除
- unsigned char *ptr=(unsigned char *)malloc(pbuf.st_size*sizeof(unsigned char)+1);
- if(ptr==NULL){
- MSG_WARN(1, "%s malloc err", APP_LOG);
- LSAPI_FS_Close(fd);
- goto FAILED;
- }
- if(pbuf.st_size){
- len=LSAPI_FS_Read(fd, ptr, pbuf.st_size);
- }else len=0;
- LSAPI_FS_Close(fd);
- if(len<0) len=0;
- *msglen=len;
- ptr[len]=0;
- return ptr;
- FAILED:
- *msglen=0;
- return NULL;
- }
- unsigned char *debugmsg=NULL;
- int debugmsglen=0;
- void uiShowDebug(char update){
- if(update){
- guiClearAll(guiGetBackColor());
- guiShowCaption(0,"更改记录",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_16X16);
- debugmsg=takeNoteRead(&debugmsglen);
- if(debugmsglen>0 && debugmsg!=NULL)
- newMessageInit(FONT_MODE_16X16, debugmsg, NULL, NULL);
- uiMenuShowBottomLine();
- if(reachFull==0) uiMenuShowBottomIndacitor(NULL, "返回");
- else uiMenuShowBottomIndacitor("格式化", "返回");
- return;
- }
- }
- void uiShowDebugResponse(void){
- unsigned short key=getKeyValue();
- if(MKEY_VALUE_ESC==key){
- if(debugmsg != NULL) free(debugmsg);
- debugmsg=NULL;
- uISetNextStatus(UIS_STANDBY);
- }
- else if(MKEY_VALUE_DOWN==key){
- if(debugmsglen>0 && debugmsg!=NULL)
- newMessageShow(SHOW_DOWN);
- }else if(MKEY_VALUE_MENU==key){
- if(reachFull!=0){
- char buf[50];
- snprintf(buf, sizeof(buf), "AT+FSDELFILE=\"applog.txt\"\r\n",APP_LOG);
- msgAtSend(buf);
- uISetNextStatus(UIS_STANDBY);
- }
- }
- }
|