Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Console - what are the rules that make stack traces clickable?

I log quite a bit of stuff, and have noticed that Eclipse Console makes Java stacktraces clickable. The exception is highlighted (goes to the "Create Breakpoint" dialog box) and the filename + numbers are highlighted too (allows going straight to that line).

I was wondering if I could format my normal log lines so the Eclipse Console would do the same to them. A possible approach could be making them look like stack trace lines, but in order to keep as much information as possible I would like to know the exact rule that Eclipse use to detect these lines, but Eclipse 3.6.2 is rather big so it is a herculean task.

The question is then, what are the rules in play here, and where are they defined?


Edit: The logback pattern layout snippet is %msg \(%file:%line\)%n

like image 719
Thorbjørn Ravn Andersen Avatar asked Mar 08 '11 13:03

Thorbjørn Ravn Andersen


3 Answers

The question is then, what are the rules in play here, and where are they defined?

The actual regex can be found here: https://github.com/eclipse/eclipse.jdt.debug/blob/e7932c6319b3a96526134940ca57de0576e9607a/org.eclipse.jdt.debug.ui/plugin.xml#L3371

It boils down to something like

\(\w*\.java:\S*\)

i.e. only the parenthesized part is matched. The console then delegates the match to org.eclipse.jdt.debug.ui.JavaConsoleTracker which is an implementation for IPatternMatchListenerDelegate. This creates the respective IHyperlink (JavaStackTraceHyperlink). Only on clicking on the link the actual parsing is done, the whole line is read again to fetch the full package name etc. which is then searched for in the workspace.


In case anybody like me needs to implement this behaviour outside of the Console (e.g. with StyledText):

Match links with some regex, add respective StyleRange (cf. SWT.UNDERLINE_LINK) with the data property set to the link data. On click, search the actual file in the workspace by package/class with:

SearchEngine search = new SearchEngine();
NullProgressMonitor monitor = new NullProgressMonitor();
TypeNameMatchRequestor collector = result -> {
    IPath path = result.getType().getPath();
    //handle result, e.g. open editor
};
search.searchAllTypeNames(fullPackageName, SearchPattern.R_EXACT_MATCH, className, IJavaSearchConstants.TYPE, IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
like image 136
user1666456 Avatar answered Sep 27 '22 20:09

user1666456


This snippet may help. It can be placed anywhere in your code and will print a "clickable" line on the eclipse console:

StackTraceElement s = Thread.currentThread().getStackTrace()[1];
System.out.printf("%s.%s(%s:%s)%n", s.getClassName(), s.getMethodName(),
            s.getFileName(), s.getLineNumber());

Update:

This question has an answer, that may include a solution for your problem:

Eclipse console: detect warning and error patterns and make them clickable


Here we go: we have to contribute an implementation of org.eclipse.ui.console.IPatternMatchListenerDelegate through extension point org.eclipse.ui.console.consolePatternMatchListeners.

The contributions that provide hyperlinks for exceptions and line numbers in stack traces are defined in the org.eclipse.jdt.debug.ui plugin, the implementing classes are in the same bundle.

The rules are regular expressions and can be found in the plugin.xml of the contributing plugin.

like image 24
Andreas Dolk Avatar answered Sep 27 '22 22:09

Andreas Dolk


If you print (filename:lineNumber), Eclipse will convert it to a link.

Example:

System.out.println("message (Hello.java:2)");

I don't know if there are other rules or where they are defined.

like image 37
dogbane Avatar answered Sep 27 '22 22:09

dogbane