ClassLogFile.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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", "/sdcard/log/data", "data_%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::WriteToData(std::string _ReturnRawValue, std::string _ReturnValue, std::string _ErrorMessageText, std::string _digital, std::string _analog)
  55. {
  56. time_t rawtime;
  57. struct tm* timeinfo;
  58. char buffer[30];
  59. time(&rawtime);
  60. timeinfo = localtime(&rawtime);
  61. strftime(buffer, 30, datafile.c_str(), timeinfo);
  62. std::string logpath = dataroot + "/" + buffer;
  63. FILE* pFile;
  64. std::string zwtime;
  65. if (!doLogFile){
  66. return;
  67. }
  68. pFile = fopen(logpath.c_str(), "a+");
  69. if (pFile!=NULL) {
  70. time_t rawtime;
  71. struct tm* timeinfo;
  72. char buffer[80];
  73. time(&rawtime);
  74. timeinfo = localtime(&rawtime);
  75. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  76. zwtime = std::string(buffer) + ":";
  77. fputs(zwtime.c_str(), pFile);
  78. fputs(_ReturnRawValue.c_str(), pFile);
  79. fputs("\t", pFile);
  80. fputs(_ReturnValue.c_str(), pFile);
  81. fputs("\t", pFile);
  82. fputs(_ErrorMessageText.c_str(), pFile);
  83. fputs("\t", pFile);
  84. fputs(_digital.c_str(), pFile);
  85. fputs("\t", pFile);
  86. fputs(_analog.c_str(), pFile);
  87. fputs("\t", pFile);
  88. fputs("\n", pFile);
  89. fclose(pFile);
  90. } else {
  91. ESP_LOGI(TAG, "Can't open data file %s", logpath.c_str());
  92. }
  93. }
  94. void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool _time)
  95. {
  96. FILE* pFile;
  97. std::string zwtime;
  98. if (!doLogFile){
  99. return;
  100. }
  101. // pFile = OpenFileAndWait(_fn.c_str(), "a");
  102. pFile = fopen(_fn.c_str(), "a+");
  103. // printf("Logfile opened: %s\n", _fn.c_str());
  104. if (pFile!=NULL) {
  105. if (_time)
  106. {
  107. time_t rawtime;
  108. struct tm* timeinfo;
  109. char buffer[80];
  110. time(&rawtime);
  111. timeinfo = localtime(&rawtime);
  112. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  113. zwtime = std::string(buffer);
  114. info = zwtime + ": " + info;
  115. }
  116. fputs(info.c_str(), pFile);
  117. fputs("\n", pFile);
  118. fclose(pFile);
  119. } else {
  120. ESP_LOGI(TAG, "Can't open log file %s", _fn.c_str());
  121. }
  122. }
  123. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  124. doLogFile = _doLogFile;
  125. };
  126. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  127. retentionInDays = _retentionInDays;
  128. };
  129. void ClassLogFile::WriteToFile(std::string info, bool _time)
  130. {
  131. /*
  132. struct stat path_stat;
  133. if (stat(logroot.c_str(), &path_stat) != 0) {
  134. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  135. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  136. ESP_LOGI(TAG, "Can't create log folder");
  137. }
  138. }
  139. */
  140. time_t rawtime;
  141. struct tm* timeinfo;
  142. char buffer[30];
  143. time(&rawtime);
  144. timeinfo = localtime(&rawtime);
  145. strftime(buffer, 30, logfile.c_str(), timeinfo);
  146. std::string logpath = logroot + "/" + buffer;
  147. WriteToDedicatedFile(logpath, info, _time);
  148. printf((info + "\n").c_str());
  149. }
  150. std::string ClassLogFile::GetCurrentFileName()
  151. {
  152. time_t rawtime;
  153. struct tm* timeinfo;
  154. char buffer[60];
  155. time(&rawtime);
  156. timeinfo = localtime(&rawtime);
  157. strftime(buffer, 60, logfile.c_str(), timeinfo);
  158. std::string logpath = logroot + "/" + buffer;
  159. return logpath;
  160. }
  161. void ClassLogFile::RemoveOld()
  162. {
  163. if (retentionInDays == 0) {
  164. return;
  165. }
  166. time_t rawtime;
  167. struct tm* timeinfo;
  168. char cmpfilename[30];
  169. time(&rawtime);
  170. rawtime = addDays(rawtime, -retentionInDays);
  171. timeinfo = localtime(&rawtime);
  172. ////////////////////// message /////////////////////////////////////////
  173. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  174. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  175. DIR *dir = opendir(logroot.c_str());
  176. if (!dir) {
  177. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  178. return;
  179. }
  180. struct dirent *entry;
  181. int deleted = 0;
  182. int notDeleted = 0;
  183. while ((entry = readdir(dir)) != NULL) {
  184. if (entry->d_type == DT_REG) {
  185. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  186. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) == 0)) {
  187. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  188. std::string filepath = logroot + "/" + entry->d_name;
  189. if (unlink(filepath.c_str()) == 0) {
  190. deleted ++;
  191. } else {
  192. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  193. }
  194. } else {
  195. notDeleted ++;
  196. }
  197. }
  198. }
  199. ESP_LOGI(TAG, "%d logfiles deleted. %d files not deleted (incl. leer.txt).", deleted, notDeleted);
  200. LogFile.WriteToFile("logfiles deleted: " + std::to_string(deleted) + " files not deleted (incl. leer.txt): " + std::to_string(notDeleted));
  201. closedir(dir);
  202. ////////////////////// data /////////////////////////////////////////
  203. strftime(cmpfilename, 30, datafile.c_str(), timeinfo);
  204. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  205. dir = opendir(dataroot.c_str());
  206. if (!dir) {
  207. ESP_LOGI(TAG, "Failed to stat dir : %s", dataroot.c_str());
  208. return;
  209. }
  210. deleted = 0;
  211. notDeleted = 0;
  212. while ((entry = readdir(dir)) != NULL) {
  213. if (entry->d_type == DT_REG) {
  214. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  215. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  216. ESP_LOGI(TAG, "delete data file : %s", entry->d_name);
  217. std::string filepath = logroot + "/" + entry->d_name;
  218. if (unlink(filepath.c_str()) == 0) {
  219. deleted ++;
  220. } else {
  221. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  222. }
  223. } else {
  224. notDeleted ++;
  225. }
  226. }
  227. }
  228. ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  229. closedir(dir);
  230. }
  231. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::string _logdatapath, std::string _datafile)
  232. {
  233. logroot = _logroot;
  234. logfile = _logfile;
  235. datafile = _datafile;
  236. dataroot = _logdatapath;
  237. doLogFile = true;
  238. retentionInDays = 10;
  239. loglevel = 0;
  240. }