ClassLogFile.cpp 8.8 KB

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