From: <log...@li...> - 2016-11-06 12:49:02
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Log4cpp Git repository.". The branch, master has been updated via 626e36a137197405760e232faab8aafffc5a5f8e (commit) from fe4ed077cd0bf6f5f45666c9ba93f8e9b509c7c6 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- http://sourceforge.net/p/log4cpp/codegit/ci/ commit 626e36a137197405760e232faab8aafffc5a5f8e Author: Alexander Perepelkin <san...@us...> Date: Sun Nov 6 14:58:28 2016 +0400 bug-145: fix daily rolling; add manual test to see rolling diff --git a/src/DailyRollingFileAppender.cpp b/src/DailyRollingFileAppender.cpp index 5aef55a..74b2923 100644 --- a/src/DailyRollingFileAppender.cpp +++ b/src/DailyRollingFileAppender.cpp @@ -100,42 +100,42 @@ namespace log4cpp { return; for (int i = 0; i < nentries; i++) { struct stat statBuf; - int res = ::stat(entries[i]->d_name, &statBuf); + const std::string fullfilename = dirname + PATHDELIMITER + entries[i]->d_name; + int res = ::stat(fullfilename.c_str(), &statBuf); if ((res == -1) || (!S_ISREG(statBuf.st_mode))) { free(entries[i]); continue; } if (statBuf.st_mtime < oldest && strstr(entries[i]->d_name, filname.c_str())) { - const std::string fullfilename = dirname + PATHDELIMITER + entries[i]->d_name; - ::unlink(fullfilename.c_str()); std::cout << " Deleting " << fullfilename.c_str() << std::endl; + ::unlink(fullfilename.c_str()); } free(entries[i]); } free(entries); #else - HANDLE hFind = INVALID_HANDLE_VALUE; - WIN32_FIND_DATA ffd; - const std::string pattern = _fileName + "*"; - - hFind = FindFirstFile(pattern.c_str(), &ffd); - if (hFind != INVALID_HANDLE_VALUE) { - do { + HANDLE hFind = INVALID_HANDLE_VALUE; + WIN32_FIND_DATA ffd; + const std::string pattern = _fileName + "*"; + + hFind = FindFirstFile(pattern.c_str(), &ffd); + if (hFind != INVALID_HANDLE_VALUE) { + do { struct stat statBuf; const std::string fullfilename = dirname + PATHDELIMITER + ffd.cFileName; int res = ::stat(fullfilename.c_str(), &statBuf); - if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - std::cout << "Deleting " << fullfilename << "\n"; + if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + std::cout << "Deleting " << fullfilename << "\n"; ::unlink(fullfilename.c_str()); - } - } while (FindNextFile(hFind, &ffd) != 0); - - if (GetLastError() != ERROR_NO_MORE_FILES) { - // [XXX] some kind of error happened - } - FindClose(hFind); + } + } while (FindNextFile(hFind, &ffd) != 0); + + if (GetLastError() != ERROR_NO_MORE_FILES) { + // [XXX] some kind of error happened + } + FindClose(hFind); hFind = INVALID_HANDLE_VALUE; - } + } #endif } diff --git a/tests/testDailyRollingFileAppender.cpp b/tests/testDailyRollingFileAppender.cpp index c8b5a85..46d3fc0 100644 --- a/tests/testDailyRollingFileAppender.cpp +++ b/tests/testDailyRollingFileAppender.cpp @@ -1,10 +1,29 @@ #include <log4cpp/Category.hh> #include <log4cpp/PropertyConfigurator.hh> #include <log4cpp/DailyRollingFileAppender.hh> +#include <log4cpp/PatternLayout.hh> +#include <log4cpp/OstreamAppender.hh> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <iostream> +#include <ctime> +#include <sys/stat.h> +#include <fcntl.h> +#include <memory> +#include <cstring> + +#include "PortabilityImpl.hh" +#ifdef LOG4CPP_HAVE_IO_H +# include <io.h> +#endif +#ifdef LOG4CPP_HAVE_UNISTD_H +# include <unistd.h> +#endif + +#ifndef WIN32 // only available on Win32 +#include <dirent.h> +#endif #ifdef WIN32 #pragma comment(lib, "Ws2_32.lib") @@ -148,11 +167,146 @@ int testConfigDailyRollingFileAppender() return 0; } +// Note: this test changes system time. Run it only manually +namespace OnlyManualTesting { + + const char* absolutePathCategoryName = "absolutePathCategory"; + const int maxDaysToKeep = 3; + +#if defined(WIN32) + const char *logFilename = "C:/Temp/log4cpp/dailyrolling_abs_path_file.log"; + const char *logPathname = "C:/Temp/log4cpp"; +#else + const char *logFilename = "/var/log/log4cpp/dailyrolling_abs_path_file.log"; + const char *logPathname = "/var/log/log4cpp"; +#endif + + void setupManualEntryLog() { + if (access(logPathname, F_OK) != 0) { + mkdir(logPathname, 644); + } + + log4cpp::PatternLayout *ostreamLayout = new log4cpp::PatternLayout(); + ostreamLayout->setConversionPattern("%d: %p %c %x: %m %n"); + log4cpp::Appender *ostreamAppender = new log4cpp::OstreamAppender("ostreamAppender", &std::cout); + ostreamAppender->setLayout(ostreamLayout); + + log4cpp::PatternLayout *fileLayout = new log4cpp::PatternLayout(); + fileLayout->setConversionPattern("%d: %p %c %x: %m %n"); + log4cpp::Appender *fileAppender = new log4cpp::DailyRollingFileAppender("fileAppender", logFilename, maxDaysToKeep); + fileAppender->setLayout(fileLayout); + + log4cpp::Category& absolutePathCategory = + log4cpp::Category::getInstance(std::string(absolutePathCategoryName)); + absolutePathCategory.setAdditivity(false); + + absolutePathCategory.addAppender(ostreamAppender); + absolutePathCategory.addAppender(fileAppender); + absolutePathCategory.setPriority(log4cpp::Priority::DEBUG); + } + + int checkThatNoMoreThanNLogFilesPresent(const std::string _fileName, int n); + + int makeManualEntryLog() + { + const int totalLinesCount = 14, linesPerDay=3, jumpPeriod=24*60*60 + 1; + int i = 0, future = 0; + + log4cpp::Category& absolutePathCategory = + log4cpp::Category::getInstance(std::string(absolutePathCategoryName)); + + // 1. update system time (eg: use 'date' command on Linux) manually when test program is running here (at least 4 times) + absolutePathCategory.debugStream() << "debug line " << i; + while (++i <= totalLinesCount) { + if (i % linesPerDay == 0) { + time_t now; + if (time(&now) == -1) + return -1; + now += jumpPeriod; + future += jumpPeriod; + if (stime(&now) == -1) { + std::cerr << "Can not set date. Need admin privileges?" << std::endl; + return -1; + } + } + absolutePathCategory.debugStream() << "debug line " << i; + } + + time_t now; + if (time(&now) == -1) + return -1; + now -= future; + if (stime(&now) == -1) { + std::cerr << "Can not set date. Need admin privileges?" << std::endl; + return -1; + } + + // 2. check the number of files in /var/log/log4cpp ( <= maxDaysToKeep) (+1 to allow consequent runs of test) + if (checkThatNoMoreThanNLogFilesPresent(std::string(logFilename), maxDaysToKeep + 1) == -1) + return -1; + + return 0; + } + +// Note: this test changes system time. Run it only manually + int checkThatNoMoreThanNLogFilesPresent(const std::string _fileName, int n) { + // iterate over files around log file and count files with same prefix + const std::string::size_type last_delimiter = _fileName.rfind(PATHDELIMITER); + const std::string dirname((last_delimiter == std::string::npos)? "." : _fileName.substr(0, last_delimiter)); + const std::string filname((last_delimiter == std::string::npos)? _fileName : _fileName.substr(last_delimiter+1, _fileName.size()-last_delimiter-1)); + int logFilesCount(0); +#ifndef WIN32 // only available on Win32 + struct dirent **entries; + int nentries = scandir(dirname.c_str(), &entries, 0, alphasort); + if (nentries < 0) + return -1; + for (int i = 0; i < nentries; i++) { + if (strstr(entries[i]->d_name, filname.c_str())) { + ++logFilesCount; + } + free(entries[i]); + } + free(entries); +#else + HANDLE hFind = INVALID_HANDLE_VALUE; + WIN32_FIND_DATA ffd; + const std::string pattern = _fileName + "*"; + + hFind = FindFirstFile(pattern.c_str(), &ffd); + if (hFind != INVALID_HANDLE_VALUE) { + do { + if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + ++logFilesCount; + } + } while (FindNextFile(hFind, &ffd) != 0); + FindClose(hFind); + hFind = INVALID_HANDLE_VALUE; + } +#endif + if (logFilesCount > n) { + std::cerr << "Too many log files in the dir " << dirname << ": " << logFilesCount << std::endl; + } else { + std::cout << "Daily log files in the dir " << dirname << ": " << logFilesCount << std::endl; + } + + return (logFilesCount <= n) ? 0 : -1; + } + + int testDailyRollingFileAppenderChangeDateManualOnly() { + setupManualEntryLog(); + return makeManualEntryLog(); + } +} + int main() { int res = testOnlyDailyRollingFileAppender(); if (!res) res = testConfigDailyRollingFileAppender(); +// Note: this test changes system time. Run it only manually +// if (!res) +// res = OnlyManualTesting::testDailyRollingFileAppenderChangeDateManualOnly(); + return res; } ----------------------------------------------------------------------- Summary of changes: src/DailyRollingFileAppender.cpp | 40 ++++---- tests/testDailyRollingFileAppender.cpp | 154 ++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 20 deletions(-) hooks/post-receive -- Log4cpp Git repository. |