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