1313#include < memory>
1414#include < cstring>
1515
16- #include " PortabilityImpl.hh"
1716#ifdef LOG4CPP_HAVE_IO_H
1817# include < io.h>
1918#endif
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 {
174175const 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
185191if (access (logPathname, F_OK) != 0 ) {
186192mkdir (logPathname, 644 );
187193}
194+ #endif
188195
189196log4cpp::PatternLayout *ostreamLayout = new log4cpp::PatternLayout ();
190197ostreamLayout->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+
210243int makeManualEntryLog ()
211244{
212245const int totalLinesCount = 14 , linesPerDay=3 , jumpPeriod=24 *60 *60 + 1 ;
@@ -219,27 +252,15 @@ namespace OnlyManualTesting {
219252 absolutePathCategory.debugStream () << " debug line " << i;
220253while (++i <= totalLinesCount) {
221254if (i % linesPerDay == 0 ) {
222- time_t now;
223- if (time (&now) == -1 )
255+ if (jumpToFuture (jumpPeriod) == -1 )
224256return -1 ;
225- now += jumpPeriod;
226257future += 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 )
237263return -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()
305326res = 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
311332return res;
312333}
0 commit comments