ClassLogFile.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. pFile = fopen(_fn.c_str(), "a+");
  19. printf("Logfile opened: %s\n", _fn.c_str());
  20. if (pFile!=NULL) {
  21. if (_time)
  22. {
  23. time_t rawtime;
  24. struct tm* timeinfo;
  25. char buffer[80];
  26. time(&rawtime);
  27. timeinfo = localtime(&rawtime);
  28. strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);
  29. zwtime = std::string(buffer);
  30. info = zwtime + ": " + info;
  31. }
  32. fputs(info.c_str(), pFile);
  33. fputs("\n", pFile);
  34. fclose(pFile);
  35. } else {
  36. ESP_LOGI(TAG, "Can't open log file %s", _fn.c_str());
  37. }
  38. }
  39. void ClassLogFile::SwitchOnOff(bool _doLogFile){
  40. doLogFile = _doLogFile;
  41. };
  42. void ClassLogFile::SetRetention(unsigned short _retentionInDays){
  43. retentionInDays = _retentionInDays;
  44. };
  45. void ClassLogFile::WriteToFile(std::string info, bool _time)
  46. {
  47. /*
  48. struct stat path_stat;
  49. if (stat(logroot.c_str(), &path_stat) != 0) {
  50. ESP_LOGI(TAG, "Create log folder: %s", logroot.c_str());
  51. if (mkdir_r(logroot.c_str(), S_IRWXU) == -1) {
  52. ESP_LOGI(TAG, "Can't create log foolder");
  53. }
  54. }
  55. */
  56. time_t rawtime;
  57. struct tm* timeinfo;
  58. char buffer[30];
  59. time(&rawtime);
  60. timeinfo = localtime(&rawtime);
  61. strftime(buffer, 30, logfile.c_str(), timeinfo);
  62. std::string logpath = logroot + "/" + buffer;
  63. WriteToDedicatedFile(logpath, info, _time);
  64. }
  65. std::string ClassLogFile::GetCurrentFileName()
  66. {
  67. time_t rawtime;
  68. struct tm* timeinfo;
  69. char buffer[60];
  70. time(&rawtime);
  71. timeinfo = localtime(&rawtime);
  72. strftime(buffer, 60, logfile.c_str(), timeinfo);
  73. std::string logpath = logroot + "/" + buffer;
  74. return logpath;
  75. }
  76. void ClassLogFile::RemoveOld()
  77. {
  78. if (retentionInDays == 0) {
  79. return;
  80. }
  81. time_t rawtime;
  82. struct tm* timeinfo;
  83. char cmpfilename[30];
  84. time(&rawtime);
  85. rawtime = addDays(rawtime, -retentionInDays);
  86. timeinfo = localtime(&rawtime);
  87. strftime(cmpfilename, 30, logfile.c_str(), timeinfo);
  88. //ESP_LOGE(TAG, "log file name to compare: %s", cmpfilename);
  89. DIR *dir = opendir(logroot.c_str());
  90. if (!dir) {
  91. ESP_LOGI(TAG, "Failed to stat dir : %s", logroot.c_str());
  92. return;
  93. }
  94. struct dirent *entry;
  95. int deleted = 0;
  96. int notDeleted = 0;
  97. while ((entry = readdir(dir)) != NULL) {
  98. if (entry->d_type == DT_REG) {
  99. //ESP_LOGI(TAG, "list log file : %s %s", entry->d_name, cmpfilename);
  100. if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) {
  101. ESP_LOGI(TAG, "delete log file : %s", entry->d_name);
  102. std::string filepath = logroot + "/" + entry->d_name;
  103. if (unlink(filepath.c_str()) == 0) {
  104. deleted ++;
  105. } else {
  106. ESP_LOGE(TAG, "can't delete file : %s", entry->d_name);
  107. }
  108. } else {
  109. notDeleted ++;
  110. }
  111. }
  112. }
  113. ESP_LOGI(TAG, "%d older log files deleted. %d current log files not deleted.", deleted, notDeleted);
  114. closedir(dir);
  115. }
  116. ClassLogFile::ClassLogFile(std::string _logroot, std::string _logfile)
  117. {
  118. logroot = _logroot;
  119. logfile = _logfile;
  120. doLogFile = true;
  121. retentionInDays = 10;
  122. }