Skip to content

Commit 01a17e2

Browse files
committed
Bug 573109: Use new StackWalker API
Java 9+ contains new API for getting/walking stack frames. It is much faster and has a much cleaner API than using Throwable.getStackTrace Change-Id: Id0c888aeb06665f10605ce5ab5f0a567249e068c
1 parent fe76286 commit 01a17e2

File tree

1 file changed

+21
-19
lines changed
  • terminal/plugins/org.eclipse.tm.terminal.control/src/org/eclipse/tm/internal/terminal/provisional/api

1 file changed

+21
-19
lines changed

terminal/plugins/org.eclipse.tm.terminal.control/src/org/eclipse/tm/internal/terminal/provisional/api/Logger.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.FileOutputStream;
1919
import java.io.PrintStream;
20+
import java.lang.StackWalker.StackFrame;
21+
import java.util.Optional;
2022

2123
import org.eclipse.core.runtime.IPath;
2224
import org.eclipse.core.runtime.IStatus;
@@ -51,6 +53,7 @@ public final class Logger {
5153
public static final String TRACE_DEBUG_LOG_HOVER = "org.eclipse.tm.terminal.control/debug/log/hover"; //$NON-NLS-1$
5254

5355
private static PrintStream logStream;
56+
private static StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
5457

5558
static {
5659
// Any of the known debugging options turns on the creation of the log file
@@ -165,17 +168,7 @@ public static final boolean isLogEnabled() {
165168
*/
166169
public static final void log(String message) {
167170
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$
179172
logStream.flush();
180173
}
181174
}
@@ -196,21 +189,30 @@ public static final void logException(Exception ex) {
196189
// Read my own stack to get the class name, method name, and line number
197190
// of where this method was called
198191
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-
205192
PrintStream tmpStream = System.err;
206-
207193
if (logStream != null) {
208194
tmpStream = logStream;
209195
}
210196

211-
tmpStream.println(className + "." + methodName + ":" + lineNumber + ": " + //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
197+
tmpStream.println(getCallSiteDescription() + ": " + //$NON-NLS-1$
212198
"Caught exception: " + ex); //$NON-NLS-1$
213199
ex.printStackTrace(tmpStream);
214200
}
215201
}
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+
}
216218
}

0 commit comments

Comments
 (0)