17
17
18
18
import java .io .FileOutputStream ;
19
19
import java .io .PrintStream ;
20
+ import java .lang .StackWalker .StackFrame ;
21
+ import java .util .Optional ;
20
22
21
23
import org .eclipse .core .runtime .IPath ;
22
24
import org .eclipse .core .runtime .IStatus ;
@@ -51,6 +53,7 @@ public final class Logger {
51
53
public static final String TRACE_DEBUG_LOG_HOVER = "org.eclipse.tm.terminal.control/debug/log/hover" ; //$NON-NLS-1$
52
54
53
55
private static PrintStream logStream ;
56
+ private static StackWalker walker = StackWalker .getInstance (StackWalker .Option .RETAIN_CLASS_REFERENCE );
54
57
55
58
static {
56
59
// Any of the known debugging options turns on the creation of the log file
@@ -165,17 +168,7 @@ public static final boolean isLogEnabled() {
165
168
*/
166
169
public static final void log (String message ) {
167
170
if (logStream != null ) {
168
- // Read my own stack to get the class name, method name, and line
169
- // number of
170
- // where this method was called.
171
-
172
- StackTraceElement caller = new Throwable ().getStackTrace ()[1 ];
173
- int lineNumber = caller .getLineNumber ();
174
- String className = caller .getClassName ();
175
- String methodName = caller .getMethodName ();
176
- className = className .substring (className .lastIndexOf ('.' ) + 1 );
177
-
178
- logStream .println (className + "." + methodName + ":" + lineNumber + ": " + message ); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
171
+ logStream .println (getCallSiteDescription () + ": " + message ); //$NON-NLS-1$
179
172
logStream .flush ();
180
173
}
181
174
}
@@ -196,21 +189,30 @@ public static final void logException(Exception ex) {
196
189
// Read my own stack to get the class name, method name, and line number
197
190
// of where this method was called
198
191
if (logStream != null ) {
199
- StackTraceElement caller = new Throwable ().getStackTrace ()[1 ];
200
- int lineNumber = caller .getLineNumber ();
201
- String className = caller .getClassName ();
202
- String methodName = caller .getMethodName ();
203
- className = className .substring (className .lastIndexOf ('.' ) + 1 );
204
-
205
192
PrintStream tmpStream = System .err ;
206
-
207
193
if (logStream != null ) {
208
194
tmpStream = logStream ;
209
195
}
210
196
211
- tmpStream .println (className + "." + methodName + ":" + lineNumber + ": " + //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3 $
197
+ tmpStream .println (getCallSiteDescription () + ": " + //$NON-NLS-1$
212
198
"Caught exception: " + ex ); //$NON-NLS-1$
213
199
ex .printStackTrace (tmpStream );
214
200
}
215
201
}
202
+
203
+ /**
204
+ * Return a description string of the call site of this logging call for use in logged messages.
205
+ * This method will walk the stack to find the first method in the call stack not from the Logger
206
+ * class.
207
+ */
208
+ private static String getCallSiteDescription () {
209
+ Optional <StackFrame > stackFrame = walker
210
+ .walk (stream -> stream .filter (f -> f .getDeclaringClass () != Logger .class ).findFirst ());
211
+ int lineNumber = stackFrame .map (StackFrame ::getLineNumber ).orElse (0 );
212
+ String className = stackFrame .map (StackFrame ::getDeclaringClass ).map (Class ::getName )
213
+ .map (name -> name .substring (name .lastIndexOf ('.' ) + 1 )).orElse ("UnknownClass" ); //$NON-NLS-1$
214
+ String methodName = stackFrame .map (StackFrame ::getMethodName ).orElse ("unknownMethod" ); //$NON-NLS-1$
215
+ String locationString = className + "." + methodName + ":" + lineNumber ; //$NON-NLS-1$//$NON-NLS-2$
216
+ return locationString ;
217
+ }
216
218
}
0 commit comments