ClassLogFile.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 _timestamp, std::string _name, std::string _ReturnRawValue, std::string _ReturnValue, std::string _ReturnPreValue, std::string _ReturnRateValue, std::string _ReturnChangeAbsolute, 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. fputs(_timestamp.c_str(), pFile);
  73. fputs("\t", pFile);
  74. fputs(_name.c_str(), pFile);
  75. fputs("\t", pFile);
  76. fputs(_ReturnRawValue.c_str(), pFile);
  77. fputs("\t", pFile);
  78. fputs(_ReturnValue.c_str(), pFile);
  79. fputs("\t", pFile);
  80. fputs(_ReturnPreValue.c_str(), pFile);
  81. fputs("\t", pFile);
  82. fputs(_ReturnRateValue.c_str(), pFile);
  83. fputs("\t", pFile);
  84. fputs(_ReturnChangeAbsolute.c_str(), pFile);
  85. fputs("\t", pFile);
  86. fputs(_ErrorMessageText.c_str(), pFile);
  87. fputs(_digital.c_str(), pFile);
  88. fputs(_analog.c_str(), pFile);
  89. fputs("\n", pFile);
  90. fclose(pFile);
  91. } else {
  92. ESP_LOGI(TAG, "Can't open data file %s", logpath.c_str());
  93. }
  94. }
  95. void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool _time)
  96. {
  97. FILE* pFile;
  98. std::string zwtime;
  99. if (!doLogFile){
  100. return;
  101. }
  102. // pFile = OpenFileAndWait(_fn.c_str(), "a");
  103. pFile = fopen(_fn.c_str(), "a+");
  104. // ESP_LOGD(TAG, "Logfile opened: %s", _fn.c_str());
  105. if (pFile!=NULL) {
  106. if (_time)
  107. {
  108. time_t rawtime;
  109. struct tm* timeinfo;
  110. char buffer[80];
  111. time(&rawtime);
  112. timeinfo = localtime(&rawtime);
  113. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  114. zwtime = std::string(buffer);
  115. info = zwtime + ": " + info;
  116. }
  117. fputs(info.c_str(), pFile);
  118. fputs("\n", pFile);
  119. fclose(pFile);
  120. } else {
  121. ESP_LOGI(TAG, "Can't open log file %s", _fn.c_str());
  122. }
  123. }
  124. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  125. doLogFile = _doLogFile;
  126. };
  127. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  128. retentionInDays = _retentionInDays;
  129. };
  130. void ClassLogFile::WriteToFile(std::string info, bool _time)
  131. {
  132. /*
  133. struct stat path_stat;
  134. if (stat(logroot.c_str(), &path_stat) != 0) {
  135. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  136. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  137. ESP_LOGI(TAG, "Can't create log folder");
  138. }
  139. }
  140. */
  141. time_t rawtime;
  142. struct tm* timeinfo;
  143. char buffer[30];
  144. time(&rawtime);
  145. timeinfo = localtime(&rawtime);
  146. strftime(buffer, 30, logfile.c_str(), timeinfo);
  147. std::string logpath = logroot + "/" + buffer;
  148. WriteToDedicatedFile(logpath, info, _time);
  149. ESP_LOGD(TAG, "%s", info.c_str());
  150. }
  151. std::string ClassLogFile::GetCurrentFileName()
  152. {
  153. time_t rawtime;
  154. struct tm* timeinfo;
  155. char buffer[60];
  156. time(&rawtime);
  157. timeinfo = localtime(&rawtime);
  158. strftime(buffer, 60, logfile.c_str(), timeinfo);
  159. std::string logpath = logroot + "/" + buffer;
  160. return logpath;
  161. }
  162. void ClassLogFile::RemoveOld()
  163. {
  164. if (retentionInDays == 0) {
  165. return;
  166. }
  167. time_t rawtime;
  168. struct tm* timeinfo;
  169. char cmpfilename[30];
  170. time(&rawtime);
  171. rawtime = addDays(rawtime, -retentionInDays);
  172. timeinfo = localtime(&rawtime);
  173. ////////////////////// message /////////////////////////////////////////
  174. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  175. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  176. DIR *dir = opendir(logroot.c_str());
  177. if (!dir) {
  178. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  179. return;
  180. }
  181. struct dirent *entry;
  182. int deleted = 0;
  183. int notDeleted = 0;
  184. while ((entry = readdir(dir)) != NULL) {
  185. if (entry->d_type == DT_REG) {
  186. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  187. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) == 0)) {
  188. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  189. std::string filepath = logroot + "/" + entry->d_name;
  190. if (unlink(filepath.c_str()) == 0) {
  191. deleted ++;
  192. } else {
  193. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  194. }
  195. } else {
  196. notDeleted ++;
  197. }
  198. }
  199. }
  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. void ClassLogFile::CreateLogDirectories()
  232. {
  233. MakeDir("/sdcard/log");
  234. MakeDir("/sdcard/log/data");
  235. MakeDir("/sdcard/log/analog");
  236. MakeDir("/sdcard/log/digit");
  237. MakeDir("/sdcard/log/message");
  238. MakeDir("/sdcard/log/source");
  239. }
  240. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::string _logdatapath, std::string _datafile)
  241. {
  242. logroot = _logroot;
  243. logfile = _logfile;
  244. datafile = _datafile;
  245. dataroot = _logdatapath;
  246. doLogFile = true;
  247. retentionInDays = 10;
  248. loglevel = 0;
  249. MakeDir("/sdcard/log/data");
  250. }