ClassLogFile.cpp 5.8 KB

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