]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/LogContentProvider.java
Logging management
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / views / LogContentProvider.java
1 package org.argeo.security.ui.views;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.List;
7
8 import org.argeo.ArgeoLogListener;
9 import org.eclipse.jface.viewers.ILazyContentProvider;
10 import org.eclipse.jface.viewers.TableViewer;
11 import org.eclipse.jface.viewers.Viewer;
12
13 /** A content provider maintaing an array of lines */
14 class LogContentProvider implements ILazyContentProvider, ArgeoLogListener {
15 private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
16
17 private final TableViewer viewer;
18 private List<String> lines;
19
20 public LogContentProvider(TableViewer viewer) {
21 this.viewer = viewer;
22 }
23
24 public synchronized void dispose() {
25 lines = null;
26 }
27
28 @SuppressWarnings("unchecked")
29 public synchronized void inputChanged(Viewer viewer, Object oldInput,
30 Object newInput) {
31 lines = (List<String>) newInput;
32 if (lines == null)
33 return;
34 this.viewer.setItemCount(lines.size());
35 }
36
37 public void updateElement(int index) {
38 viewer.replace(lines.get(index), index);
39 }
40
41 public synchronized void appendLog(String username, Long timestamp,
42 String level, String category, String thread, Object msg,
43 String[] exception) {
44 // check if valid
45 if (lines == null)
46 return;
47
48 String message = msg.toString();
49 StringBuffer buf = new StringBuffer("");
50 buf.append(dateFormat.format(new Date(timestamp))).append(" ");
51 buf.append(level).append(" ");
52 int count = 0;
53 String lastLine = null;
54 for (String line : message.split("\n")) {
55 if (count == 0)
56 lastLine = buf + line;
57 else
58 lastLine = line;
59 lines.add(lastLine);
60 count++;
61 }
62
63 if (exception != null) {
64 for (String ste : exception) {
65 lastLine = ste;
66 lines.add(lastLine);
67 }
68 }
69 final Object lastElement = lastLine;
70 viewer.getTable().getDisplay().asyncExec(new Runnable() {
71 public void run() {
72 viewer.setItemCount(lines.size());
73 if (lastElement != null)
74 viewer.reveal(lastElement);
75 }
76 });
77 }
78 // private class LogLine {
79 // private String message;
80 // }
81 }