1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw;
19
20 import org.apache.log4j.helpers.Constants;
21 import org.apache.log4j.spi.LoggingEvent;
22
23 import java.util.Comparator;
24
25
26
27
28
29
30
31 public class ColumnComparator implements Comparator {
32 protected int index;
33 protected boolean ascending;
34 protected String columnName;
35
36 public ColumnComparator(String columnName, int index, boolean ascending) {
37 this.columnName = columnName;
38 this.index = index;
39 this.ascending = ascending;
40 }
41
42 public int compare(Object o1, Object o2) {
43 int sort = 1;
44
45 if (o1 instanceof LoggingEventWrapper && o2 instanceof LoggingEventWrapper) {
46
47
48
49 LoggingEvent e1 = ((LoggingEventWrapper) o1).getLoggingEvent();
50 LoggingEvent e2 = ((LoggingEventWrapper) o2).getLoggingEvent();
51
52 switch (index + 1) {
53 case ChainsawColumns.INDEX_LEVEL_COL_NAME:
54 sort = e1.getLevel().isGreaterOrEqual(e2.getLevel()) ? 1 : (-1);
55
56 break;
57
58 case ChainsawColumns.INDEX_LOGGER_COL_NAME:
59 sort = e1.getLoggerName().compareToIgnoreCase(e2.getLoggerName());
60
61 break;
62
63 case ChainsawColumns.INDEX_MESSAGE_COL_NAME:
64 sort =
65 e1.getMessage().toString().compareToIgnoreCase(
66 e2.getMessage().toString());
67
68 break;
69
70 case ChainsawColumns.INDEX_NDC_COL_NAME:
71 if (e1.getNDC() != null && e2.getNDC() != null) {
72 sort =
73 e1.getNDC().compareToIgnoreCase(
74 e2.getNDC());
75 } else if (e1.getNDC() == null && e2.getNDC() == null) {
76 sort = 0;
77 } else if (e1.getNDC() == null) {
78 sort = -1;
79 } else if (e2.getNDC() == null) {
80 sort = 1;
81 }
82
83 break;
84
85 case ChainsawColumns.INDEX_METHOD_COL_NAME:
86
87 if (
88 (e1.locationInformationExists())
89 & (e2.locationInformationExists())) {
90 sort =
91 e1.getLocationInformation().getMethodName().compareToIgnoreCase(
92 e2.getLocationInformation().getMethodName());
93 }
94
95 break;
96
97 case ChainsawColumns.INDEX_CLASS_COL_NAME:
98
99 if (
100 (e1.locationInformationExists())
101 & (e2.locationInformationExists())) {
102 sort =
103 e1.getLocationInformation().getClassName().compareToIgnoreCase(
104 e2.getLocationInformation().getClassName());
105 }
106
107 break;
108
109 case ChainsawColumns.INDEX_FILE_COL_NAME:
110
111 if (
112 (e1.locationInformationExists())
113 & (e2.locationInformationExists())) {
114 sort =
115 e1.getLocationInformation().getFileName().compareToIgnoreCase(
116 e2.getLocationInformation().getFileName());
117 }
118
119 break;
120
121 case ChainsawColumns.INDEX_TIMESTAMP_COL_NAME:
122 sort = (Long.compare(e1.getTimeStamp(), e2.getTimeStamp()));
123 break;
124
125 case ChainsawColumns.INDEX_THREAD_COL_NAME:
126 sort = e1.getThreadName().compareToIgnoreCase(e2.getThreadName());
127 break;
128
129 case ChainsawColumns.INDEX_ID_COL_NAME:
130 int id1 = Integer.parseInt(e1.getProperty(Constants.LOG4J_ID_KEY));
131 int id2 = Integer.parseInt(e2.getProperty(Constants.LOG4J_ID_KEY));
132 sort = Integer.compare(id2, id1);
133 break;
134
135 case ChainsawColumns.INDEX_THROWABLE_COL_NAME:
136 if (e1.getThrowableStrRep() != null && e2.getThrowableStrRep() != null) {
137 String[] s1 = e1.getThrowableStrRep();
138 String[] s2 = e2.getThrowableStrRep();
139 boolean foundDiff = false;
140 for (int i = 0; i < s1.length; i++) {
141 if (foundDiff || i > s2.length) {
142 break;
143 }
144 sort = s1[i].compareToIgnoreCase(s2[i]);
145 foundDiff = sort != 0;
146 }
147 }
148 break;
149
150 case ChainsawColumns.INDEX_LINE_COL_NAME:
151 if (
152 (e1.locationInformationExists())
153 & (e2.locationInformationExists())) {
154 sort =
155 e1.getLocationInformation().getLineNumber().compareToIgnoreCase(
156 e2.getLocationInformation().getLineNumber());
157 }
158 break;
159
160
161 default:
162 if (e1.getProperty(columnName) != null && e2.getProperty(columnName) != null) {
163 sort = e1.getProperty(columnName).compareToIgnoreCase(e2.getProperty(columnName));
164 }
165 }
166 }
167
168 sort = Integer.compare(sort, 0);
169
170 if (!ascending && (sort != 0)) {
171 sort = (sort < 0) ? 1 : (-1);
172 }
173
174 return sort;
175 }
176 }