ClassLogFile.cpp 11 KB

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