1 16 17 package org.apache.log4j.helpers; 18 19 import org.apache.log4j.spi.LoggingEvent; 20 21 35 public abstract class PatternConverter { 36 public PatternConverter next; 37 int min = -1; 38 int max = 0x7FFFFFFF; 39 boolean leftAlign = false; 40 41 protected 42 PatternConverter() { } 43 44 protected 45 PatternConverter(FormattingInfo fi) { 46 min = fi.min; 47 max = fi.max; 48 leftAlign = fi.leftAlign; 49 } 50 51 55 abstract 56 protected 57 String convert(LoggingEvent event); 58 59 62 public 63 void format(StringBuffer sbuf, LoggingEvent e) { 64 String s = convert(e); 65 66 if(s == null) { 67 if(0 < min) 68 spacePad(sbuf, min); 69 return; 70 } 71 72 int len = s.length(); 73 74 if(len > max) 75 sbuf.append(s.substring(len-max)); 76 else if(len < min) { 77 if(leftAlign) { 78 sbuf.append(s); 79 spacePad(sbuf, min-len); 80 } 81 else { 82 spacePad(sbuf, min-len); 83 sbuf.append(s); 84 } 85 } 86 else 87 sbuf.append(s); 88 } 89 90 static String [] SPACES = {" ", " ", " ", " ", " ", " " }; 94 97 public 98 void spacePad(StringBuffer sbuf, int length) { 99 while(length >= 32) { 100 sbuf.append(SPACES[5]); 101 length -= 32; 102 } 103 104 for(int i = 4; i >= 0; i--) { 105 if((length & (1<<i)) != 0) { 106 sbuf.append(SPACES[i]); 107 } 108 } 109 } 110 } 111 | Popular Tags |