ClassLogFile.cpp 12 KB

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