ClassLogFile.cpp 5.7 KB

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