KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > gui > TextPanelAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.gui;
17
18
19 import java.awt.Color JavaDoc;
20 import java.awt.Image JavaDoc;
21 import java.awt.Toolkit JavaDoc;
22 import java.io.*;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import javax.swing.JPanel JavaDoc;
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 /**
41  *
42  * @author James House
43  */

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 JavaDoc COLOR_OPTION_FATAL = "Color.Fatal";
54   final String JavaDoc COLOR_OPTION_ERROR = "Color.Error";
55   final String JavaDoc COLOR_OPTION_WARN = "Color.Warn";
56   final String JavaDoc COLOR_OPTION_INFO = "Color.Info";
57   final String JavaDoc COLOR_OPTION_DEBUG = "Color.Debug";
58   final String JavaDoc COLOR_OPTION_BACKGROUND = "Color.Background";
59   final String JavaDoc FONT_NAME_OPTION = "Font.Name";
60   final String JavaDoc FONT_SIZE_OPTION = "Font.Size";
61   final String JavaDoc EVENT_BUFFER_SIZE_OPTION = "EventBuffer.Size";
62
63   public TextPanelAppender(Layout layout, String JavaDoc 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     //logPublisher = new LogPublishingThread(logTextPanel, null, 500);
72
}
73
74   public
75   void close() {
76   }
77
78   public void append(LoggingEvent event) {
79
80     String JavaDoc text = this.layout.format(event);
81
82     // Print Stacktrace
83
// Quick Hack maybe there is a better/faster way?
84
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 JavaDoc getLogTextPanel() {
102     return logTextPanel;
103   }
104
105   public
106   String JavaDoc[] getOptionStrings() {
107     return new String JavaDoc[] { 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 JavaDoc 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 JavaDoc option, String JavaDoc 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 JavaDoc {
155
156     LogTextPanel logTextPanel;
157     ArrayList JavaDoc 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 JavaDoc(1000);
164       this.triggerPrio = triggerPrio;
165       this.pubInterval = pubInterval;
166       //this.setPriority(Thread.NORM_PRIORITY - 1);
167
this.start();
168     }
169
170     public void run() {
171       while(true) {
172         synchronized(evts) {
173           try {
174             evts.wait(pubInterval);
175           }
176           catch(InterruptedException JavaDoc 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 JavaDoc 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 } // TextPaneAppender
196

197 class EventBufferElement {
198
199   public String JavaDoc text;
200   public Priority prio;
201   public int numLines;
202
203   EventBufferElement(Priority prio, String JavaDoc 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