@@ -742,48 +742,6 @@ DailyRollingFileAppender::init(DailyRollingFileSchedule sch)
742
742
743
743
Time now = Time::gettimeofday ();
744
744
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
-
787
745
scheduledFilename = getFilename (now);
788
746
nextRolloverTime = calculateNextRolloverTime (now);
789
747
}
@@ -908,64 +866,142 @@ DailyRollingFileAppender::rollover(bool alreadyLocked)
908
866
}
909
867
910
868
911
-
869
+ static
912
870
Time
913
- DailyRollingFileAppender:: calculateNextRolloverTime (const Time& t) const
871
+ calculateNextRolloverTime (const Time& t, DailyRollingFileSchedule schedule)
914
872
{
873
+ struct tm next;
915
874
switch (schedule)
916
875
{
917
876
case MONTHLY:
918
877
{
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 ;
923
885
924
886
Time ret;
925
- if (ret.setTime (&nextMonthTime) == -1 ) {
887
+ if (ret.setTime (&next) == -1 )
888
+ {
926
889
helpers::getLogLog ().error (
927
890
LOG4CPLUS_TEXT (" DailyRollingFileAppender::calculateNextRolloverTime()-" )
928
891
LOG4CPLUS_TEXT (" setTime() returned error" ));
929
892
// 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 );
931
894
}
932
-
933
895
return ret;
934
896
}
935
897
936
898
case WEEKLY:
937
899
{
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;
940
918
}
941
919
942
920
default :
943
921
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" ));
946
924
// Fall through.
947
925
948
926
case DAILY:
949
927
{
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;
952
945
}
953
946
954
947
case TWICE_DAILY:
955
948
{
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;
958
968
}
959
969
960
970
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
+ }
962
990
963
991
case MINUTELY:
964
992
return round_time_and_add (t, Time (60 ));
965
993
};
966
994
}
967
995
968
996
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
+
969
1005
970
1006
tstring
971
1007
DailyRollingFileAppender::getFilename (const Time& t) const
@@ -1400,53 +1436,9 @@ TimeBasedRollingFileAppender::getRolloverPeriodDuration() const
1400
1436
Time
1401
1437
TimeBasedRollingFileAppender::calculateNextRolloverTime (const Time& t) const
1402
1438
{
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;
1450
1442
}
1451
1443
1452
1444
} // namespace log4cplus
0 commit comments