You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2004/12/23 23:49:11 UTC

cvs commit: logging-log4j/tests/src/java/org/apache/log4j/pattern CacheUtilTest.java CachedDateFormatTest.java

ceki        2004/12/23 14:49:11

  Modified:    src/java/org/apache/log4j/pattern DatePatternConverter.java
                        CachedDateFormat.java CacheUtil.java
               tests/src/java/org/apache/log4j/pattern CacheUtilTest.java
                        CachedDateFormatTest.java
  Log:
  Changed the signatire of the CachedDateFormat constructor to CachedDateFormat(DateFormat) instead of CachedDateFormat(String pattern)
  all tests pass except CachedDateFormatTest.test5 which uses the thai locale
  
  Revision  Changes    Path
  1.14      +14 -6     logging-log4j/src/java/org/apache/log4j/pattern/DatePatternConverter.java
  
  Index: DatePatternConverter.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/DatePatternConverter.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DatePatternConverter.java	23 Dec 2004 20:37:45 -0000	1.13
  +++ DatePatternConverter.java	23 Dec 2004 22:49:11 -0000	1.14
  @@ -22,6 +22,7 @@
   
   import java.text.DateFormat;
   import java.text.FieldPosition;
  +import java.text.SimpleDateFormat;
   
   import java.util.Date;
   import java.util.TimeZone;
  @@ -39,7 +40,7 @@
     // appender method are serialized (per appender).
     StringBuffer buf;
     Logger logger = Logger.getLogger(DatePatternConverter.class);
  -  private DateFormat cdf;
  +  private DateFormat df;
     private Date date;
     protected FieldPosition pos = new FieldPosition(0);
     long lastTimestamp = 0;
  @@ -89,19 +90,26 @@
       }
   
       try {
  -      cdf = new CachedDateFormat(pattern);
  +      df = new SimpleDateFormat(pattern);
       } catch (IllegalArgumentException e) {
         logger.warn(
           "Could not instantiate SimpleDateFormat with pattern " + patternOption, e);
         // detault for the ISO8601 format
  -      cdf = new CachedDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
  +      pattern = "yyyy-MM-dd HH:mm:ss,SSS";
  +      df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
       }
  -
  +  
  +     
       // if the option list contains a TZ option, then set it.
       if (optionList != null && optionList.size() > 1) {
         TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
  -      cdf.setTimeZone(tz);
  +      df.setTimeZone(tz);
       }
  +    
  +    // if we can cache, so much the better
  +    if(CacheUtil.isPatternSafeForCaching(pattern)) {
  +      df = new CachedDateFormat(df);
  +    }     
     }
     
     public StringBuffer convert(LoggingEvent event) {
  @@ -115,7 +123,7 @@
         lastTimestamp = timestamp;
         date.setTime(timestamp);
         try {
  -        cdf.format(date, buf, pos);
  +        df.format(date, buf, pos);
           lastTimestamp = timestamp;
         } catch (Exception ex) {
           // this should never happen
  
  
  
  1.11      +33 -65    logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java
  
  Index: CachedDateFormat.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- CachedDateFormat.java	23 Dec 2004 21:33:18 -0000	1.10
  +++ CachedDateFormat.java	23 Dec 2004 22:49:11 -0000	1.11
  @@ -21,10 +21,8 @@
   import java.text.FieldPosition;
   import java.text.NumberFormat;
   import java.text.ParsePosition;
  -import java.text.SimpleDateFormat;
   
   import java.util.Date;
  -import java.util.Locale;
   import java.util.TimeZone;
   
   
  @@ -35,6 +33,7 @@
    */
   final class CachedDateFormat extends DateFormat {
     private static final int BAD_PATTERN = -1;
  +  private static final int NO_MILLISECONDS = -2;
     
     // Given that the JVM precision is 1/1000 of a second, 3 digit millisecond
     // precision is the best we can ever expect.
  @@ -49,59 +48,30 @@
     private StringBuffer milliBuf = new StringBuffer(JVM_MAX_MILLI_DIGITS);
     private NumberFormat numberFormat;
     
  -  public CachedDateFormat(String pattern) {
  -    this(pattern, null);
  -  }
  + 
   
  -  public CachedDateFormat(String pattern, Locale locale) {
  -    if (pattern == null) {
  -      throw new IllegalArgumentException("Pattern cannot be null");
  -    }
  -    if (locale == null) {
  -      this.formatter = new SimpleDateFormat(pattern);
  -    } else {
  -      this.formatter = new SimpleDateFormat(pattern, locale);
  +  public CachedDateFormat(DateFormat dateFormat) {
  +    if (dateFormat == null) {
  +      throw new IllegalArgumentException("dateFormat cannot be null");
       }
  +    formatter = dateFormat;
       
  -    String cleanedPattern = CacheUtil.removeLiterals(pattern);
  -    milliDigits = CacheUtil.computeSuccessiveS(cleanedPattern);
  -   
  -    if(!CacheUtil.isPatternSafeForCaching(cleanedPattern)) {
  -      millisecondStart = BAD_PATTERN;
  -      return;
  -    }
  +    numberFormat = new DecimalFormat();
  +    // numberFormat will zero as necessary
  +    numberFormat.setMinimumIntegerDigits(JVM_MAX_MILLI_DIGITS);
       
  -    if(milliDigits == 0) {
  -      // millisecondStart value won't be used
  -      millisecondStart = 0;
  -    } else if (milliDigits < JVM_MAX_MILLI_DIGITS) {
  -      // we don't deal well with these
  -      millisecondStart = BAD_PATTERN;
  -    } else if(milliDigits >= JVM_MAX_MILLI_DIGITS) {
  -      // if the number if millisecond digits is 3 or more, we can safely reduce
  -      // the precision to 3, because the values for the extra digits will always
  -      // be 0, thus immutable across iterations.
  -      milliDigits = JVM_MAX_MILLI_DIGITS;
  -      numberFormat = new DecimalFormat();
  -      // numberFormat will zero as necessary
  -      numberFormat.setMinimumIntegerDigits(JVM_MAX_MILLI_DIGITS);
  +    Date now = new Date();
  +    long nowTime = now.getTime();
  +    slotBegin = (nowTime / 1000L) * 1000L;
  +
  +    slotBeginDate = new Date(slotBegin);
  +    String formatted = formatter.format(slotBeginDate);
  +    cache.append(formatted);
  +    millisecondStart = findMillisecondStart(slotBegin, formatted, formatter);
  +    System.out.println("millisecondStart="+millisecondStart);
  +  } 
       
  -      Date now = new Date();
  -      long nowTime = now.getTime();
  -      slotBegin = (nowTime / 1000L) * 1000L;
  -
  -      slotBeginDate = new Date(slotBegin);
  -      String formatted = formatter.format(slotBeginDate);
  -      cache.append(formatted);
  -      millisecondStart = findMillisecondStart(slotBegin, formatted, formatter);
  -    } 
  -    
  -//    if(millisecondStart == BAD_PATTERN) {
  -//      System.out.println("BAD PATTERN");
  -//    } else {
  -//      System.out.println("millisecondStart="+millisecondStart);
  -//    }
  -  }
  +
   
     /**
      * Finds start of millisecond field in formatted time.
  @@ -116,25 +86,23 @@
       final long time, final String formatted, final DateFormat formatter) {
       String plus987 = formatter.format(new Date(time + 987));
   
  -    //
  -    //    find first difference between values
  -    //
  +    System.out.println("-formatted="+formatted);
  +    System.out.println("plus987="+plus987);
  +    // find first difference between values
       for (int i = 0; i < formatted.length(); i++) {
         if (formatted.charAt(i) != plus987.charAt(i)) {
  -        //
           //   if one string has "000" and the other "987"
           //      we have found the millisecond field
  -        //
           if (i + 3 <= formatted.length() 
  -            && formatted.substring(i, i + JVM_MAX_MILLI_DIGITS) == "000" 
  -            && plus987.substring(i, i + JVM_MAX_MILLI_DIGITS) == "987") {
  +            && "000".equals(formatted.substring(i, i + JVM_MAX_MILLI_DIGITS))  
  +            && "987".equals(plus987.substring(i, i + JVM_MAX_MILLI_DIGITS))) {
             return i;
           } else {
             return BAD_PATTERN;  
           }
         }
       }
  -    return BAD_PATTERN;
  +    return  NO_MILLISECONDS;
     }
   
     /**
  @@ -154,22 +122,22 @@
       
       long now = date.getTime();
       if ((now < (slotBegin + 1000L)) && (now >= slotBegin)) {
  -      //System.out.println("Using cached val:"+date);
  +      System.out.println("Using cached val:"+date);
   
  -      // caching is safe only for milliDigits == 3, if milliDigits == 0
  -      // we don't have to bother  with milliseonds at all.
  -      if (millisecondStart >= 0 && milliDigits == JVM_MAX_MILLI_DIGITS) {
  +      // If there are NO_MILLISECONDS we don't bother computing the millisecs.
  +      if (millisecondStart >= 0) {
           int millis = (int) (now - slotBegin);
           int cacheLength = cache.length();
   
           milliBuf.setLength(0);
           numberFormat.format(millis, milliBuf, fieldPosition);
  +        System.out.println("milliBuf="+milliBuf);
           for(int j = 0; j < JVM_MAX_MILLI_DIGITS; j++) {
             cache.setCharAt(millisecondStart+j, milliBuf.charAt(j));
           }
         }
       } else {
  -      //System.out.println("Refreshing the cache: "+date+","+(date.getTime()%1000));
  +      System.out.println("Refreshing the cache: "+date+","+(date.getTime()%1000));
         slotBegin = (now / 1000L) * 1000L;
         int prevLength = cache.length();
         cache.setLength(0);
  @@ -178,8 +146,8 @@
         //   if the length changed then
         //      recalculate the millisecond position
         if (cache.length() != prevLength && (milliDigits > 0)) {
  -        //System.out.println("Recomputing cached len changed oldLen="+prevLength
  -        //      +", newLen="+cache.length());
  +        System.out.println("Recomputing cached len changed oldLen="+prevLength
  +              +", newLen="+cache.length());
           //
           //    format the previous integral second
           StringBuffer tempBuffer = new StringBuffer(cache.length());
  
  
  
  1.5       +9 -3      logging-log4j/src/java/org/apache/log4j/pattern/CacheUtil.java
  
  Index: CacheUtil.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/CacheUtil.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CacheUtil.java	23 Dec 2004 20:37:17 -0000	1.4
  +++ CacheUtil.java	23 Dec 2004 22:49:11 -0000	1.5
  @@ -65,11 +65,17 @@
      * a non-sensical pattern, but unsafe nonetheless.
      */
     public static boolean isPatternSafeForCaching(String pattern) {
  -    // this code assumes that literals have been removed from the pattern
  -    if(pattern.indexOf("EEEE") != -1 && pattern.indexOf("MMMM") != -1) {
  + 
  +    String cleanedPattern = CacheUtil.removeLiterals(pattern);
  + 
  +    if(cleanedPattern.indexOf("EEEE") != -1 && cleanedPattern.indexOf("MMMM") != -1) {
         return false;
       }
  -    if(disjointS(pattern)) {
  +    if(disjointS(cleanedPattern)) {
  +      return false;
  +    }
  +    int successiveS = CacheUtil.computeSuccessiveS(cleanedPattern);
  +    if(successiveS == 1 || successiveS == 2) {
         return false;
       }
       return true;
  
  
  
  1.3       +2 -2      logging-log4j/tests/src/java/org/apache/log4j/pattern/CacheUtilTest.java
  
  Index: CacheUtilTest.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/pattern/CacheUtilTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CacheUtilTest.java	23 Dec 2004 20:37:17 -0000	1.2
  +++ CacheUtilTest.java	23 Dec 2004 22:49:11 -0000	1.3
  @@ -58,10 +58,10 @@
       assertEquals(true, result);
       
       result = CacheUtil.isPatternSafeForCaching("aS");
  -    assertEquals(true, result);
  +    assertEquals(false, result);
   
       result = CacheUtil.isPatternSafeForCaching("aSS");
  -    assertEquals(true, result);
  +    assertEquals(false, result);
   
       result = CacheUtil.isPatternSafeForCaching("aSSS");
       assertEquals(true, result);
  
  
  
  1.7       +50 -58    logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java
  
  Index: CachedDateFormatTest.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CachedDateFormatTest.java	23 Dec 2004 18:07:52 -0000	1.6
  +++ CachedDateFormatTest.java	23 Dec 2004 22:49:11 -0000	1.7
  @@ -34,7 +34,8 @@
      Unit test {@link AbsoluteTimeDateFormat}.
      @author Curt Arnold
      @since 1.3.0 */
  -public final class CachedDateFormatTest extends TestCase {
  +public final class CachedDateFormatTest
  +    extends TestCase {
   
     /**
      * Test constructor
  @@ -78,13 +79,10 @@
       //   subsequent calls within one minute
       //     are optimized to reuse previous formatted value
       //     make a couple of nearly spaced calls
  -    DateFormat gmtFormat = new CachedDateFormat(AbsoluteTimeDateFormat.ABS_TIME_DATE_PATTERN);
  -    gmtFormat.setTimeZone(GMT);
  +    DateFormat gmtFormat = new CachedDateFormat(new AbsoluteTimeDateFormat(GMT));
       long ticks = 12601L * 86400000L;
       Date jul1 = new Date(ticks);
  -    String r = gmtFormat.format(jul1);
  -    assertEquals("00:00:00,000", r);
  -    
  +    assertEquals("00:00:00,000", gmtFormat.format(jul1));
       Date plus8ms = new Date(ticks + 8);
       assertEquals("00:00:00,008", gmtFormat.format(plus8ms));
       Date plus17ms = new Date(ticks + 17);
  @@ -101,10 +99,8 @@
   
     public void test2() {
         Date jul2 = new Date(12602L * 86400000L);
  -      DateFormat gmtFormat = new CachedDateFormat(AbsoluteTimeDateFormat.ABS_TIME_DATE_PATTERN);
  -      gmtFormat.setTimeZone(GMT);
  -      DateFormat chicagoFormat = new CachedDateFormat(AbsoluteTimeDateFormat.ABS_TIME_DATE_PATTERN);
  -      chicagoFormat.setTimeZone(CHICAGO);
  +      DateFormat gmtFormat = new CachedDateFormat(new AbsoluteTimeDateFormat(GMT));
  +      DateFormat chicagoFormat = new CachedDateFormat(new AbsoluteTimeDateFormat(CHICAGO));
         assertEquals("00:00:00,000", gmtFormat.format(jul2));
         assertEquals("19:00:00,000", chicagoFormat.format(jul2));
         assertEquals("00:00:00,000", gmtFormat.format(jul2));
  @@ -117,8 +113,8 @@
       //   subsequent calls within one minute
       //     are optimized to reuse previous formatted value
       //     make a couple of nearly spaced calls
  -    DateFormat gmtFormat = new CachedDateFormat(AbsoluteTimeDateFormat.ABS_TIME_DATE_PATTERN);
  -    gmtFormat.setTimeZone(GMT);
  +    DateFormat gmtFormat = new CachedDateFormat(
  +       new AbsoluteTimeDateFormat(GMT));
       long ticks = -7L * 86400000L;
       Date jul1 = new Date(ticks);
       assertEquals("00:00:00,000", gmtFormat.format(jul1));
  @@ -137,10 +133,8 @@
       //     are optimized to reuse previous formatted value
       //     make a couple of nearly spaced calls
       SimpleDateFormat baseFormat =
  -         new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS Z");
  -    //     new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS Z", Locale.ENGLISH);
  -
  -    DateFormat cachedFormat = new CachedDateFormat("EEE, MMM dd, HH:mm:ss.SSS Z");
  +         new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS Z", Locale.ENGLISH);
  +    DateFormat cachedFormat = new CachedDateFormat(baseFormat);
       //
       //   use a date in 2000 to attempt to confuse the millisecond locator
       long ticks = 11141L * 86400000L;
  @@ -163,21 +157,39 @@
       //     are optimized to reuse previous formatted value
       //     make a couple of nearly spaced calls
       Locale thai = new Locale("th");
  -    String pattern = "EEE, MMM dd, HH:mm:ss.SSS Z";
       SimpleDateFormat baseFormat =
            new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS Z", thai);
  -    DateFormat cachedFormat = new CachedDateFormat(pattern, thai);
  +    DateFormat cachedFormat = new CachedDateFormat(baseFormat);
       //
  -    //   use a date in 2000 to attempt to confuse the millisecond locator
  +    // use a date in the year 2000 CE to attempt to confuse the millisecond locator
       long ticks = 11141L * 86400000L;
  +   
  +    String sx;
       Date jul1 = new Date(ticks);
  -    assertEquals(baseFormat.format(jul1), cachedFormat.format(jul1));
  +    sx = cachedFormat.format(jul1);
  +    System.out.println(baseFormat.format(jul1));
  +    System.out.println(sx);
  +    assertEquals(baseFormat.format(jul1), sx);
  +    
  +    sx = cachedFormat.format(jul1);
  +    System.out.println(baseFormat.format(jul1));
  +    System.out.println(sx);
  +    assertEquals(baseFormat.format(jul1), sx);
  +    
  +    
       Date plus8ms = new Date(ticks + 8);
  -    assertEquals(baseFormat.format(plus8ms), cachedFormat.format(plus8ms));
  +    sx = cachedFormat.format(plus8ms);
  +    System.out.println(baseFormat.format(plus8ms));
  +    System.out.println(sx);
  +    
  +    assertEquals(baseFormat.format(plus8ms), sx);
  +    
       Date plus17ms = new Date(ticks + 17);
       assertEquals(baseFormat.format(plus17ms), cachedFormat.format(plus17ms));
  +    
       Date plus237ms = new Date(ticks + 237);
       assertEquals(baseFormat.format(plus237ms), cachedFormat.format(plus237ms));
  +    
       Date plus1415ms = new Date(ticks + 1415);
       assertEquals(baseFormat.format(plus1415ms), cachedFormat.format(plus1415ms));
     }
  @@ -186,16 +198,15 @@
      * Checks that getNumberFormat does not return null.
      */
     public void test6() {
  -    assertNotNull(new SimpleDateFormat().getNumberFormat());
  +    assertNotNull(new CachedDateFormat(new SimpleDateFormat()).getNumberFormat());
     }
   
     /**
      * Set time zone on cached and check that it is effective.
      */
     public void test8() {
  -    String pattern = "yyyy-MM-dd HH:mm:ss,SSS";
  -    DateFormat baseFormat = new SimpleDateFormat(pattern);
  -    DateFormat cachedFormat = new CachedDateFormat(pattern);
  +    DateFormat baseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
  +    DateFormat cachedFormat = new CachedDateFormat(baseFormat);
       cachedFormat.setTimeZone(TimeZone.getTimeZone("GMT-6"));
       Date jul4 = new Date(12603L * 86400000L);
       assertEquals("2004-07-03 18:00:00,000", cachedFormat.format(jul4));
  @@ -206,9 +217,8 @@
      * Test of caching when less than three millisecond digits are specified.
      */
     public void test9() {
  -    String pattern = "yyyy-MMMM-dd HH:mm:ss,SS Z";
  -    DateFormat baseFormat = new SimpleDateFormat(pattern, Locale.US);
  -    CachedDateFormat cachedFormat = new CachedDateFormat(pattern);
  +    DateFormat baseFormat = new SimpleDateFormat("yyyy-MMMM-dd HH:mm:ss,SS Z", Locale.US);
  +    DateFormat cachedFormat = new CachedDateFormat(baseFormat);
       TimeZone cet = TimeZone.getTimeZone("GMT+1");
       cachedFormat.setTimeZone(cet);
       
  @@ -234,54 +244,36 @@
      * Test when millisecond position moves but length remains constant.
      */
     public void test10() {
  -    String pattern = "MMMM SSS EEEEEE";
  -    DateFormat cachedFormat = new CachedDateFormat(pattern);
  +    DateFormat baseFormat = new SimpleDateFormat("MMMM SSS EEEEEE", Locale.US);
  +    DateFormat cachedFormat = new CachedDateFormat(baseFormat);
  +    TimeZone cet = TimeZone.getTimeZone("GMT+1");
  +    cachedFormat.setTimeZone(cet);
       
       Calendar c = Calendar.getInstance();
       c.set(2004, Calendar.OCTOBER, 5, 20, 0);
       c.set(Calendar.SECOND, 37);
       c.set(Calendar.MILLISECOND, 23);
  +    c.setTimeZone(cet);
   
       String s = cachedFormat.format(c.getTime());
       assertEquals("October 023 Tuesday", s);
   
  -    // since we are in a different slot, cachedFormat will return correct
  -    // results since it will freshly format
       c.set(2004, Calendar.NOVEMBER, 1, 0, 0);
  -    c.set(Calendar.MILLISECOND, 6);
  -    s = cachedFormat.format(c.getTime());
  -    assertEquals("November 006 Monday", s);
  -
  -    // exercise the cache, (there should be none)
  +    c.set(Calendar.MILLISECOND, 23);
       s = cachedFormat.format(c.getTime());
  -    assertEquals("November 006 Monday", s);
  +    assertEquals("November 023 Monday", s);
   
  -  }
  -  
  -  public void testS2() {
  -    String pattern = "HH:mm:ss,SS";
  -    DateFormat cachedFormat = new CachedDateFormat(pattern);
  -    DateFormat sdf = new SimpleDateFormat(pattern);
  -    String s;
  -    
  -    Calendar c = Calendar.getInstance();
  -    c.set(2004, Calendar.OCTOBER, 5, 20, 54);
  -    c.set(Calendar.SECOND, 37);
  -    c.set(Calendar.MILLISECOND, 7);
   
  +    c.set(Calendar.MILLISECOND, 984);
       s = cachedFormat.format(c.getTime());
  -    assertEquals("20:54:37,07", s);
  -    
  -    // excercise the cache (if there is any)
  -    s = cachedFormat.format(c.getTime());
  -    assertEquals("20:54:37,07", s);
  +    assertEquals("November 984 Monday", s);
     }
     
  -  
     public static Test xsuite() {
       TestSuite suite = new TestSuite();
  -    suite.addTest(new CachedDateFormatTest("test9"));
  +    suite.addTest(new CachedDateFormatTest("test5"));
       //suite.addTest(new CachedDateFormatTest("testS2"));
       return suite;
     }
  -}
  +
  +}
  \ No newline at end of file
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org