1
2
3
4
5
6
7
8
9
10 package ch.qos.logback.classic.spi;
11
12 import ch.qos.logback.core.CoreConstants;
13
14
15
16
17
18
19 public class CallerData implements java.io.Serializable {
20
21 private static final long serialVersionUID = 2473626903716082403L;
22
23
24
25
26
27 public static final String NA = "?";
28
29
30 private static final String LOG4J_CATEGORY = "org.apache.log4j.Category";
31
32
33
34
35
36 public static final int LINE_NA = -1;
37
38
39 public static String CALLER_DATA_NA = "?#?:?"+CoreConstants.LINE_SEPARATOR;
40
41
42
43
44 public static CallerData[] EMPTY_CALLER_DATA_ARRAY = new CallerData[0];
45
46
47
48
49 int lineNumber;
50
51
52
53
54 String fileName;
55
56
57
58
59 String className;
60
61
62
63
64 String methodName;
65
66 boolean nativeMethod = false;
67
68 public CallerData(String fileName, String className, String methodName,
69 int lineNumber) {
70 this.fileName = fileName;
71 this.className = className;
72 this.methodName = methodName;
73 this.lineNumber = lineNumber;
74 }
75
76 public CallerData(StackTraceElement ste) {
77 className = ste.getClassName();
78 fileName = ste.getFileName();
79 methodName = ste.getMethodName();
80 lineNumber = ste.getLineNumber();
81 nativeMethod = ste.isNativeMethod();
82 }
83
84
85
86
87 public static CallerData[] extract(Throwable t, String fqnOfInvokingClass) {
88 if (t == null) {
89 return null;
90 }
91
92 StackTraceElement[] steArray = t.getStackTrace();
93 CallerData[] callerDataArray;
94
95 int found = LINE_NA;
96 for (int i = 0; i < steArray.length; i++) {
97 if(isDirectlyInvokingClass(steArray[i].getClassName(), fqnOfInvokingClass)) {
98
99 found = i + 1;
100 } else {
101 if(found != LINE_NA) {
102 break;
103 }
104 }
105 }
106
107
108 if(found == LINE_NA) {
109 return EMPTY_CALLER_DATA_ARRAY;
110 }
111
112 callerDataArray = new CallerData[steArray.length - found];
113 for (int i = found; i < steArray.length; i++) {
114 callerDataArray[i-found] = new CallerData(steArray[i]);
115 }
116 return callerDataArray;
117 }
118
119 public static boolean isDirectlyInvokingClass(String currentClass, String fqnOfInvokingClass) {
120
121
122 if(currentClass.equals(fqnOfInvokingClass) || currentClass.equals(LOG4J_CATEGORY)) {
123 return true;
124 } else {
125 return false;
126 }
127 }
128
129 public boolean equals(Object o) {
130
131 if (this == o) {
132 return true;
133 }
134
135 if (!(o instanceof CallerData)) {
136
137 return false;
138 }
139
140 CallerData r = (CallerData) o;
141
142 if (!getClassName().equals(r.getClassName())) {
143
144 return false;
145 }
146
147 if (!getFileName().equals(r.getFileName())) {
148
149 return false;
150 }
151
152 if (!getMethodName().equals(r.getMethodName())) {
153
154 return false;
155 }
156
157 if (!(lineNumber == r.lineNumber)) {
158
159 return false;
160 }
161
162 return true;
163 }
164
165
166
167
168
169 public String getClassName() {
170 return className;
171 }
172
173
174
175
176
177
178
179 public String getFileName() {
180 return fileName;
181 }
182
183
184
185
186
187
188
189 public int getLineNumber() {
190 return lineNumber;
191 }
192
193
194
195
196 public String getMethodName() {
197 return methodName;
198 }
199
200 public String toString() {
201 StringBuffer buf = new StringBuffer();
202 buf.append(getClassName());
203 buf.append('.');
204 buf.append(getMethodName());
205 buf.append('(');
206 if (isNativeMethod()) {
207 buf.append("Native Method");
208 } else if (getFileName() == null) {
209 buf.append("Unknown Source");
210 } else {
211 buf.append(getFileName());
212 buf.append(':');
213 buf.append(getLineNumber());
214 }
215 buf.append(')');
216 return buf.toString();
217 }
218
219 public boolean isNativeMethod() {
220 return nativeMethod;
221 }
222 }