ExRollingFileAppender.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef EX_ROLLING_FILE_APPENDER_H
  2. #define EX_ROLLING_FILE_APPENDER_H
  3. #include <log4cpp/RollingFileAppender.hh>
  4. #include <log4cpp/LoggingEvent.hh>
  5. #include <sstream>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <iomanip>
  10. #include <memory>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <string>
  14. #include <stdarg.h>
  15. #include <unordered_map>
  16. class FileData{
  17. private:
  18. std::string fileName;
  19. std::string fileDate;
  20. public:
  21. FileData(std::string fileName,std::string fileDate){
  22. this->fileName = fileName;
  23. this->fileDate = fileDate;
  24. }
  25. public:
  26. std::string getFileName(){
  27. return fileName;
  28. }
  29. std::string getFileDate(){
  30. return fileDate;
  31. }
  32. void setFileName(std::string fileName){
  33. this->fileName = fileName;
  34. }
  35. void setFileDate(std::string fileDate){
  36. this->fileDate = fileDate;
  37. }
  38. };
  39. class ExRollingFileAppender : public RollingFileAppender{
  40. public:
  41. ExRollingFileAppender(const std::string& name,
  42. const std::string& fileName,
  43. size_t maxFileSize = 1*1024*1024, //lemon 2017/08/04
  44. unsigned int maxBackupIndex = 1,
  45. bool append = true,
  46. mode_t mode = 00644):RollingFileAppender(name,fileName,append,mode){
  47. //RollingFileAppender(name,fileName,append,mode);
  48. _maxBackupIndex = maxBackupIndex > 0 ? maxBackupIndex : 1;
  49. _maxBackupIndexWidth = (_maxBackupIndex > 0) ? log10((float)_maxBackupIndex)+1 : 1;
  50. _maxFileSize = maxFileSize;
  51. //strDate
  52. }
  53. protected:
  54. unsigned int _maxBackupIndex;
  55. unsigned short int _maxBackupIndexWidth;
  56. size_t _maxFileSize;
  57. private:
  58. std::string strDate;
  59. std::unordered_map<std::string,std::shared_ptr<FileData>> fileData;
  60. public:
  61. void setMaxBackupIndex(unsigned int maxBackups){
  62. _maxBackupIndex = maxBackups;
  63. _maxBackupIndexWidth = (_maxBackupIndex > 0) ? log10((float)_maxBackupIndex)+1 : 1;
  64. }
  65. unsigned int getMaxBackupIndex() const{
  66. return _maxBackupIndex;
  67. }
  68. void setMaximumFileSize(size_t maxFileSize){
  69. _maxFileSize = maxFileSize;
  70. }
  71. size_t getMaxFileSize() const{
  72. return _maxFileSize;
  73. }
  74. void rollOver(){
  75. if (_maxBackupIndex > 0) {
  76. std::ostringstream filename_stream;
  77. filename_stream << _fileName << "." << std::setw( _maxBackupIndexWidth ) << std::setfill( '0' ) << _maxBackupIndex << std::ends;
  78. // remove the very last (oldest) file
  79. std::string last_log_filename = filename_stream.str();
  80. // std::cout << last_log_filename << std::endl; // removed by request on sf.net #140
  81. ::remove(last_log_filename.c_str());
  82. // rename each existing file to the consequent one
  83. for(unsigned int i = _maxBackupIndex; i > 1; i--) {
  84. filename_stream.str(std::string());
  85. filename_stream << _fileName << '.' << std::setw( _maxBackupIndexWidth ) << std::setfill( '0' ) << i - 1 << std::ends; // set padding so the files are listed in order
  86. ::rename(filename_stream.str().c_str(), last_log_filename.c_str());
  87. last_log_filename = filename_stream.str();
  88. }
  89. // new file will be numbered 1
  90. ::rename(_fileName.c_str(), last_log_filename.c_str());
  91. }
  92. _fd = ::open(_fileName.c_str(), _flags, _mode);
  93. }
  94. void SubAppend(const LoggingEvent& event){
  95. RollingFileAppender::_append(event);
  96. off_t offset = ::lseek(_fd, 0, SEEK_END);
  97. if (offset < 0) {
  98. // XXX we got an error, ignore for now
  99. } else {
  100. //日期超过了就重建目录和文件
  101. //文件超过指定大小就重新建立文件
  102. if(static_cast<size_t>(offset) >= _maxFileSize) {
  103. rollOver();
  104. }
  105. }
  106. }
  107. std::string getIndex(int i,int maxBackupIndexLength){
  108. std::string index = "";
  109. return index;
  110. }
  111. };
  112. #endif