ClassLogFile.cpp 9.0 KB

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