1 package net.sf.panoptes.module.log4j;
2
3 import java.util.Date;
4 import java.util.HashMap;
5
6 import net.sf.panoptes.controller.MainController;
7 import net.sf.panoptes.model.node.Node;
8 import net.sf.panoptes.model.node.NodeUpdateEvent;
9 import net.sf.panoptes.view.configurator.NodeConfigurator;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.log4j.Level;
14 import org.apache.log4j.spi.LoggingEvent;
15 import org.eclipse.jface.viewers.ILabelProviderListener;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.ITableLabelProvider;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.SashForm;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.FillLayout;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.TableColumn;
37 import org.eclipse.swt.widgets.Text;
38
39 /***
40 * LoggerTable.java
41 *
42 * NodeConfigurator for <code>LoggerComponent</code>s.
43 *
44 * @author Dag Liodden
45 * @version 0.1
46 */
47 public class LoggerTable extends NodeConfigurator {
48
49 Log logger = LogFactory.getLog(this.getClass());
50
51 TableViewer table;
52 Combo comboLevel;
53 Text details;
54 final Text filter;
55
56 LoggerRepository repository;
57 private LoggerComponent loggerComponent = null;
58
59 /***
60 * Constructor for LoggerTable.
61 * @param parent
62 * @param style
63 */
64 public LoggerTable(MainController controller, Composite parent, int style, HashMap attributes) {
65 super(controller, parent, style, attributes);
66
67 this.repository = controller.getRootNode().getLoggerRepository();
68
69 setLayout(new FillLayout());
70
71 SashForm sash = new SashForm(this, SWT.VERTICAL);
72
73 Composite top = new Composite(sash, SWT.NULL);
74 top.setLayout(new GridLayout(4, false));
75
76 new Label(top, SWT.NULL).setText("Threshold:");
77 comboLevel = new Combo(top, SWT.DROP_DOWN | SWT.READ_ONLY);
78
79 comboLevel.add(Level.ERROR.toString());
80 comboLevel.add(Level.WARN.toString());
81 comboLevel.add(Level.INFO.toString());
82 comboLevel.add(Level.DEBUG.toString());
83
84 comboLevel.addListener(SWT.Selection, new Listener() {
85 public void handleEvent(Event arg0) {
86 Level l = Level.toLevel(comboLevel.getText());
87 setLevel(l);
88 refresh();
89 }
90 });
91
92 new Label(top, SWT.NULL).setText("Filter:");
93 filter = new Text(top, SWT.BORDER);
94 filter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95
96 filter.setText("");
97 filter.addListener(SWT.Modify, new Listener() {
98 public void handleEvent(Event event) {
99 loggerComponent.getFilter().setPrefix(filter.getText());
100 refresh();
101 }
102 });
103
104 table = new TableViewer(top, SWT.FULL_SELECTION | SWT.BORDER);
105 GridData gd = new GridData(GridData.FILL_BOTH);
106 gd.horizontalSpan = 4;
107 table.getTable().setLayoutData(gd);
108 table.getTable().setHeaderVisible(true);
109 table.getTable().setLinesVisible(true);
110
111 Composite bottom = new Composite(sash, SWT.NULL);
112 bottom.setLayout(new GridLayout());
113
114 Label tmp = new Label(bottom, SWT.NULL);
115 tmp.setText("Details:");
116 tmp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117
118 details = new Text(bottom, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
119 details.setLayoutData(new GridData(GridData.FILL_BOTH));
120 details.setEditable(false);
121
122 table.setContentProvider(new IStructuredContentProvider() {
123 public Object[] getElements(Object inputElement) {
124
125 Object[] kids = null;
126 if (loggerComponent != null)
127 kids = loggerComponent.listEvents();
128 return kids == null ? new Object[0] : kids;
129 }
130
131 public void dispose() {
132 }
133
134 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
135 }
136 });
137
138 table.setLabelProvider(new ITableLabelProvider() {
139 public Image getColumnImage(Object element, int columnIndex) {
140 if (columnIndex == 0) {
141 LoggingEvent m = ((LoggingEventInfo) element).getLoggingEvent();
142 Image icon;
143
144 switch (m.getLevel().toInt()) {
145 case Level.ERROR_INT :
146 {
147 icon = Node.ICON_ERROR;
148 break;
149 }
150 case Level.WARN_INT :
151 {
152 icon = Node.ICON_WARNING;
153 break;
154 }
155 case Level.INFO_INT :
156 case Level.DEBUG_INT :
157 {
158 icon = Node.ICON_INFO;
159 break;
160 }
161 default :
162 {
163 icon = null;
164 }
165 }
166 return icon == null ? null : icon;
167 } else
168 return null;
169 }
170
171 public String getColumnText(Object element, int columnIndex) {
172 LoggingEvent m = ((LoggingEventInfo) element).getLoggingEvent();
173 switch (columnIndex) {
174 case 0 :
175 {
176 return "";
177 }
178 case 1 :
179 {
180 return (new Date(m.timeStamp)).toString();
181 }
182 case 2 :
183 {
184 return m.getRenderedMessage() == null ? "-" : m.getRenderedMessage().toString();
185 }
186 case 3 :
187 {
188 return m.getLocationInformation().fullInfo == null
189 ? "-"
190 : m.getLocationInformation().fullInfo;
191 }
192 case 4 :
193 {
194 return m.getNDC() == null ? "-" : m.getNDC();
195 }
196 default :
197 {
198 return "Unknown";
199 }
200 }
201 }
202
203 public void addListener(ILabelProviderListener listener) {
204 }
205
206 public void dispose() {
207 }
208
209 public boolean isLabelProperty(Object element, String property) {
210 return false;
211 }
212
213 public void removeListener(ILabelProviderListener listener) {
214 }
215 });
216
217 table.addSelectionChangedListener(new ISelectionChangedListener() {
218 public void selectionChanged(SelectionChangedEvent event) {
219 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
220 LoggingEvent m = ((LoggingEventInfo) selection.getFirstElement()).getLoggingEvent();
221 String message = m.getMessage().toString();
222 String cause = "No stacktrace available";
223
224 if (m.getThrowableInformation() != null) {
225 String[] trace = m.getThrowableInformation().getThrowableStrRep();
226 StringBuffer b = new StringBuffer();
227 for (int i = 0; i < trace.length; i++) {
228 b.append(trace[i] + "\n");
229 }
230 cause = b.toString();
231 }
232 details.setText(
233 "Message:\n"
234 + message
235 + "\n\nLocation:\n"
236 + m.getLocationInformation().fullInfo
237 + "\n\nNDC:\n"
238 + m.getNDC()
239 + "\n\nCause:\n"
240 + cause);
241 }
242 });
243
244 TableColumn tc;
245
246 tc = new TableColumn(table.getTable(), SWT.NULL);
247 tc.setWidth(20);
248 tc.setText("!");
249
250 tc = new TableColumn(table.getTable(), SWT.NULL);
251 tc.setWidth(200);
252 tc.setText("Time");
253
254 tc = new TableColumn(table.getTable(), SWT.NULL);
255 tc.setWidth(200);
256 tc.setText("Message");
257
258 tc = new TableColumn(table.getTable(), SWT.NULL);
259 tc.setWidth(200);
260 tc.setText("Location");
261
262 tc = new TableColumn(table.getTable(), SWT.NULL);
263 tc.setWidth(200);
264 tc.setText("NDC");
265
266 table.setInput("");
267
268 addDisposeListener(new DisposeListener() {
269 public void widgetDisposed(DisposeEvent e) {
270 }
271 });
272 }
273
274 private void revealLastItem() {
275 if (table.getTable().getItemCount() > 0)
276 table.reveal(table.getElementAt(table.getTable().getItemCount() - 1));
277 }
278
279 /***
280 * Refreshes the table
281 */
282 public void refresh() {
283 table.getTable().setRedraw(false); // Increases performance
284 long start = System.currentTimeMillis(); // Benchmarking performance
285 try {
286 table.refresh(true);
287 } finally {
288 table.getTable().setRedraw(true);
289 }
290 revealLastItem();
291 logger.debug("Refresh time: " + (System.currentTimeMillis() - start));
292
293
294 }
295
296 public void setLoggerComponent(LoggerComponent component) {
297 loggerComponent = component;
298 if (component == null)
299 return;
300 setLevel(component.getFilter().getLevel());
301 refresh();
302 }
303
304 public void setNode(Node component) {
305 comboLevel.setText(((LoggerComponent) component).getFilter().getLevel().toString());
306 setLoggerComponent((LoggerComponent) component);
307 }
308
309 protected void setLevel(Level level) {
310 loggerComponent.getFilter().setLevel(level);
311 comboLevel.setText(level.toString());
312 }
313
314 public LoggerComponent getLoggerComponent() {
315 return loggerComponent;
316 }
317
318 /* (non-Javadoc)
319 * @see net.sf.panoptes.model.node.NodeUpdateListener#nodeUpdated(net.sf.panoptes.model.node.NodeUpdateEvent)
320 */
321 public void nodeUpdated(NodeUpdateEvent event) {
322 if (event instanceof LogComponentUpdateEvent) {
323 table.add(((LogComponentUpdateEvent) event).getNewEvents().toArray());
324 table.reveal(table.getElementAt(table.getTable().getItemCount() - 1));
325 }
326 }
327
328 }
This page was automatically generated by Maven