ClassLogFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 = "LOGFILE";
  17. ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt", "/sdcard/log/data", "data_%Y-%m-%d.csv");
  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, "HEAP", _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. // TODO add separate parameter to disable write of data.
  69. /*if (!doLogFile){
  70. return;
  71. }*/
  72. ESP_LOGD(TAG, "Datalogfile: %s", logpath.c_str());
  73. pFile = fopen(logpath.c_str(), "a+");
  74. if (pFile!=NULL) {
  75. fputs(_timestamp.c_str(), pFile);
  76. fputs(",", pFile);
  77. fputs(_name.c_str(), pFile);
  78. fputs(",", pFile);
  79. fputs(_ReturnRawValue.c_str(), pFile);
  80. fputs(",", pFile);
  81. fputs(_ReturnValue.c_str(), pFile);
  82. fputs(",", pFile);
  83. fputs(_ReturnPreValue.c_str(), pFile);
  84. fputs(",", pFile);
  85. fputs(_ReturnRateValue.c_str(), pFile);
  86. fputs(",", pFile);
  87. fputs(_ReturnChangeAbsolute.c_str(), pFile);
  88. fputs(",", pFile);
  89. fputs(_ErrorMessageText.c_str(), pFile);
  90. fputs(_digital.c_str(), pFile);
  91. fputs(_analog.c_str(), pFile);
  92. fputs("\n", pFile);
  93. fclose(pFile);
  94. } else {
  95. ESP_LOGE(TAG, "Can't open data file %s", logpath.c_str());
  96. }
  97. }
  98. void ClassLogFile::WriteToDedicatedFile(std::string _fn, esp_log_level_t level, std::string message, bool _time)
  99. {
  100. FILE* pFile;
  101. std::string zwtime;
  102. std::string logline = "";
  103. if (level > loglevel) {// Only write to file if loglevel is below threshold
  104. return;
  105. }
  106. // pFile = OpenFileAndWait(_fn.c_str(), "a");
  107. pFile = fopen(_fn.c_str(), "a+");
  108. // ESP_LOGD(TAG, "Logfile opened: %s", _fn.c_str());
  109. if (pFile!=NULL) {
  110. if (_time)
  111. {
  112. time_t rawtime;
  113. struct tm* timeinfo;
  114. char buffer[80];
  115. time(&rawtime);
  116. timeinfo = localtime(&rawtime);
  117. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo);
  118. zwtime = std::string(buffer);
  119. logline = zwtime;
  120. }
  121. std::string loglevelString;
  122. switch(level) {
  123. case ESP_LOG_ERROR:
  124. loglevelString = "ERR";
  125. break;
  126. case ESP_LOG_WARN:
  127. loglevelString = "WRN";
  128. break;
  129. case ESP_LOG_INFO:
  130. loglevelString = "INF";
  131. break;
  132. case ESP_LOG_DEBUG:
  133. loglevelString = "DBG";
  134. break;
  135. case ESP_LOG_VERBOSE:
  136. loglevelString = "VER";
  137. break;
  138. case ESP_LOG_NONE:
  139. default:
  140. loglevelString = "NONE";
  141. break;
  142. }
  143. logline = logline + "\t<" + loglevelString + ">\t" + message.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::setLogLevel(esp_log_level_t _logLevel){
  151. loglevel = _logLevel;
  152. std::string levelText;
  153. switch(_logLevel) {
  154. case ESP_LOG_WARN:
  155. levelText = "WARNING";
  156. break;
  157. case ESP_LOG_INFO:
  158. levelText = "INFO";
  159. break;
  160. case ESP_LOG_DEBUG:
  161. levelText = "DEBUG";
  162. break;
  163. case ESP_LOG_ERROR:
  164. default:
  165. levelText = "ERROR";
  166. break;
  167. }
  168. ESP_LOGI(TAG, "Log Level set to %s", levelText.c_str());
  169. /*
  170. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Test");
  171. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Test");
  172. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Test");
  173. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test");
  174. */
  175. };
  176. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  177. retentionInDays = _retentionInDays;
  178. };
  179. void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message, bool _time)
  180. {
  181. /*
  182. struct stat path_stat;
  183. if (stat(logroot.c_str(), &path_stat) != 0) {
  184. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  185. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  186. ESP_LOGE(TAG, "Can't create log folder");
  187. }
  188. }
  189. */
  190. time_t rawtime;
  191. struct tm* timeinfo;
  192. char buffer[30];
  193. time(&rawtime);
  194. timeinfo = localtime(&rawtime);
  195. strftime(buffer, 30, logfile.c_str(), timeinfo);
  196. std::string logpath = logroot + "/" + buffer;
  197. std::replace(message.begin(), message.end(), '\n', ' '); // Replace all newline characters
  198. if (tag != "") {
  199. ESP_LOG_LEVEL(level, tag.c_str(), "%s", message.c_str());
  200. message = "[" + tag + "] " + message;
  201. }
  202. else {
  203. ESP_LOG_LEVEL(level, "", "%s", message.c_str());
  204. }
  205. WriteToDedicatedFile(logpath, level, message, _time);
  206. }
  207. void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message) {
  208. LogFile.WriteToFile(level, tag, message, true);
  209. }
  210. std::string ClassLogFile::GetCurrentFileNameData()
  211. {
  212. time_t rawtime;
  213. struct tm* timeinfo;
  214. char buffer[60];
  215. time(&rawtime);
  216. timeinfo = localtime(&rawtime);
  217. strftime(buffer, 60, datafile.c_str(), timeinfo);
  218. std::string logpath = dataroot + "/" + buffer;
  219. return logpath;
  220. }
  221. std::string ClassLogFile::GetCurrentFileName()
  222. {
  223. time_t rawtime;
  224. struct tm* timeinfo;
  225. char buffer[60];
  226. time(&rawtime);
  227. timeinfo = localtime(&rawtime);
  228. strftime(buffer, 60, logfile.c_str(), timeinfo);
  229. std::string logpath = logroot + "/" + buffer;
  230. return logpath;
  231. }
  232. void ClassLogFile::RemoveOld()
  233. {
  234. if (retentionInDays == 0) {
  235. return;
  236. }
  237. time_t rawtime;
  238. struct tm* timeinfo;
  239. char cmpfilename[30];
  240. time(&rawtime);
  241. rawtime = addDays(rawtime, -retentionInDays + 1);
  242. timeinfo = localtime(&rawtime);
  243. //ESP_LOGD(TAG, "logfileRetentionInDays: %d", retentionInDays);
  244. ////////////////////// message /////////////////////////////////////////
  245. ESP_LOGI(TAG, "remove old log files");
  246. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  247. //ESP_LOGD(TAG, "log file name to compare: %s", cmpfilename);
  248. DIR *dir = opendir(logroot.c_str());
  249. if (!dir) {
  250. ESP_LOGE(TAG, "Failed to stat dir : %s", logroot.c_str());
  251. return;
  252. }
  253. struct dirent *entry;
  254. int deleted = 0;
  255. int notDeleted = 0;
  256. while ((entry = readdir(dir)) != NULL) {
  257. if (entry->d_type == DT_REG) {
  258. //ESP_LOGD(TAG, "compare log file : %s to %s", entry->d_name, cmpfilename);
  259. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  260. //ESP_LOGD(TAG, "delete log file : %s", entry->d_name);
  261. std::string filepath = logroot + "/" + entry->d_name;
  262. if (unlink(filepath.c_str()) == 0) {
  263. deleted ++;
  264. } else {
  265. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  266. notDeleted ++;
  267. }
  268. } else {
  269. notDeleted ++;
  270. }
  271. }
  272. }
  273. ESP_LOGI(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
  274. closedir(dir);
  275. ////////////////////// data /////////////////////////////////////////
  276. ESP_LOGI(TAG, "remove old data files");
  277. strftime(cmpfilename, 30, datafile.c_str(), timeinfo);
  278. //ESP_LOGD(TAG, "data file name to compare: %s", cmpfilename);
  279. dir = opendir(dataroot.c_str());
  280. if (!dir) {
  281. ESP_LOGE(TAG, "Failed to stat dir : %s", dataroot.c_str());
  282. return;
  283. }
  284. deleted = 0;
  285. notDeleted = 0;
  286. while ((entry = readdir(dir)) != NULL) {
  287. if (entry->d_type == DT_REG) {
  288. //ESP_LOGD(TAG, "Compare data file : %s to %s", entry->d_name, cmpfilename);
  289. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  290. //ESP_LOGD(TAG, "delete data file : %s", entry->d_name);
  291. std::string filepath = dataroot + "/" + entry->d_name;
  292. if (unlink(filepath.c_str()) == 0) {
  293. deleted ++;
  294. } else {
  295. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  296. notDeleted ++;
  297. }
  298. } else {
  299. notDeleted ++;
  300. }
  301. }
  302. }
  303. ESP_LOGI(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
  304. closedir(dir);
  305. }
  306. void ClassLogFile::CreateLogDirectories()
  307. {
  308. MakeDir("/sdcard/log");
  309. MakeDir("/sdcard/log/data");
  310. MakeDir("/sdcard/log/analog");
  311. MakeDir("/sdcard/log/digit");
  312. MakeDir("/sdcard/log/message");
  313. MakeDir("/sdcard/log/source");
  314. }
  315. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::string _logdatapath, std::string _datafile)
  316. {
  317. logroot = _logroot;
  318. logfile = _logfile;
  319. datafile = _datafile;
  320. dataroot = _logdatapath;
  321. retentionInDays = 10;
  322. loglevel = ESP_LOG_INFO;
  323. }