debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "includes.h"
  2. #define APP_LOG_MAX_SIZE (3*1024)
  3. #define APP_LOG "applog.txt"
  4. void takeNoteOfSerType(void){
  5. sutApp.serverDef=newPara.serList.defaultuse;
  6. MSG_INFO(1, "pri server option:%d\n", sutApp.serverDef);
  7. }
  8. typedef struct{
  9. int fd;
  10. MY_CLOCK myclock;
  11. LSAPI_FS_Stat_info_t pbuf;
  12. char info[20+20+10];
  13. }LOG_PARADEF;
  14. LOG_PARADEF logPara={0};
  15. void takeNoteFile(char *reason){
  16. int offset,i,j,k;
  17. int extneedsize;
  18. if(sutApp.serverDef==newPara.serList.defaultuse) return;
  19. ohpocGetClock(&logPara.myclock);
  20. snprintf(logPara.info, sizeof(logPara.info),"*[%04d-%02d-%02d,%02d:%02d:%02d,%d,%d,%s]\n",
  21. logPara.myclock.year,
  22. logPara.myclock.month,
  23. logPara.myclock.day,
  24. logPara.myclock.hour,
  25. logPara.myclock.min,
  26. logPara.myclock.sec,
  27. sutApp.serverDef,newPara.serList.defaultuse,
  28. reason);
  29. MSG_WARN(1, logPara.info);
  30. logPara.fd=LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDWR|LSAPI_FS_O_CREAT,0x0);
  31. if(logPara.fd<0){
  32. MSG_ERR(1,"%s open failed",APP_LOG);
  33. return;
  34. }
  35. LSAPI_FS_Fstat(logPara.fd, &logPara.pbuf);
  36. //检测是否够写,如果不够写,则将第一条删除
  37. if((logPara.pbuf.st_size+strlen(logPara.info))>APP_LOG_MAX_SIZE){
  38. extneedsize=strlen(logPara.info)-(APP_LOG_MAX_SIZE-logPara.pbuf.st_size);//至少还需要这么多
  39. unsigned char *ptr=(unsigned char *)malloc(logPara.pbuf.st_size*sizeof(unsigned char));
  40. if(ptr==NULL){//如果申请失败,那这一条信息就直接覆盖第一条好了
  41. MSG_WARN(1, "malloc failed, save to 0");
  42. offset=0;
  43. }else{//将文件所有数据先读出来
  44. LSAPI_FS_Read(logPara.fd, ptr, logPara.pbuf.st_size);
  45. //然后删除数据直到够存本条信息
  46. j=0;
  47. for(i=0;i<logPara.pbuf.st_size;i++){
  48. if(ptr[i]=='\n'){
  49. j=i+1;
  50. if(j>=extneedsize) break;//够数据了
  51. }
  52. }
  53. //整体数据移动j
  54. for(i=j;i<logPara.pbuf.st_size;i++) ptr[i-j]=ptr[i];
  55. //将新数据填充在最后
  56. k=logPara.pbuf.st_size-j;
  57. for(i=0;i<strlen(logPara.info);i++) ptr[k+i]=logPara.info[i];
  58. //先关掉本文件
  59. LSAPI_FS_Close(logPara.fd);
  60. //再将它打开并截断清0
  61. logPara.fd=LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDWR|LSAPI_FS_O_CREAT|LSAPI_FS_O_TRUNC,0x0);
  62. if(logPara.fd<0){//那就扔掉吧
  63. MSG_ERR(1,"%s open and trunc failed",APP_LOG);
  64. free(ptr);
  65. return;
  66. }
  67. //回写
  68. j=k+strlen(logPara.info);
  69. LSAPI_FS_Write(logPara.fd, ptr, j);
  70. LSAPI_FS_Close(logPara.fd);
  71. free(ptr);
  72. return;
  73. }
  74. }else offset=logPara.pbuf.st_size;
  75. LSAPI_FS_Seek(logPara.fd, offset, LSAPI_FS_SEEK_SET);
  76. LSAPI_FS_Write(logPara.fd, logPara.info, strlen(logPara.info));
  77. LSAPI_FS_Close(logPara.fd);
  78. }
  79. unsigned char reachFull=0;
  80. unsigned char* takeNoteRead(int *msglen){
  81. int len;
  82. reachFull=0;
  83. int fd = LSAPI_FS_Open(APP_LOG, LSAPI_FS_O_RDONLY, 0);
  84. if(fd<0){
  85. MSG_WARN(1, "%s open err", APP_LOG);
  86. goto FAILED;
  87. }
  88. LSAPI_FS_Stat_info_t pbuf;
  89. LSAPI_FS_Fstat(fd, &pbuf);
  90. if(pbuf.st_size>(APP_LOG_MAX_SIZE-30)) reachFull=1;//基础每一条长度30左右,提示已慢,提示可删除
  91. unsigned char *ptr=(unsigned char *)malloc(pbuf.st_size*sizeof(unsigned char)+1);
  92. if(ptr==NULL){
  93. MSG_WARN(1, "%s malloc err", APP_LOG);
  94. LSAPI_FS_Close(fd);
  95. goto FAILED;
  96. }
  97. if(pbuf.st_size){
  98. len=LSAPI_FS_Read(fd, ptr, pbuf.st_size);
  99. }else len=0;
  100. LSAPI_FS_Close(fd);
  101. if(len<0) len=0;
  102. *msglen=len;
  103. ptr[len]=0;
  104. return ptr;
  105. FAILED:
  106. *msglen=0;
  107. return NULL;
  108. }
  109. unsigned char *debugmsg=NULL;
  110. int debugmsglen=0;
  111. void uiShowDebug(char update){
  112. if(update){
  113. guiClearAll(guiGetBackColor());
  114. guiShowCaption(0,"更改记录",UI_STATUS_ITEM_Y+1,guiGetBackColor(),guiGetForeColor(),FONT_MODE_16X16);
  115. debugmsg=takeNoteRead(&debugmsglen);
  116. if(debugmsglen>0 && debugmsg!=NULL)
  117. newMessageInit(FONT_MODE_16X16, debugmsg, NULL, NULL);
  118. uiMenuShowBottomLine();
  119. if(reachFull==0) uiMenuShowBottomIndacitor(NULL, "返回");
  120. else uiMenuShowBottomIndacitor("格式化", "返回");
  121. return;
  122. }
  123. }
  124. void uiShowDebugResponse(void){
  125. unsigned short key=getKeyValue();
  126. if(MKEY_VALUE_ESC==key){
  127. if(debugmsg != NULL) free(debugmsg);
  128. debugmsg=NULL;
  129. uISetNextStatus(UIS_STANDBY);
  130. }
  131. else if(MKEY_VALUE_DOWN==key){
  132. if(debugmsglen>0 && debugmsg!=NULL)
  133. newMessageShow(SHOW_DOWN);
  134. }else if(MKEY_VALUE_MENU==key){
  135. if(reachFull!=0){
  136. char buf[50];
  137. snprintf(buf, sizeof(buf), "AT+FSDELFILE=\"applog.txt\"\r\n",APP_LOG);
  138. msgAtSend(buf);
  139. uISetNextStatus(UIS_STANDBY);
  140. }
  141. }
  142. }