ClassLogFile.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 = OpenFileAndWait(_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. std::string ClassLogFile::GetCurrentFileName()
  62. {
  63. time_t rawtime;
  64. struct tm* timeinfo;
  65. char buffer[30];
  66. time(&rawtime);
  67. timeinfo = localtime(&rawtime);
  68. strftime(buffer, 30, logfile.c_str(), timeinfo);
  69. std::string logpath = logroot + "/" + buffer;
  70. return logpath;
  71. }
  72. void ClassLogFile::RemoveOld()
  73. {
  74. if (retentionInDays == 0) {
  75. return;
  76. }
  77. time_t rawtime;
  78. struct tm* timeinfo;
  79. char cmpfilename[30];
  80. time(&rawtime);
  81. rawtime = addDays(rawtime, -retentionInDays);
  82. timeinfo = localtime(&rawtime);
  83. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  84. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  85. DIR *dir = opendir(logroot.c_str());
  86. if (!dir) {
  87. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  88. return;
  89. }
  90. struct dirent *entry;
  91. int deleted = 0;
  92. int notDeleted = 0;
  93. while ((entry = readdir(dir)) != NULL) {
  94. if (entry->d_type == DT_REG) {
  95. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  96. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  97. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  98. std::string filepath = logroot + "/" + entry->d_name;
  99. if (unlink(filepath.c_str()) == 0) {
  100. deleted ++;
  101. } else {
  102. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  103. }
  104. } else {
  105. notDeleted ++;
  106. }
  107. }
  108. }
  109. ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  110. closedir(dir);
  111. }
  112. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile)
  113. {
  114. logroot = _logroot;
  115. logfile = _logfile;
  116. doLogFile = true;
  117. retentionInDays = 10;
  118. }