ClassLogFile.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "ClassLogFile.h"
  2. #include "time_sntp.h"
  3. #include "esp_log.h"
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <dirent.h>
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. #include "Helper.h"
  15. static const char *TAG = "log";
  16. ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt", "/sdcard/log/data", "data_%Y-%m-%d.txt");
  17. void ClassLogFile::WriteHeapInfo(std::string _id)
  18. {
  19. std::string _zw = _id;
  20. if (loglevel > ESP_LOG_WARN)
  21. _zw = _zw + "\t" + getESPHeapInfo();
  22. WriteToFile(ESP_LOG_DEBUG, _zw);
  23. }
  24. std::string ClassLogFile::getESPHeapInfo(){
  25. string espInfoResultStr = "";
  26. char aMsgBuf[80];
  27. multi_heap_info_t aMultiHead_info ;
  28. heap_caps_get_info (&aMultiHead_info,MALLOC_CAP_8BIT);
  29. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  30. size_t aMinFreeHeadSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  31. size_t aMinFreeHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  32. size_t aHeapLargestFreeBlockSize = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
  33. sprintf(aMsgBuf,"Free Heap Size: \t%ld", (long) aFreeHeapSize);
  34. size_t aFreeSPIHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_SPIRAM);
  35. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  36. size_t aMinFreeInternalHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  37. sprintf(aMsgBuf,"\tHeap:\t%ld", (long) aFreeHeapSize);
  38. espInfoResultStr += string(aMsgBuf);
  39. sprintf(aMsgBuf,"\tMin Free:\t%ld", (long) aMinFreeHeapSize);
  40. espInfoResultStr += string(aMsgBuf);
  41. sprintf(aMsgBuf,"\tlarg. Block: \t%ld", (long) aHeapLargestFreeBlockSize);
  42. espInfoResultStr += string(aMsgBuf);
  43. sprintf(aMsgBuf,"\tSPI Heap:\t%ld", (long) aFreeSPIHeapSize);
  44. espInfoResultStr += string(aMsgBuf);
  45. sprintf(aMsgBuf,"\tMin Free Heap Size:\t%ld", (long) aMinFreeHeadSize);
  46. sprintf(aMsgBuf,"\tNOT_SPI Heap:\t%ld", (long) (aFreeHeapSize - aFreeSPIHeapSize));
  47. espInfoResultStr += string(aMsgBuf);
  48. sprintf(aMsgBuf,"\tlargest Block Size: \t%ld", (long) aHeapLargestFreeBlockSize);
  49. sprintf(aMsgBuf,"\tInternal Heap:\t%ld", (long) (aFreeInternalHeapSize));
  50. espInfoResultStr += string(aMsgBuf);
  51. sprintf(aMsgBuf,"\tInternal Min Heap free:\t%ld", (long) (aMinFreeInternalHeapSize));
  52. espInfoResultStr += string(aMsgBuf);
  53. return espInfoResultStr;
  54. }
  55. void ClassLogFile::WriteToData(std::string _ReturnRawValue, std::string _ReturnValue, std::string _ReturnPreValue, std::string _ErrorMessageText, std::string _digital, std::string _analog)
  56. {
  57. ESP_LOGD(TAG, "Start WriteToData");
  58. time_t rawtime;
  59. struct tm* timeinfo;
  60. char buffer[30];
  61. time(&rawtime);
  62. timeinfo = localtime(&rawtime);
  63. strftime(buffer, 30, datafile.c_str(), timeinfo);
  64. std::string logpath = dataroot + "/" + buffer;
  65. FILE* pFile;
  66. std::string zwtime;
  67. if (!doLogFile){
  68. return;
  69. }
  70. ESP_LOGD(TAG, "Datalogfile: %s", logpath.c_str());
  71. pFile = fopen(logpath.c_str(), "a+");
  72. if (pFile!=NULL) {
  73. time_t rawtime;
  74. struct tm* timeinfo;
  75. char buffer[80];
  76. time(&rawtime);
  77. timeinfo = localtime(&rawtime);
  78. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  79. zwtime = std::string(buffer) + ":\t";
  80. fputs(zwtime.c_str(), pFile);
  81. fputs(_ReturnRawValue.c_str(), pFile);
  82. fputs("\t", pFile);
  83. fputs(_ReturnValue.c_str(), pFile);
  84. fputs("\t", pFile);
  85. fputs(_ReturnPreValue.c_str(), pFile);
  86. fputs("\t", pFile);
  87. fputs(_ErrorMessageText.c_str(), pFile);
  88. fputs("\t", pFile);
  89. fputs(_digital.c_str(), pFile);
  90. fputs("\t", pFile);
  91. fputs(_analog.c_str(), pFile);
  92. fputs("\t", pFile);
  93. fputs("\n", pFile);
  94. fclose(pFile);
  95. } else {
  96. ESP_LOGE(TAG, "Can't open data file %s", logpath.c_str());
  97. }
  98. }
  99. void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level, std::string info, bool _time)
  100. {
  101. FILE* pFile;
  102. std::string zwtime;
  103. std::string logline = "";
  104. if (!doLogFile){
  105. return;
  106. }
  107. // pFile = OpenFileAndWait(_fn.c_str(), "a");
  108. pFile = fopen(_fn.c_str(), "a+");
  109. // ESP_LOGD(TAG, "Logfile opened: %s", _fn.c_str());
  110. if (pFile!=NULL) {
  111. if (_time)
  112. {
  113. time_t rawtime;
  114. struct tm* timeinfo;
  115. char buffer[80];
  116. time(&rawtime);
  117. timeinfo = localtime(&rawtime);
  118. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  119. zwtime = std::string(buffer);
  120. logline = zwtime;
  121. }
  122. std::string loglevelString;
  123. switch(level) {
  124. case ESP_LOG_NONE:
  125. loglevelString = "NONE";
  126. break;
  127. case ESP_LOG_ERROR:
  128. loglevelString = "ERR";
  129. break;
  130. case ESP_LOG_WARN:
  131. loglevelString = "WRN";
  132. break;
  133. case ESP_LOG_INFO:
  134. loglevelString = "INF";
  135. break;
  136. case ESP_LOG_DEBUG:
  137. loglevelString = "DBG";
  138. break;
  139. case ESP_LOG_VERBOSE:
  140. loglevelString = "VER";
  141. break;
  142. default:
  143. loglevelString = "NONE";
  144. break;
  145. }
  146. logline = logline + "\t<" + loglevelString + ">\t" + info.c_str() + "\n";
  147. fputs(logline.c_str(), pFile);
  148. fclose(pFile);
  149. } else {
  150. ESP_LOGE(TAG, "Can't open log file %s", _fn.c_str());
  151. }
  152. }
  153. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  154. doLogFile = _doLogFile;
  155. };
  156. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  157. retentionInDays = _retentionInDays;
  158. };
  159. void ClassLogFile::WriteToFile(esp_log_level_t level, std::string info, bool _time)
  160. {
  161. /*
  162. struct stat path_stat;
  163. if (stat(logroot.c_str(), &path_stat) != 0) {
  164. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  165. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  166. ESP_LOGE(TAG, "Can't create log folder");
  167. }
  168. }
  169. */
  170. time_t rawtime;
  171. struct tm* timeinfo;
  172. char buffer[30];
  173. time(&rawtime);
  174. timeinfo = localtime(&rawtime);
  175. strftime(buffer, 30, logfile.c_str(), timeinfo);
  176. std::string logpath = logroot + "/" + buffer;
  177. WriteToDedicatedFile(logpath, level, info, _time);
  178. ESP_LOG_LEVEL(level, TAG, "%s", info.c_str());
  179. }
  180. std::string ClassLogFile::GetCurrentFileName()
  181. {
  182. time_t rawtime;
  183. struct tm* timeinfo;
  184. char buffer[60];
  185. time(&rawtime);
  186. timeinfo = localtime(&rawtime);
  187. strftime(buffer, 60, logfile.c_str(), timeinfo);
  188. std::string logpath = logroot + "/" + buffer;
  189. return logpath;
  190. }
  191. void ClassLogFile::RemoveOld()
  192. {
  193. if (retentionInDays == 0) {
  194. return;
  195. }
  196. time_t rawtime;
  197. struct tm* timeinfo;
  198. char cmpfilename[30];
  199. time(&rawtime);
  200. rawtime = addDays(rawtime, -retentionInDays);
  201. timeinfo = localtime(&rawtime);
  202. ////////////////////// message /////////////////////////////////////////
  203. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  204. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  205. DIR *dir = opendir(logroot.c_str());
  206. if (!dir) {
  207. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  208. return;
  209. }
  210. struct dirent *entry;
  211. int deleted = 0;
  212. int notDeleted = 0;
  213. while ((entry = readdir(dir)) != NULL) {
  214. if (entry->d_type == DT_REG) {
  215. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  216. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) == 0)) {
  217. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  218. std::string filepath = logroot + "/" + entry->d_name;
  219. if (unlink(filepath.c_str()) == 0) {
  220. deleted ++;
  221. } else {
  222. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  223. }
  224. } else {
  225. notDeleted ++;
  226. }
  227. }
  228. }
  229. LogFile.WriteToFile(ESP_LOG_INFO, "logfiles deleted: " + std::to_string(deleted) + " files not deleted (incl. leer.txt): " + std::to_string(notDeleted));
  230. closedir(dir);
  231. ////////////////////// data /////////////////////////////////////////
  232. strftime(cmpfilename, 30, datafile.c_str(), timeinfo);
  233. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  234. dir = opendir(dataroot.c_str());
  235. if (!dir) {
  236. ESP_LOGI(TAG, "Failed to stat dir : %s", dataroot.c_str());
  237. return;
  238. }
  239. deleted = 0;
  240. notDeleted = 0;
  241. while ((entry = readdir(dir)) != NULL) {
  242. if (entry->d_type == DT_REG) {
  243. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  244. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  245. ESP_LOGI(TAG, "delete data file : %s", entry->d_name);
  246. std::string filepath = logroot + "/" + entry->d_name;
  247. if (unlink(filepath.c_str()) == 0) {
  248. deleted ++;
  249. } else {
  250. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  251. }
  252. } else {
  253. notDeleted ++;
  254. }
  255. }
  256. }
  257. ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  258. closedir(dir);
  259. }
  260. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::string _logdatapath, std::string _datafile)
  261. {
  262. logroot = _logroot;
  263. logfile = _logfile;
  264. datafile = _datafile;
  265. dataroot = _logdatapath;
  266. doLogFile = true;
  267. retentionInDays = 10;
  268. loglevel = ESP_LOG_INFO;
  269. MakeDir("/sdcard/log/data");
  270. MakeDir("/sdcard/test");
  271. MakeDir("/test");
  272. }