1 16 package org.apache.log4j.gui; 17 18 19 import java.awt.Color ; 20 import java.awt.Image ; 21 import java.awt.Toolkit ; 22 import java.io.*; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 import java.util.StringTokenizer ; 26 import java.util.Hashtable ; 27 import java.util.ArrayList ; 28 29 import javax.swing.JPanel ; 30 31 import org.apache.log4j.*; 32 33 import org.apache.log4j.spi.LoggingEvent; 34 import org.apache.log4j.helpers.Loader; 35 import org.apache.log4j.helpers.QuietWriter; 36 import org.apache.log4j.helpers.TracerPrintWriter; 37 import org.apache.log4j.helpers.OptionConverter; 38 39 40 44 45 public class TextPanelAppender extends AppenderSkeleton { 46 47 TracerPrintWriter tp; 48 StringWriter sw; 49 QuietWriter qw; 50 LogTextPanel logTextPanel; 51 LogPublishingThread logPublisher; 52 53 final String COLOR_OPTION_FATAL = "Color.Fatal"; 54 final String COLOR_OPTION_ERROR = "Color.Error"; 55 final String COLOR_OPTION_WARN = "Color.Warn"; 56 final String COLOR_OPTION_INFO = "Color.Info"; 57 final String COLOR_OPTION_DEBUG = "Color.Debug"; 58 final String COLOR_OPTION_BACKGROUND = "Color.Background"; 59 final String FONT_NAME_OPTION = "Font.Name"; 60 final String FONT_SIZE_OPTION = "Font.Size"; 61 final String EVENT_BUFFER_SIZE_OPTION = "EventBuffer.Size"; 62 63 public TextPanelAppender(Layout layout, String name) { 64 this.layout = layout; 65 this.name = name; 66 this.sw = new StringWriter(); 67 this.qw = new QuietWriter(sw, errorHandler); 68 this.tp = new TracerPrintWriter(qw); 69 setLogTextPanel(new LogTextPanel()); 70 logPublisher = new LogPublishingThread(logTextPanel, Priority.ERROR, 500); 71 } 73 74 public 75 void close() { 76 } 77 78 public void append(LoggingEvent event) { 79 80 String text = this.layout.format(event); 81 82 if (event.throwable!=null) { 85 event.throwable.printStackTrace(tp); 86 for (int i=0; i< sw.getBuffer().length(); i++) { 87 if (sw.getBuffer().charAt(i)=='\t') 88 sw.getBuffer().replace(i,i+1," "); 89 } 90 text += sw.toString(); 91 sw.getBuffer().delete(0,sw.getBuffer().length()); 92 } 93 else 94 if(!text.endsWith("\n")) 95 text += "\n"; 96 97 logPublisher.publishEvent(event.priority, text); 98 } 99 100 public 101 JPanel getLogTextPanel() { 102 return logTextPanel; 103 } 104 105 public 106 String [] getOptionStrings() { 107 return new String [] { COLOR_OPTION_FATAL, COLOR_OPTION_ERROR, 108 COLOR_OPTION_WARN, COLOR_OPTION_INFO, COLOR_OPTION_DEBUG, 109 COLOR_OPTION_BACKGROUND, FONT_NAME_OPTION, FONT_SIZE_OPTION}; 110 } 111 112 113 public 114 void setName(String name) { 115 this.name = name; 116 } 117 118 protected 119 void setLogTextPanel(LogTextPanel logTextPanel) { 120 this.logTextPanel = logTextPanel; 121 logTextPanel.setTextBackground(Color.white); 122 } 123 124 public 125 void setOption(String option, String value) { 126 if (option.equalsIgnoreCase(COLOR_OPTION_FATAL)) 127 logTextPanel.setTextColor(Priority.FATAL,value); 128 if (option.equalsIgnoreCase(COLOR_OPTION_ERROR)) 129 logTextPanel.setTextColor(Priority.ERROR,value); 130 if (option.equalsIgnoreCase(COLOR_OPTION_WARN)) 131 logTextPanel.setTextColor(Priority.WARN,value); 132 if (option.equalsIgnoreCase(COLOR_OPTION_INFO)) 133 logTextPanel.setTextColor(Priority.INFO,value); 134 if (option.equalsIgnoreCase(COLOR_OPTION_DEBUG)) 135 logTextPanel.setTextColor(Priority.DEBUG,value); 136 if (option.equalsIgnoreCase(COLOR_OPTION_BACKGROUND)) 137 logTextPanel.setTextBackground(value); 138 if (option.equalsIgnoreCase(FONT_SIZE_OPTION)) 139 logTextPanel.setTextFontSize(Integer.parseInt(value)); 140 if (option.equalsIgnoreCase(FONT_NAME_OPTION)) 141 logTextPanel.setTextFontName(value); 142 if (option.equalsIgnoreCase(EVENT_BUFFER_SIZE_OPTION)) 143 logTextPanel.setEventBufferSize(Integer.parseInt(value)); 144 return; 145 } 146 147 public 148 boolean requiresLayout() { 149 return true; 150 } 151 152 153 154 class LogPublishingThread extends Thread { 155 156 LogTextPanel logTextPanel; 157 ArrayList evts; 158 Priority triggerPrio; 159 long pubInterval; 160 161 public LogPublishingThread(LogTextPanel logTextPanel, Priority triggerPrio, long pubInterval) { 162 this.logTextPanel = logTextPanel; 163 this.evts = new ArrayList (1000); 164 this.triggerPrio = triggerPrio; 165 this.pubInterval = pubInterval; 166 this.start(); 168 } 169 170 public void run() { 171 while(true) { 172 synchronized(evts) { 173 try { 174 evts.wait(pubInterval); 175 } 176 catch(InterruptedException e) {} 177 178 logTextPanel.newEvents((EventBufferElement[])evts.toArray(new EventBufferElement[evts.size()])); 179 180 evts.clear(); 181 } 182 } 183 184 } 185 186 public void publishEvent(Priority prio, String text) { 187 synchronized(evts) { 188 evts.add(new EventBufferElement(prio, text)); 189 if(triggerPrio != null && prio.isGreaterOrEqual(triggerPrio)) 190 evts.notify(); 191 } 192 } 193 } 194 195 } 197 class EventBufferElement { 198 199 public String text; 200 public Priority prio; 201 public int numLines; 202 203 EventBufferElement(Priority prio, String text) { 204 this.prio = prio; 205 this.text = text; 206 numLines = 1; 207 int pos = pos = text.indexOf('\n', 0); 208 int len = text.length() - 1; 209 210 while( (pos > 0) && (pos < len) ) 211 numLines++; 212 pos = text.indexOf('\n', pos + 1); 213 } 214 } 215 216 217 | Popular Tags |