ClassLogFile.cpp 10 KB

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