ClassLogFile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #include "../../include/defines.h"
  17. static const char *TAG = "LOGFILE";
  18. ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt", "/sdcard/log/data", "data_%Y-%m-%d.csv");
  19. void ClassLogFile::WriteHeapInfo(std::string _id)
  20. {
  21. if (loglevel >= ESP_LOG_DEBUG) {
  22. std::string _zw = _id + "\t" + getESPHeapInfo();
  23. WriteToFile(ESP_LOG_DEBUG, "HEAP", _zw);
  24. }
  25. }
  26. 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)
  27. {
  28. ESP_LOGD(TAG, "Start WriteToData");
  29. time_t rawtime;
  30. struct tm* timeinfo;
  31. char buffer[30];
  32. time(&rawtime);
  33. timeinfo = localtime(&rawtime);
  34. strftime(buffer, 30, datafile.c_str(), timeinfo);
  35. std::string logpath = dataroot + "/" + buffer;
  36. FILE* pFile;
  37. std::string zwtime;
  38. ESP_LOGD(TAG, "Datalogfile: %s", logpath.c_str());
  39. pFile = fopen(logpath.c_str(), "a+");
  40. if (pFile!=NULL) {
  41. fputs(_timestamp.c_str(), pFile);
  42. fputs(",", pFile);
  43. fputs(_name.c_str(), pFile);
  44. fputs(",", pFile);
  45. fputs(_ReturnRawValue.c_str(), pFile);
  46. fputs(",", pFile);
  47. fputs(_ReturnValue.c_str(), pFile);
  48. fputs(",", pFile);
  49. fputs(_ReturnPreValue.c_str(), pFile);
  50. fputs(",", pFile);
  51. fputs(_ReturnRateValue.c_str(), pFile);
  52. fputs(",", pFile);
  53. fputs(_ReturnChangeAbsolute.c_str(), pFile);
  54. fputs(",", pFile);
  55. fputs(_ErrorMessageText.c_str(), pFile);
  56. fputs(_digital.c_str(), pFile);
  57. fputs(_analog.c_str(), pFile);
  58. fputs("\n", pFile);
  59. fclose(pFile);
  60. } else {
  61. ESP_LOGE(TAG, "Can't open data file %s", logpath.c_str());
  62. }
  63. }
  64. void ClassLogFile::setLogLevel(esp_log_level_t _logLevel){
  65. loglevel = _logLevel;
  66. std::string levelText;
  67. switch(_logLevel) {
  68. case ESP_LOG_WARN:
  69. levelText = "WARNING";
  70. break;
  71. case ESP_LOG_INFO:
  72. levelText = "INFO";
  73. break;
  74. case ESP_LOG_DEBUG:
  75. levelText = "DEBUG";
  76. break;
  77. case ESP_LOG_ERROR:
  78. default:
  79. levelText = "ERROR";
  80. break;
  81. }
  82. ESP_LOGI(TAG, "Log Level set to %s", levelText.c_str());
  83. /*
  84. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Test");
  85. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Test");
  86. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Test");
  87. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Test");
  88. */
  89. }
  90. void ClassLogFile::SetLogFileRetention(unsigned short _LogFileRetentionInDays){
  91. logFileRetentionInDays = _LogFileRetentionInDays;
  92. }
  93. void ClassLogFile::SetDataLogRetention(unsigned short _DataLogRetentionInDays){
  94. dataLogRetentionInDays = _DataLogRetentionInDays;
  95. }
  96. void ClassLogFile::SetDataLogToSD(bool _doDataLogToSD){
  97. doDataLogToSD = _doDataLogToSD;
  98. }
  99. bool ClassLogFile::GetDataLogToSD(){
  100. return doDataLogToSD;
  101. }
  102. static FILE* logFileAppendHandle = NULL;
  103. std::string fileNameDate;
  104. void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message, bool _time)
  105. {
  106. time_t rawtime;
  107. struct tm* timeinfo;
  108. std::string fileNameDateNew;
  109. std::string zwtime;
  110. std::string ntpTime = "";
  111. time(&rawtime);
  112. timeinfo = localtime(&rawtime);
  113. char buf[30];
  114. strftime(buf, sizeof(buf), logfile.c_str(), timeinfo);
  115. fileNameDateNew = std::string(buf);
  116. std::replace(message.begin(), message.end(), '\n', ' '); // Replace all newline characters
  117. if (tag != "") {
  118. ESP_LOG_LEVEL(level, tag.c_str(), "%s", message.c_str());
  119. message = "[" + tag + "] " + message;
  120. }
  121. else {
  122. ESP_LOG_LEVEL(level, "", "%s", message.c_str());
  123. }
  124. if (level > loglevel) {// Only write to file if loglevel is below threshold
  125. return;
  126. }
  127. if (_time)
  128. {
  129. char logLineDate[30];
  130. strftime(logLineDate, sizeof(logLineDate), "%Y-%m-%dT%H:%M:%S", timeinfo);
  131. ntpTime = std::string(logLineDate);
  132. }
  133. std::string loglevelString;
  134. switch(level) {
  135. case ESP_LOG_ERROR:
  136. loglevelString = "ERR";
  137. break;
  138. case ESP_LOG_WARN:
  139. loglevelString = "WRN";
  140. break;
  141. case ESP_LOG_INFO:
  142. loglevelString = "INF";
  143. break;
  144. case ESP_LOG_DEBUG:
  145. loglevelString = "DBG";
  146. break;
  147. case ESP_LOG_VERBOSE:
  148. loglevelString = "VER";
  149. break;
  150. case ESP_LOG_NONE:
  151. default:
  152. loglevelString = "NONE";
  153. break;
  154. }
  155. std::string formatedUptime = getFormatedUptime(true);
  156. std::string fullmessage = "[" + formatedUptime + "] " + ntpTime + "\t<" + loglevelString + ">\t" + message + "\n";
  157. #ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
  158. if (fileNameDateNew != fileNameDate) { // Filename changed
  159. // Make sure each day gets its own logfile
  160. // Also we need to re-open it in case it needed to get closed for reading
  161. std::string logpath = logroot + "/" + fileNameDateNew;
  162. ESP_LOGI(TAG, "Opening logfile %s for appending", logpath.c_str());
  163. logFileAppendHandle = fopen(logpath.c_str(), "a+");
  164. if (logFileAppendHandle==NULL) {
  165. ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str());
  166. return;
  167. }
  168. fileNameDate = fileNameDateNew;
  169. }
  170. #else
  171. std::string logpath = logroot + "/" + fileNameDateNew;
  172. logFileAppendHandle = fopen(logpath.c_str(), "a+");
  173. if (logFileAppendHandle==NULL) {
  174. ESP_LOGE(TAG, "Can't open log file %s", logpath.c_str());
  175. return;
  176. }
  177. #endif
  178. fputs(fullmessage.c_str(), logFileAppendHandle);
  179. #ifdef KEEP_LOGFILE_OPEN_FOR_APPENDING
  180. fflush(logFileAppendHandle);
  181. fsync(fileno(logFileAppendHandle));
  182. #else
  183. CloseLogFileAppendHandle();
  184. #endif
  185. }
  186. void ClassLogFile::CloseLogFileAppendHandle() {
  187. if (logFileAppendHandle != NULL) {
  188. fclose(logFileAppendHandle);
  189. logFileAppendHandle = NULL;
  190. fileNameDate = "";
  191. }
  192. }
  193. void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message) {
  194. LogFile.WriteToFile(level, tag, message, true);
  195. }
  196. std::string ClassLogFile::GetCurrentFileNameData()
  197. {
  198. time_t rawtime;
  199. struct tm* timeinfo;
  200. char buffer[60];
  201. time(&rawtime);
  202. timeinfo = localtime(&rawtime);
  203. strftime(buffer, 60, datafile.c_str(), timeinfo);
  204. std::string logpath = dataroot + "/" + buffer;
  205. return logpath;
  206. }
  207. std::string ClassLogFile::GetCurrentFileName()
  208. {
  209. time_t rawtime;
  210. struct tm* timeinfo;
  211. char buffer[60];
  212. time(&rawtime);
  213. timeinfo = localtime(&rawtime);
  214. strftime(buffer, 60, logfile.c_str(), timeinfo);
  215. std::string logpath = logroot + "/" + buffer;
  216. return logpath;
  217. }
  218. void ClassLogFile::RemoveOldLogFile()
  219. {
  220. if (logFileRetentionInDays == 0) {
  221. return;
  222. }
  223. ESP_LOGD(TAG, "Remove old log files");
  224. time_t rawtime;
  225. struct tm* timeinfo;
  226. char cmpfilename[30];
  227. time(&rawtime);
  228. rawtime = addDays(rawtime, -logFileRetentionInDays + 1);
  229. timeinfo = localtime(&rawtime);
  230. //ESP_LOGD(TAG, "logFileRetentionInDays: %d", logFileRetentionInDays);
  231. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  232. //ESP_LOGD(TAG, "log file name to compare: %s", cmpfilename);
  233. DIR *dir = opendir(logroot.c_str());
  234. if (!dir) {
  235. ESP_LOGE(TAG, "Failed to stat dir: %s", logroot.c_str());
  236. return;
  237. }
  238. struct dirent *entry;
  239. int deleted = 0;
  240. int notDeleted = 0;
  241. while ((entry = readdir(dir)) != NULL) {
  242. if (entry->d_type == DT_REG) {
  243. //ESP_LOGD(TAG, "compare log file: %s to %s", entry->d_name, cmpfilename);
  244. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  245. //ESP_LOGD(TAG, "delete log file: %s", entry->d_name);
  246. std::string filepath = logroot + "/" + entry->d_name;
  247. if (unlink(filepath.c_str()) == 0) {
  248. deleted ++;
  249. } else {
  250. ESP_LOGE(TAG, "can't delete file: %s", entry->d_name);
  251. notDeleted ++;
  252. }
  253. } else {
  254. notDeleted ++;
  255. }
  256. }
  257. }
  258. ESP_LOGD(TAG, "log files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
  259. closedir(dir);
  260. }
  261. void ClassLogFile::RemoveOldDataLog()
  262. {
  263. if (dataLogRetentionInDays == 0 || !doDataLogToSD) {
  264. return;
  265. }
  266. ESP_LOGD(TAG, "Remove old data files");
  267. time_t rawtime;
  268. struct tm* timeinfo;
  269. char cmpfilename[30];
  270. time(&rawtime);
  271. rawtime = addDays(rawtime, -dataLogRetentionInDays + 1);
  272. timeinfo = localtime(&rawtime);
  273. //ESP_LOGD(TAG, "dataLogRetentionInDays: %d", dataLogRetentionInDays);
  274. strftime(cmpfilename, 30, datafile.c_str(), timeinfo);
  275. //ESP_LOGD(TAG, "data file name to compare: %s", cmpfilename);
  276. DIR *dir = opendir(dataroot.c_str());
  277. if (!dir) {
  278. ESP_LOGE(TAG, "Failed to stat dir: %s", dataroot.c_str());
  279. return;
  280. }
  281. struct dirent *entry;
  282. int deleted = 0;
  283. int notDeleted = 0;
  284. while ((entry = readdir(dir)) != NULL) {
  285. if (entry->d_type == DT_REG) {
  286. //ESP_LOGD(TAG, "Compare data file: %s to %s", entry->d_name, cmpfilename);
  287. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  288. //ESP_LOGD(TAG, "delete data file: %s", entry->d_name);
  289. std::string filepath = dataroot + "/" + entry->d_name;
  290. if (unlink(filepath.c_str()) == 0) {
  291. deleted ++;
  292. } else {
  293. ESP_LOGE(TAG, "can't delete file: %s", entry->d_name);
  294. notDeleted ++;
  295. }
  296. } else {
  297. notDeleted ++;
  298. }
  299. }
  300. }
  301. ESP_LOGD(TAG, "data files deleted: %d | files not deleted (incl. leer.txt): %d", deleted, notDeleted);
  302. closedir(dir);
  303. }
  304. void ClassLogFile::CreateLogDirectories()
  305. {
  306. MakeDir("/sdcard/log");
  307. MakeDir("/sdcard/log/data");
  308. MakeDir("/sdcard/log/analog");
  309. MakeDir("/sdcard/log/digit");
  310. MakeDir("/sdcard/log/message");
  311. MakeDir("/sdcard/log/source");
  312. }
  313. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile, std::string _logdatapath, std::string _datafile)
  314. {
  315. logroot = _logroot;
  316. logfile = _logfile;
  317. datafile = _datafile;
  318. dataroot = _logdatapath;
  319. logFileRetentionInDays = 3;
  320. dataLogRetentionInDays = 3;
  321. doDataLogToSD = true;
  322. loglevel = ESP_LOG_INFO;
  323. }