ClassLogFile.cpp 10 KB

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