ClassLogFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "ClassLogFile.h"
  2. #include "time_sntp.h"
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include "Helper.h"
  8. static const char *TAG = "log";
  9. ClassLogFile LogFile("/sdcard/log/message", "log_%Y-%m-%d.txt");
  10. void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool _time)
  11. {
  12. FILE* pFile;
  13. std::string zwtime;
  14. if (!doLogFile){
  15. return;
  16. }
  17. pFile = fopen(_fn.c_str(), "a+");
  18. if (pFile!=NULL) {
  19. if (_time)
  20. {
  21. time_t rawtime;
  22. struct tm* timeinfo;
  23. char buffer[80];
  24. time(&rawtime);
  25. timeinfo = localtime(&rawtime);
  26. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  27. zwtime = std::string(buffer);
  28. info = zwtime + ": " + info;
  29. }
  30. fputs(info.c_str(), pFile);
  31. fputs("\n", pFile);
  32. fclose(pFile);
  33. } else {
  34. ESP_LOGI(TAG, "Can't open log file %s", _fn.c_str());
  35. }
  36. }
  37. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  38. doLogFile = _doLogFile;
  39. };
  40. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  41. retentionInDays = _retentionInDays;
  42. };
  43. void ClassLogFile::WriteToFile(std::string info, bool _time)
  44. {
  45. struct stat path_stat;
  46. if (stat(logroot.c_str(), &path_stat) != 0) {
  47. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  48. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  49. ESP_LOGI(TAG, "Can't create log foolder");
  50. }
  51. }
  52. time_t rawtime;
  53. struct tm* timeinfo;
  54. char buffer[30];
  55. time(&rawtime);
  56. timeinfo = localtime(&rawtime);
  57. strftime(buffer, 30, logfile.c_str(), timeinfo);
  58. std::string logpath = logroot + "/" + buffer;
  59. WriteToDedicatedFile(logpath, info, _time);
  60. }
  61. void ClassLogFile::RemoveOld()
  62. {
  63. if (retentionInDays == 0) {
  64. return;
  65. }
  66. time_t rawtime;
  67. struct tm* timeinfo;
  68. char cmpfilename[30];
  69. time(&rawtime);
  70. rawtime = addDays(rawtime, -retentionInDays);
  71. timeinfo = localtime(&rawtime);
  72. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  73. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  74. DIR *dir = opendir(logroot.c_str());
  75. if (!dir) {
  76. ESP_LOGE(TAG, "Failed to stat dir : %s", logroot.c_str());
  77. return;
  78. }
  79. struct dirent *entry;
  80. int deleted = 0;
  81. int notDeleted = 0;
  82. while ((entry = readdir(dir)) != NULL) {
  83. if (entry->d_type == DT_REG) {
  84. //ESP_LOGE(TAG, "list log file : %s", entry->d_name);
  85. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  86. ESP_LOGE(TAG, "delete log file : %s", entry->d_name);
  87. std::string filepath = logroot + "/" + entry->d_name;
  88. if (unlink(filepath.c_str()) == 0) {
  89. deleted ++;
  90. } else {
  91. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  92. }
  93. } else {
  94. notDeleted ++;
  95. }
  96. }
  97. }
  98. ESP_LOGE(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  99. closedir(dir);
  100. }
  101. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile)
  102. {
  103. logroot = _logroot;
  104. logfile = _logfile;
  105. doLogFile = true;
  106. retentionInDays = 10;
  107. }