8000 Merge pull request #226 from wilx/1.2.x-gh-136 · log4cplus/log4cplus@466963c · GitHub
[go: up one dir, main page]

Skip to content

Commit 466963c

Browse files
authored
Merge pull request #226 from wilx/1.2.x-gh-136
Fix GitHub issue #136 on 1.2.x branch.
2 parents f711e35 + e73655b commit 466963c

File tree

1 file changed

+99
-107
lines changed

1 file changed

+99
-107
lines changed

src/fileappender.cxx

Lines changed: 99 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -742,48 +742,6 @@ DailyRollingFileAppender::init(DailyRollingFileSchedule sch)
742742

743743
Time now = Time::gettimeofday();
744744
now.usec(0);
745-
struct tm time;
746-
now.localtime(&time);
747-
748-
time.tm_sec = 0;
749-
switch (schedule)
750-
{
751-
case MONTHLY:
752-
time.tm_mday = 1;
753-
time.tm_hour = 0;
754-
time.tm_min = 0;
755-
break;
756-
757-
case WEEKLY:
758-
time.tm_mday -= (time.tm_wday % 7);
759-
time.tm_hour = 0;
760-
time.tm_min = 0;
761-
break;
762-
763-
case DAILY:
764-
time.tm_hour = 0;
765-
time.tm_min = 0;
766-
break;
767-
768-
case TWICE_DAILY:
769-
if(time.tm_hour >= 12) {
770-
time.tm_hour = 12;
771-
}
772-
else {
773-
time.tm_hour = 0;
774-
}
775-
time.tm_min = 0;
776-
break;
777-
778-
case HOURLY:
779-
time.tm_min = 0;
780-
break;
781-
782-
case MINUTELY:
783-
break;
784-
};
785-
now.setTime(&time);
786-
787745
scheduledFilename = getFilename(now);
788746
nextRolloverTime = calculateNextRolloverTime(now);
789747
}
@@ -908,64 +866,142 @@ DailyRollingFileAppender::rollover(bool alreadyLocked)
908866
}
909867

910868

911-
869+
static
912870
Time
913-
DailyRollingFileAppender::calculateNextRolloverTime(const Time& t) const
871+
calculateNextRolloverTime(const Time& t, DailyRollingFileSchedule schedule)
914872
{
873+
struct tm next;
915874
switch(schedule)
916875
{
917876
case MONTHLY:
918877
{
919-
struct tm nextMonthTime;
920-
t.localtime(&nextMonthTime);
921-
nextMonthTime.tm_mon += 1;
922-
nextMonthTime.tm_isdst = 0;
878+
t.localtime (&next);
879+
next.tm_mon += 1;
880+
next.tm_mday = 1; // Round up to next month start
881+
next.tm_hour = 0;
882+
next.tm_min = 0;
883+
next.tm_sec = 0;
884+
next.tm_isdst = -1;
923885

924886
Time ret;
925-
if(ret.setTime(&nextMonthTime) == -1) {
887+
if (ret.setTime (&next) == -1)
888+
{
926889
helpers::getLogLog().error(
927890
LOG4CPLUS_TEXT("DailyRollingFileAppender::calculateNextRolloverTime()-")
928891
LOG4CPLUS_TEXT(" setTime() returned error"));
929892
// Set next rollover to 31 days in future.
930-
ret = round_time (t, 24 * 60 * 60) + Time(2678400);
893+
ret = round_time (t, 24 * 60 * 60) + Time (31 * 24 * 60 * 60);
931894
}
932-
933895
return ret;
934896
}
935897

936898
case WEEKLY:
937899
{
938-
Time next = round_time (t, 24 * 60 * 60) + Time (7 * 24 * 60 * 60);
939-
return adjust_for_time_zone (next, local_time_offset (next));
900+
t.localtime (&next);
901+
// Round up to next week
902+
next.tm_mday += (7 - next.tm_wday + 1);
903+
next.tm_hour = 0;
904+
next.tm_min = 0;
905+
next.tm_sec = 0;
906+
next.tm_isdst = -1;
907+
908+
Time ret;
909+< A93C div class="diff-text-inner"> if (ret.setTime (&next) == -1)
910+
{
911+
helpers::getLogLog().error(
912+
LOG4CPLUS_TEXT("DailyRollingFileAppender::calculateNextRolloverTime()-")
913+
LOG4CPLUS_TEXT(" setTime() returned error"));
914+
// Set next rollover to 7 days in future.
915+
ret = round_time (t, 24 * 60 * 60) + Time (7 * 24 * 60 * 60);
916+
}
917+
return ret;
940918
}
941919

942920
default:
943921
helpers::getLogLog ().error (
944-
LOG4CPLUS_TEXT ("DailyRollingFileAppender::calculateNextRolloverTime()-")
945-
LOG4CPLUS_TEXT (" invalid schedule value"));
922+
LOG4CPLUS_TEXT ("calculateNextRolloverTime()-")
923+
LOG4CPLUS_TEXT (" unhandled or invalid schedule value"));
946924
// Fall through.
947925

948926
case DAILY:
949927
{
950-
Time next = round_time_and_add (t, Time (24 * 60 * 60));
951-
return adjust_for_time_zone (next, local_time_offset (next));
928+
t.localtime (&next);
929+
next.tm_mday += 1;
930+
next.tm_hour = 0;
931+
next.tm_min = 0;
932+
next.tm_sec = 0;
933+
next.tm_isdst = -1;
934+
935+
Time ret;
936+
if (ret.setTime (&next) == -1)
937+
{
938+
helpers::getLogLog().error(
939+
LOG4CPLUS_TEXT("DailyRollingFileAppender::calculateNextRolloverTime()-")
940+
LOG4CPLUS_TEXT(" setTime() returned error"));
941+
// Set next rollover to 7 days in future.
942+
ret = round_time (t, 60 * 60) + Time (24 * 60 * 60);
943+
}
944+
return ret;
952945
}
953946

954947
case TWICE_DAILY:
955948
{
956-
Time next = round_time_and_add (t, Time (12 * 60 * 60));
957-
return adjust_for_time_zone (next, local_time_offset (next));
949+
t.localtime (&next);
950+
if (next.tm_hour < 12)
951+
next.tm_hour = 12;
952+
else
953+
next.tm_hour = 24;
954+
next.tm_min = 0;
955+
next.tm_sec = 0;
956+
next.tm_isdst = -1;
957+
958+
Time ret;
959+
if (ret.setTime (&next) == -1)
960+
{
961+
helpers::getLogLog().error(
962+
LOG4CPLUS_TEXT("DailyRollingFileAppender::calculateNextRolloverTime()-")
963+
LOG4CPLUS_TEXT(" setTime() returned error"));
964+
// Set next rollover to 7 days in future.
965+
ret = round_time (t, 60 * 60) + Time (12 * 60 * 60);
966+
}
967+
return ret;
958968
}
959969

960970
case HOURLY:
961-
return round_time_and_add (t, Time (60 * 60));
971+
{
972+
t.localtime (&next);
973+
next.tm_hour += 1;
974+
next.tm_min = 0;
975+
next.tm_sec = 0;
976+
next.tm_isdst = -1;
977+
978+
Time ret;
979+
if (ret.setTime (&next) == -1)
980+
{
981+
helpers::getLogLog().error(
982+
LOG4CPLUS_TEXT("DailyRollingFileAppender::calculateNextRolloverTime()-")
983+
LOG4CPLUS_TEXT(" setTime() returned error"));
984+
// Set next rollover to 7 days in future.
985+
ret = round_time (t, 60 * 60) + Time (60 * 60);
986+
}
987+
988+
return ret;
989+
}
962990

963991
case MINUTELY:
964992
return round_time_and_add (t, Time (60));
965993
};
966994
}
967995

968996

997+
Time
998+
DailyRollingFileAppender::calculateNextRolloverTime(const Time& t) const
999+
{
1000+
Time ret = log4cplus::calculateNextRolloverTime (t, schedule);
1001+
ret.usec (0);
1002+
return ret;
1003+
}
1004+
9691005

9701006
tstring
9711007
DailyRollingFileAppender::getFilename(const Time& t) const
@@ -1400,53 +1436,9 @@ TimeBasedRollingFileAppender::getRolloverPeriodDuration() const
14001436
Time
14011437
TimeBasedRollingFileAppender::calculateNextRolloverTime(const Time& t) const
14021438
{
1403-
Time result;
1404-
struct tm next;
1405-
1406-
switch(schedule)
1407-
{
1408-
case MONTHLY:
1409-
t.localtime(&next);
1410-
next.tm_mon += 1;
1411-
next.tm_mday = 0; // Round up to next month start
1412-
next.tm_hour = 0;
1413-
next.tm_min = 0;
1414-
next.tm_sec = 0;
1415-
next.tm_isdst = 0;
1416-
if (result.setTime(&next) == -1) {
1417-
result = t + Time(getRolloverPeriodDuration());
1418-
}
1419-
break;
1420-
1421-
case WEEKLY:
1422-
t.localtime(&next);
1423-
next.tm_mday += (7 - next.tm_wday + 1); // Round up to next week
1424-
next.tm_hour = 0; // Round up to next week start
1425-
next.tm_min = 0;
1426-
next.tm_sec = 0;
1427-
next.tm_isdst = 0;
1428-
if (result.setTime(&next) == -1) {
1429-
result = t + Time(getRolloverPeriodDuration());
1430-
}
1431-
break;
1432-
1433-
default:
1434-
case DAILY:
1435-
case HOURLY:
1436-
case MINUTELY:
1437-
{
1438-
int periodDuration = getRolloverPeriodDuration();
1439-
result = t + Time(periodDuration);
1440-
time_t seconds = result.sec();
1441-
int remainder = seconds % periodDuration;
1442-
result.sec(seconds - remainder);
1443-
break;
1444-
}
1445-
};
1446-
1447-
result.usec(0);
1448-
1449-
return result;
1439+
Time ret = log4cplus::calculateNextRolloverTime (t, schedule);
1440+
ret.usec (0);
1441+
return ret;
14501442
}
14511443

14521444
} // namespace log4cplus

0 commit comments

Comments
 (0)
0