Skip to content

Commit 2053240

Browse files
author
Alexander Perepelkin
committed
daily test support for windows
1 parent 626e36a commit 2053240

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

src/DailyRollingFileAppender.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ namespace log4cpp {
7373
void DailyRollingFileAppender::rollOver()
7474
{
7575
std::ostringstream filename_s;
76-
::close(_fd);
76+
int res_close = ::close(_fd);
77+
if (res_close != 0) {
78+
std::cerr << "Error closing file " << _fileName << std::endl;
79+
}
7780
filename_s << _fileName << "." << _logsTime.tm_year + 1900 << "-"
7881
<< std::setfill('0') << std::setw(2) << _logsTime.tm_mon + 1 << "-"
7982
<< std::setw(2) << _logsTime.tm_mday << std::ends;
8083
const std::string lastFn = filename_s.str();
81-
::rename(_fileName.c_str(), lastFn.c_str());
84+
int res_rename = ::rename(_fileName.c_str(), lastFn.c_str());
85+
if (res_rename != 0) {
86+
std::cerr << "Error renaming file " << _fileName << " to " << lastFn << std::endl;
87+
}
8288

8389
_fd = ::open(_fileName.c_str(), _flags, _mode);
90+
if (_fd == -1) {
91+
std::cerr << "Error opening file " << _fileName << std::endl;
92+
}
8493

8594
const time_t oldest = time(NULL) - _maxDaysToKeep * 60 * 60 * 24;
8695

tests/testDailyRollingFileAppender.cpp

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <memory>
1414
#include <cstring>
1515

16-
#include "PortabilityImpl.hh"
1716
#ifdef LOG4CPP_HAVE_IO_H
1817
# include <io.h>
1918
#endif
@@ -23,6 +22,8 @@
2322

2423
#ifndef WIN32 // only available on Win32
2524
#include <dirent.h>
25+
#else
26+
#include <direct.h>
2627
#endif
2728

2829
#ifdef WIN32
@@ -174,17 +175,23 @@ namespace OnlyManualTesting {
174175
const int maxDaysToKeep = 3;
175176

176177
#if defined(WIN32)
177-
const char *logFilename = "C:/Temp/log4cpp/dailyrolling_abs_path_file.log";
178-
const char *logPathname = "C:/Temp/log4cpp";
178+
const char *logFilename = "C:\\Temp\\log4cpp\\dailyrolling_abs_path_file.log";
179+
const char *logPathname = "C:\\Temp\\log4cpp";
179180
#else
180181
const char *logFilename = "/var/log/log4cpp/dailyrolling_abs_path_file.log";
181182
const char *logPathname = "/var/log/log4cpp";
182183
#endif
183184

184185
void setupManualEntryLog() {
186+
#if defined(WIN32)
187+
if (access(logPathname, 0) != 0) {
188+
mkdir(logPathname);
189+
}
190+
#else
185191
if (access(logPathname, F_OK) != 0) {
186192
mkdir(logPathname, 644);
187193
}
194+
#endif
188195

189196
log4cpp::PatternLayout *ostreamLayout = new log4cpp::PatternLayout();
190197
ostreamLayout->setConversionPattern("%d: %p %c %x: %m %n");
@@ -207,6 +214,32 @@ namespace OnlyManualTesting {
207214

208215
int checkThatNoMoreThanNLogFilesPresent(const std::string _fileName, int n);
209216

217+
int jumpToFuture(int seconds) {
218+
219+
#if defined(WIN32)
220+
SYSTEMTIME now;
221+
GetSystemTime(&now);
222+
now.wDay += seconds / (24*60*60);
223+
now.wSecond += 1;
224+
if (SetSystemTime(&now) == 0) {
225+
std::cerr << "Can not change system time. Probably not today... Try running as admin? Err: " << GetLastError() << std::endl;
226+
return -1;
227+
}
228+
#else
229+
time_t now;
230+
if (time(&now) == -1)
231+
return -1;
232+
233+
now += seconds;
234+
235+
if (stime(&now) == -1) {
236+
std::cerr << "Can not set date. Need admin privileges?" << std::endl;
237+
return -1;
238+
}
239+
#endif
240+
return 0;
241+
}
242+
210243
int makeManualEntryLog()
211244
{
212245
const int totalLinesCount = 14, linesPerDay=3, jumpPeriod=24*60*60 + 1;
@@ -219,27 +252,15 @@ namespace OnlyManualTesting {
219252
absolutePathCategory.debugStream() << "debug line " << i;
220253
while (++i <= totalLinesCount) {
221254
if (i % linesPerDay == 0) {
222-
time_t now;
223-
if (time(&now) == -1)
255+
if (jumpToFuture(jumpPeriod) == -1)
224256
return -1;
225-
now += jumpPeriod;
226257
future += jumpPeriod;
227-
if (stime(&now) == -1) {
228-
std::cerr << "Can not set date. Need admin privileges?" << std::endl;
229-
return -1;
230-
}
231258
}
232259
absolutePathCategory.debugStream() << "debug line " << i;
233260
}
234261

235-
time_t now;
236-
if (time(&now) == -1)
262+
if (jumpToFuture(0-future) == -1)
237263
return -1;
238-
now -= future;
239-
if (stime(&now) == -1) {
240-
std::cerr << "Can not set date. Need admin privileges?" << std::endl;
241-
return -1;
242-
}
243264

244265
// 2. check the number of files in /var/log/log4cpp ( <= maxDaysToKeep) (+1 to allow consequent runs of test)
245266
if (checkThatNoMoreThanNLogFilesPresent(std::string(logFilename), maxDaysToKeep + 1) == -1)
@@ -305,8 +326,8 @@ int main()
305326
res = testConfigDailyRollingFileAppender();
306327

307328
// Note: this test changes system time. Run it only manually
308-
//if (!res)
309-
//res = OnlyManualTesting::testDailyRollingFileAppenderChangeDateManualOnly();
329+
if (!res)
330+
res = OnlyManualTesting::testDailyRollingFileAppenderChangeDateManualOnly();
310331

311332
return res;
312333
}

0 commit comments

Comments
 (0)