ClassLogFile.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "ClassLogFile.h"
  2. #include "time_sntp.h"
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include "Helper.h"
  8. static const char *TAG = "log";
  9. ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt");
  10. void ClassLogFile::WriteHeapInfo(std::string _id)
  11. {
  12. std::string _zw;
  13. _zw = "\t" + _id + "\t" + getESPHeapInfo();
  14. WriteToFile(_zw);
  15. }
  16. std::string ClassLogFile::getESPHeapInfo(){
  17. string espInfoResultStr = "";
  18. char aMsgBuf[80];
  19. multi_heap_info_t aMultiHead_info ;
  20. heap_caps_get_info (&aMultiHead_info,MALLOC_CAP_8BIT);
  21. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  22. size_t aMinFreeHeadSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  23. size_t aMinFreeHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  24. size_t aHeapLargestFreeBlockSize = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
  25. sprintf(aMsgBuf,"Free Heap Size: \t%ld", (long) aFreeHeapSize);
  26. size_t aFreeSPIHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_SPIRAM);
  27. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  28. size_t aMinFreeInternalHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  29. sprintf(aMsgBuf,"\tHeap:\t%ld", (long) aFreeHeapSize);
  30. espInfoResultStr += string(aMsgBuf);
  31. sprintf(aMsgBuf,"\tMin Free:\t%ld", (long) aMinFreeHeapSize);
  32. espInfoResultStr += string(aMsgBuf);
  33. sprintf(aMsgBuf,"\tlarg. Block: \t%ld", (long) aHeapLargestFreeBlockSize);
  34. espInfoResultStr += string(aMsgBuf);
  35. sprintf(aMsgBuf,"\tSPI Heap:\t%ld", (long) aFreeSPIHeapSize);
  36. espInfoResultStr += string(aMsgBuf);
  37. sprintf(aMsgBuf,"\tMin Free Heap Size:\t%ld", (long) aMinFreeHeadSize);
  38. sprintf(aMsgBuf,"\tNOT_SPI Heap:\t%ld", (long) (aFreeHeapSize - aFreeSPIHeapSize));
  39. espInfoResultStr += string(aMsgBuf);
  40. sprintf(aMsgBuf,"\tlargest Block Size: \t%ld", (long) aHeapLargestFreeBlockSize);
  41. sprintf(aMsgBuf,"\tInternal Heap:\t%ld", (long) (aFreeInternalHeapSize));
  42. espInfoResultStr += string(aMsgBuf);
  43. sprintf(aMsgBuf,"\tInternal Min Heap free:\t%ld", (long) (aMinFreeInternalHeapSize));
  44. espInfoResultStr += string(aMsgBuf);
  45. return espInfoResultStr;
  46. }
  47. void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool _time)
  48. {
  49. FILE* pFile;
  50. std::string zwtime;
  51. if (!doLogFile){
  52. return;
  53. }
  54. // pFile = OpenFileAndWait(_fn.c_str(), "a");
  55. pFile = fopen(_fn.c_str(), "a+");
  56. printf("Logfile opened: %s\n", _fn.c_str());
  57. if (pFile!=NULL) {
  58. if (_time)
  59. {
  60. time_t rawtime;
  61. struct tm* timeinfo;
  62. char buffer[80];
  63. time(&rawtime);
  64. timeinfo = localtime(&rawtime);
  65. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  66. zwtime = std::string(buffer);
  67. info = zwtime + ": " + info;
  68. }
  69. fputs(info.c_str(), pFile);
  70. fputs("\n", pFile);
  71. fclose(pFile);
  72. } else {
  73. ESP_LOGI(TAG, "Can't open log file %s", _fn.c_str());
  74. }
  75. }
  76. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  77. doLogFile = _doLogFile;
  78. };
  79. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  80. retentionInDays = _retentionInDays;
  81. };
  82. void ClassLogFile::WriteToFile(std::string info, bool _time)
  83. {
  84. /*
  85. struct stat path_stat;
  86. if (stat(logroot.c_str(), &path_stat) != 0) {
  87. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  88. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  89. ESP_LOGI(TAG, "Can't create log foolder");
  90. }
  91. }
  92. */
  93. time_t rawtime;
  94. struct tm* timeinfo;
  95. char buffer[30];
  96. time(&rawtime);
  97. timeinfo = localtime(&rawtime);
  98. strftime(buffer, 30, logfile.c_str(), timeinfo);
  99. std::string logpath = logroot + "/" + buffer;
  100. WriteToDedicatedFile(logpath, info, _time);
  101. }
  102. std::string ClassLogFile::GetCurrentFileName()
  103. {
  104. time_t rawtime;
  105. struct tm* timeinfo;
  106. char buffer[60];
  107. time(&rawtime);
  108. timeinfo = localtime(&rawtime);
  109. strftime(buffer, 60, logfile.c_str(), timeinfo);
  110. std::string logpath = logroot + "/" + buffer;
  111. return logpath;
  112. }
  113. void ClassLogFile::RemoveOld()
  114. {
  115. if (retentionInDays == 0) {
  116. return;
  117. }
  118. time_t rawtime;
  119. struct tm* timeinfo;
  120. char cmpfilename[30];
  121. time(&rawtime);
  122. rawtime = addDays(rawtime, -retentionInDays);
  123. timeinfo = localtime(&rawtime);
  124. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  125. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  126. DIR *dir = opendir(logroot.c_str());
  127. if (!dir) {
  128. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  129. return;
  130. }
  131. struct dirent *entry;
  132. int deleted = 0;
  133. int notDeleted = 0;
  134. while ((entry = readdir(dir)) != NULL) {
  135. if (entry->d_type == DT_REG) {
  136. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  137. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  138. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  139. std::string filepath = logroot + "/" + entry->d_name;
  140. if (unlink(filepath.c_str()) == 0) {
  141. deleted ++;
  142. } else {
  143. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  144. }
  145. } else {
  146. notDeleted ++;
  147. }
  148. }
  149. }
  150. ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  151. closedir(dir);
  152. }
  153. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile)
  154. {
  155. logroot = _logroot;
  156. logfile = _logfile;
  157. doLogFile = true;
  158. retentionInDays = 10;
  159. }