1 package net.sf.panoptes.module.log4j;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Date;
6 import java.util.HashMap;
7 import java.util.Iterator;
8
9 import net.sf.panoptes.controller.MainController;
10 import net.sf.panoptes.model.node.Node;
11 import net.sf.panoptes.model.node.NodeUpdateEvent;
12 import net.sf.panoptes.view.configurator.NodeConfigurator;
13
14 import org.apache.log4j.Level;
15 import org.apache.log4j.spi.LoggingEvent;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.StyleRange;
19 import org.eclipse.swt.custom.StyledText;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.layout.FillLayout;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26
27 /***
28 *
29 *
30 * NodeConfigurator for <code>LoggerComponent</code>s. Displays log events i an
31 * console-like view.
32 *
33 * @author Dag Liodden
34 * @version 0.1
35 */
36 public class LoggerConsole extends NodeConfigurator {
37
38 StyledText output;
39 Combo comboLevel;
40 Level level;
41
42 LoggerRepository repository;
43 private LoggerComponent loggerLocation = null;
44
45 private static final Color BLACK = new Color(Display.getDefault(), 0, 0, 0);
46 private static final Color WHITE = new Color(Display.getDefault(), 255, 255, 255);
47 private static final Color RED = new Color(Display.getDefault(), 255, 0, 0);
48 private static final Color GREEN = new Color(Display.getDefault(), 0, 255, 0);
49 private static final Color BLUE = new Color(Display.getDefault(), 0, 0, 255);
50 private static final Color LIGHT_BLUE = new Color(Display.getDefault(), 160, 160, 255);
51 private static final Color YELLOW = new Color(Display.getDefault(), 255, 255, 0);
52
53 /***
54 * Constructor for LoggerTable.
55 * @param parent
56 * @param style
57 */
58 public LoggerConsole(MainController controller, Composite parent, int style, HashMap attributes) {
59 super(controller, parent, style, attributes);
60
61 this.repository = controller.getRootNode().getLoggerRepository();
62
63 setLayout(new FillLayout());
64
65 output = new StyledText(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
66 GridData gd = new GridData(GridData.FILL_BOTH);
67 gd.horizontalSpan = 4;
68 output.setLayoutData(gd);
69 output.setBackground(BLACK);
70 output.setForeground(WHITE);
71 output.setFont(JFaceResources.getBannerFont());
72 }
73
74 /***
75 * This method is used when refreshing the entire console. Setting the
76 * text and ranges all at ones increases the performance by a factor of 20-50
77 * on my computer, so it'w worth the redundancy. :)
78 *
79 * @param ei the event information
80 * @param offset the start offset in the text (so the ranges will be correct)
81 * @param buf the buffer to append the info to
82 * @param ranges the arraylist which the styleranges will be added to
83 */
84 private void addEvent(LoggingEventInfo ei, int offset, StringBuffer buf, ArrayList ranges) {
85 int startPos = buf.length() + offset;
86
87 LoggingEvent event = ei.getLoggingEvent();
88 String lvl = ei.getLoggingEvent().getLevel().toString();
89
90 Color fg;
91 switch (event.getLevel().toInt()) {
92 case Level.DEBUG_INT :
93 fg = BLUE;
94 break;
95 case Level.INFO_INT :
96 fg = BLUE;
97 break;
98 case Level.WARN_INT :
99 fg = YELLOW;
100 break;
101 case Level.ERROR_INT :
102 fg = RED;
103 break;
104 default :
105 fg = BLACK;
106 break;
107
108 }
109
110 String time = new Date(event.timeStamp).toString();
111 String className = event.getLoggerName();
112 className = "[" + className.substring(1 + className.lastIndexOf('.')) + "] ";
113
114 buf.append(time);
115 buf.append(" ");
116 buf.append(lvl);
117 buf.append("\t\t");
118 buf.append(className);
119 buf.append(ei.getLoggingEvent().getRenderedMessage().toString());
120 buf.append("\n");
121
122 // Add stacktrace
123 if (event.getThrowableInformation() != null) {
124 String[] trace = event.getThrowableInformation().getThrowableStrRep();
125 StringBuffer b = new StringBuffer();
126 for (int i = 0; i < trace.length; i++) {
127 b.append(trace[i] + "\n");
128 }
129 buf.append(b.toString());
130 }
131
132 // Highlight level
133 StyleRange sr = new StyleRange(startPos + time.length() + 1, lvl.length(), fg, BLACK);
134 ranges.add(sr);
135
136 // Highlight class
137 sr = new StyleRange(startPos + time.length() + 1 + lvl.length() + 2, className.length() - 1, LIGHT_BLUE, BLACK);
138 ranges.add(sr);
139 }
140
141 public void refresh() {
142 if (loggerLocation == null)
143 return;
144 output.setText("");
145 LoggingEventInfo[] kids = loggerLocation.listEvents();
146 ArrayList ranges = new ArrayList();
147 StringBuffer buf = new StringBuffer();
148
149 for (int i = 0; i < kids.length; i++) {
150 addEvent(kids[i], 0, buf, ranges);
151 }
152 output.setText(buf.toString());
153 output.setStyleRanges((StyleRange[]) ranges.toArray(new StyleRange[ranges.size()]));
154 output.setSelection(output.getOffsetAtLine(output.getLineCount() - 1));
155
156 }
157
158 /***
159 * Method setLocation.
160 * @param location
161 */
162 public void setLocation(LoggerComponent location) {
163 if (location == null)
164 return;
165 loggerLocation = location;
166 refresh();
167 }
168
169 public void setNode(Node component) {
170 setLocation((LoggerComponent) component);
171 }
172
173 /***
174 * Method getLoggerLocation.
175 */
176 public LoggerComponent getLoggerLocation() {
177 return loggerLocation;
178 }
179
180 /* (non-Javadoc)
181 * @see net.sf.panoptes.model.node.NodeUpdateListener#nodeUpdated(net.sf.panoptes.model.node.NodeUpdateEvent)
182 */
183 public void nodeUpdated(NodeUpdateEvent event) {
184 if (event instanceof LogComponentUpdateEvent) {
185 StringBuffer buf = new StringBuffer();
186 ArrayList ranges = new ArrayList();
187
188 int offset = output.getText().length();
189 for (Iterator i = ((LogComponentUpdateEvent) event).getNewEvents().iterator(); i.hasNext();) {
190 addEvent((LoggingEventInfo) i.next(), offset, buf, ranges);
191 }
192
193 output.append(buf.toString());
194
195 /***
196 * The new ranges have to be added AFTER the existing ranges.
197 */
198 ArrayList existingRanges = new ArrayList(Arrays.asList(output.getStyleRanges()));
199 existingRanges.addAll(ranges);
200
201 output.setStyleRanges((StyleRange[]) existingRanges.toArray(new StyleRange[existingRanges.size()]));
202 output.setSelection(output.getOffsetAtLine(output.getLineCount() - 1));
203 }
204 }
205
206 }
This page was automatically generated by Maven