nicko       2004/02/27 05:03:38

  Modified:    src/Appender RollingFileAppender.cs
               tests/src Utils.cs
               tests/src/Appender RollingFileAppenderTest.cs
  Log:
  Fixed ComputeCheckPeriod day rolling detection in pre GMT timezones.
  Fixed ComputeCheckPeriod month rolling detection.
  
  Revision  Changes    Path
  1.5       +38 -48    logging-log4net/src/Appender/RollingFileAppender.cs
  
  Index: RollingFileAppender.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/src/Appender/RollingFileAppender.cs,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RollingFileAppender.cs    23 Feb 2004 03:18:02 -0000      1.4
  +++ RollingFileAppender.cs    27 Feb 2004 13:03:38 -0000      1.5
  @@ -101,37 +101,37 @@
                        /// <summary>
                        /// Roll the log not based on the date
                        /// </summary>
  -                     TopOfTrouble    =-1,
  +                     InvalidRollPoint        =-1,
   
                        /// <summary>
                        /// Roll the log for each minute
                        /// </summary>
  -                     TopOfMinute             = 0,
  +                     TopOfMinute                     = 0,
   
                        /// <summary>
                        /// Roll the log for each hour
                        /// </summary>
  -                     TopOfHour               = 1,
  +                     TopOfHour                       = 1,
   
                        /// <summary>
                        /// Roll the log twice a day (midday and midnight)
                        /// </summary>
  -                     HalfDay                 = 2,
  +                     HalfDay                         = 2,
   
                        /// <summary>
                        /// Roll the log each day (midnight)
                        /// </summary>
  -                     TopOfDay                = 3,
  +                     TopOfDay                        = 3,
   
                        /// <summary>
                        /// Roll the log each week
                        /// </summary>
  -                     TopOfWeek               = 4,
  +                     TopOfWeek                       = 4,
   
                        /// <summary>
                        /// Roll the log each month
                        /// </summary>
  -                     TopOfMonth              = 5
  +                     TopOfMonth                      = 5
                }
   
                #endregion Protected Enums
  @@ -399,7 +399,7 @@
                                if (n >= m_nextCheck) 
                                {
                                        m_now = n;
  -                                     m_nextCheck = NextCheckDate(m_now);
  +                                     m_nextCheck = NextCheckDate(m_now, 
m_rollPoint);
        
                                        RollOverTime();
                                }
  @@ -692,8 +692,9 @@
                }
   
                /// <summary>
  -             /// Calculates the RollPoint for the m_datePattern supplied.
  +             /// Calculates the RollPoint for the datePattern supplied.
                /// </summary>
  +             /// <param name="datePattern">the date pattern to caluculate 
the check period for</param>
                /// <returns>The RollPoint that is most accurate for the date 
pattern supplied</returns>
                /// <remarks>
                /// Essentially the date pattern is examined to determine what 
the
  @@ -703,35 +704,33 @@
                /// and hour then the smallest roll point that can be detected 
would be
                /// and hourly roll point as minutes could not be detected.
                /// </remarks>
  -             private RollPoint ComputeCheckPeriod() 
  +             private RollPoint ComputeCheckPeriod(string datePattern) 
                {
  -                     if (m_datePattern != null) 
  -                     {
  -                             // set date to 1970-01-01 00:00:00Z this is 
UniversalSortableDateTimePattern 
  -                             // (based on ISO 8601) using universal time. 
This date is used for reference
  -                             // purposes to calculate the resolution of the 
date pattern.
  -                             DateTime epoch = DateTime.Parse("1970-01-01 
00:00:00Z", System.Globalization.DateTimeFormatInfo.InvariantInfo);
  +                     // set date to 1970-01-01 00:00:00 this is 
UniversalSortableDateTimePattern 
  +                     // (based on ISO 8601) using universal time. This date 
is used for reference
  +                     // purposes to calculate the resolution of the date 
pattern.
  +                     DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
   
  -                             // Get string representation of base line date
  -                             string r0 = epoch.ToString(m_datePattern, 
System.Globalization.DateTimeFormatInfo.InvariantInfo);
  +                     // Get string representation of base line date
  +                     string r0 = epoch.ToString(datePattern, 
System.Globalization.DateTimeFormatInfo.InvariantInfo);
   
  -                             // Check each type of rolling mode starting 
with the smallest increment.
  -                             for(int i = (int)RollPoint.TopOfMinute; i <= 
(int)RollPoint.TopOfMonth; i++) 
  -                             {
  -                                     // Get string representation of next 
pattern
  -                                     string r1 = NextCheckDate(epoch, 
(RollPoint)i).ToString(m_datePattern, 
System.Globalization.DateTimeFormatInfo.InvariantInfo);
  +                     // Check each type of rolling mode starting with the 
smallest increment.
  +                     for(int i = (int)RollPoint.TopOfMinute; i <= 
(int)RollPoint.TopOfMonth; i++) 
  +                     {
  +                             // Get string representation of next pattern
  +                             string r1 = NextCheckDate(epoch, 
(RollPoint)i).ToString(datePattern, 
System.Globalization.DateTimeFormatInfo.InvariantInfo);
   
  -                                     LogLog.Debug("RollingFileAppender: Type 
= ["+i+"], r0 = ["+r0+"], r1 = ["+r1+"]");
  +                             LogLog.Debug("RollingFileAppender: Type = 
["+i+"], r0 = ["+r0+"], r1 = ["+r1+"]");
   
  -                                     // Check if the string representations 
are different
  -                                     if (r0 != null && r1 != null && 
!r0.Equals(r1)) 
  -                                     {
  -                                             // Found highest precision roll 
point
  -                                             return (RollPoint)i;
  -                                     }
  +                             // Check if the string representations are 
different
  +                             if (r0 != null && r1 != null && !r0.Equals(r1)) 
  +                             {
  +                                     // Found highest precision roll point
  +                                     return (RollPoint)i;
                                }
                        }
  -                     return RollPoint.TopOfTrouble; // Deliberately head for 
trouble...
  +
  +                     return RollPoint.InvalidRollPoint; // Deliberately head 
for trouble...
                }
   
                /// <summary>
  @@ -744,10 +743,15 @@
                        if (m_rollDate && m_datePattern != null) 
                        {
                                m_now = m_dateTime.Now;
  -                             m_rollPoint = ComputeCheckPeriod();
  +                             m_rollPoint = ComputeCheckPeriod(m_datePattern);
  +
  +                             if (m_rollPoint == RollPoint.InvalidRollPoint)
  +                             {
  +                                     throw new ArgumentException("Invalid 
RollPoint, unable to parse ["+m_datePattern+"]");
  +                             }
   
                                // next line added as this removes the name 
check in rollOver
  -                             m_nextCheck = NextCheckDate(m_now);
  +                             m_nextCheck = NextCheckDate(m_now, m_rollPoint);
                        } 
                        else 
                        {
  @@ -976,20 +980,6 @@
                /// Roll on to the next interval after the date passed
                /// </summary>
                /// <param name="currentDateTime">the current date</param>
  -             /// <returns>the next roll point an interval after the 
currentDateTime date</returns>
  -             /// <remarks>
  -             /// Advances the date to the next roll point after the 
  -             /// currentDateTime date passed to the method.
  -             /// </remarks>
  -             protected DateTime NextCheckDate(DateTime currentDateTime) 
  -             {
  -                     return NextCheckDate(currentDateTime, m_rollPoint);
  -             }
  -
  -             /// <summary>
  -             /// Roll on to the next interval after the date passed
  -             /// </summary>
  -             /// <param name="currentDateTime">the current date</param>
                /// <param name="rollPoint">the type of roll point we are 
working with</param>
                /// <returns>the next roll point an interval after the 
currentDateTime date</returns>
                /// <remarks>
  @@ -1054,7 +1044,7 @@
                                        current = 
current.AddSeconds(-current.Second);
                                        current = 
current.AddMinutes(-current.Minute);
                                        current = 
current.AddHours(-current.Hour);
  -                                     current = 
current.AddDays(DateTime.DaysInMonth(current.Year, current.Month) - 
current.Day);
  +                                     current = current.AddMonths(1);
                                        break;
                        }         
                        return current;
  
  
  
  1.2       +22 -3     logging-log4net/tests/src/Utils.cs
  
  Index: Utils.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/tests/src/Utils.cs,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Utils.cs  26 Feb 2004 21:08:49 -0000      1.1
  +++ Utils.cs  27 Feb 2004 13:03:38 -0000      1.2
  @@ -26,18 +26,18 @@
        /// </summary>
        public class Utils
        {
  -             public Utils()
  +             private Utils()
                {
                }
   
                public static object InvokeMethod(object target, string name, 
params object[] args)
                {
  -                     return target.GetType().GetMethod(name, 
BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance).Invoke(target,
 args);
  +                     return target.GetType().GetMethod(name, 
BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance,
 null, GetTypesArray(args), null).Invoke(target, args);
                }
   
                public static object InvokeMethod(Type target, string name, 
params object[] args)
                {
  -                     return target.GetMethod(name, 
BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static).Invoke(null, 
args);
  +                     return target.GetMethod(name, 
BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static, null, 
GetTypesArray(args), null).Invoke(null, args);
                }
   
                public static object GetField(object target, string name)
  @@ -48,6 +48,25 @@
                public static void SetField(object target, string name, object 
val)
                {
                        target.GetType().GetField(name, 
BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance).SetValue(target,
 val);
  +             }
  +
  +             private static Type[] GetTypesArray(object[] args)
  +             {
  +                     Type[] types = new Type[args.Length];
  +
  +                     for(int i=0; i<args.Length; i++)
  +                     {
  +                             if (args[i] == null)
  +                             {
  +                                     types[i] = typeof(object);
  +                             }
  +                             else
  +                             {
  +                                     types[i] = args[i].GetType();
  +                             }
  +                     }
  +
  +                     return types;
                }
        }
   }
  
  
  
  1.3       +24 -1     
logging-log4net/tests/src/Appender/RollingFileAppenderTest.cs
  
  Index: RollingFileAppenderTest.cs
  ===================================================================
  RCS file: 
/home/cvs/logging-log4net/tests/src/Appender/RollingFileAppenderTest.cs,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RollingFileAppenderTest.cs        26 Feb 2004 21:08:49 -0000      1.2
  +++ RollingFileAppenderTest.cs        27 Feb 2004 13:03:38 -0000      1.3
  @@ -1514,5 +1514,28 @@
                {
                        Utils.SetField(appender, "m_maxSizeRollBackups", val);
                }
  -     } 
  +     }
  +
  +     [TestFixture] public class RollingFileAppenderSubClassTest : 
RollingFileAppender
  +     {
  +             [Test] public void TestComputeCheckPeriod()
  +             {
  +                     RollingFileAppender rfa = new RollingFileAppender();
  +
  +                     Assertion.AssertEquals("TopOfMinute pattern", 
RollPoint.TopOfMinute, InvokeComputeCheckPeriod(rfa, ".yyyy-MM-dd HH:mm"));
  +                     Assertion.AssertEquals("TopOfHour pattern", 
RollPoint.TopOfHour, InvokeComputeCheckPeriod(rfa, ".yyyy-MM-dd HH"));
  +                     Assertion.AssertEquals("HalfDay pattern", 
RollPoint.HalfDay, InvokeComputeCheckPeriod(rfa, ".yyyy-MM-dd tt"));
  +                     Assertion.AssertEquals("TopOfDay pattern", 
RollPoint.TopOfDay, InvokeComputeCheckPeriod(rfa, ".yyyy-MM-dd"));
  +                     Assertion.AssertEquals("TopOfMonth pattern", 
RollPoint.TopOfMonth, InvokeComputeCheckPeriod(rfa, ".yyyy-MM"));
  +
  +                     // Test invalid roll point
  +                     Assertion.AssertEquals("TopOfMonth pattern", 
RollPoint.InvalidRollPoint, InvokeComputeCheckPeriod(rfa, "..."));
  +             }
  +
  +             private static RollPoint 
InvokeComputeCheckPeriod(RollingFileAppender rollingFileAppender, string 
datePattern) 
  +             {
  +                     return 
(RollPoint)Utils.InvokeMethod(rollingFileAppender, "ComputeCheckPeriod", 
datePattern);
  +             }
  +     }
  +
   }
  
  
  

Reply via email to