Does exists Eclipse plugin for log files? - eclipse

I have check very long logs after each start of Tomcat (from Eclipse).
Does exist a plugin or editor that I can use inside Eclipse? At least it must have colored for errors, debug and info messages.
Thanks.

I would recommend using Log4j:
http://logging.apache.org/log4j/1.2/
http://en.wikipedia.org/wiki/Log4j
It works great with eclipse and has the color scheme built in. It's highly customizable, takes some time to figure out but it's worth it.

The code to do this is rather simple, aside from syntax coloring. Just start a plugin project, add dependancies for org.eclipse.ui.console and do something like this:
public void log() {
BufferedReader br = new BufferedReader(new FileReader("path of log file"));
String line = null;
while (br.nextLine ) {
line = br.readLine();
if (line == null) {
Thread.sleep(1000);
}
else {
MessageConsole console = findConsole("tomcat log");
MessageConsoleStream stream = console.newMessageStream();
stream.println(message);
}
}
}
private MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (IConsole element : existing)
if (name.equals(element.getName()))
return (MessageConsole) element;
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}

Related

how to set or replace a file on project explorer on gnu arm eclipse or cdt?

I'm studying eclipse cdt plug-in development use gnuarmeclipse.
previous question.
I need to set or replace a file(such a linkerscript) in project explorer.
I know it change on project properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C Linker -> General -> Script file(-T).
But I want, it execute on project explorer context menu.
See below.
1) Select LD(folder for only one linkerscript file) on Project Explorer.
2) Right click and select "Set Linker Script File" on context menu.
3) Select a file to set or replace on Open window.
This is setlinkerscript.java
public class setlinkerscript extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
// TODO Auto-generated method stub
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
Shell shell = new Shell();
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String[] {"*.x"});
String linkerscript = dialog.open();
System.out.println(linkerscript);
return null;
}}
I got a file location but I don't know where I set on eclipse.
Any API or method is there? or recommend documents.
I can't attach jpg for figure.. need more reputation point. Sorry!
Thanks in advance.
Oh finally, I did it myself. This is my answer.
Thanks stackoverflow and google.
But.. another problems coming... ㅜㅜ
public class setlinkerscript extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
// TODO Auto-generated method stub
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
Shell shell = new Shell();
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String[] {"*.x"});
String linkerscript = dialog.open(); // get new linkerscript with path
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
String activeProjectName = null;
if(editorPart != null)
{
IFileEditorInput input = (FileEditorInput)editorPart.getEditorInput();
IFile file = input.getFile();
IProject activeProject = file.getProject();
activeProjectName = activeProject.getName();
}
// ===========================================================================================================
// CProject
ICProject cproject = CoreModel.getDefault().getCModel().getCProject(activeProjectName);
IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(cproject.getResource());
// ===========================================================================================================
// config
IConfiguration configs[] = buildInfo.getManagedProject().getConfigurations();
int i;
for(i=0; i<2; i++)
{
// configs[0] : Debug
ITool[] tool = configs[i].getTools();
// configs[1] : Release
// ===========================================================================================================
// tool
// GCC Assembler, GCC C Compiler, GCC C++ Compiler, GCC C Linker,
// GCC C++ Linker, GCC Archiver, Windows Create Flash Image, Windows Create Listing,
// Windows Print Size
// tool[3] : EISC GCC C Linker
IOption[] option = tool[3].getOptions();
// option[0] : linkerscript
Object value = option[0].getValue();
try {
option[0].setValue(linkerscript);
} catch (BuildException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// ===========================================================================================================
return null;
}
}

how to read problems explorer view in eclipse programatically

is there any way to read the eclipse problem view programatically in eclipse plugin.
I want to fetch data from the following screen-
Yes: Ask the workbench for all Markers of type IMarker.PROBLEM. The documentation contains a code snippet for this:
IMarker[] problems = null;
int depth = IResource.DEPTH_INFINITE;
try {
problems = resource.findMarkers(IMarker.PROBLEM, true, depth);
} catch (CoreException e) {
// something went wrong
}
To get the workspace root, use ResourcesPlugin.getWorkspace().getRoot();
The file MarkerTypesModel.java contains this code:
private String getWellKnownLabel(String type) {
if (type.equals(IMarker.PROBLEM)) {
return "Problem";//$NON-NLS-1$
}
if (type.equals(IMarker.TASK)) {
return "Task";//$NON-NLS-1$
}
if (type.equals("org.eclipse.jdt.core.problem")) { //$NON-NLS-1$
return "Java Problem";//$NON-NLS-1$
}
return type;
}
As you can see, it compares the type with a fixed string to produce Java Problem (and the NON_NLS-Comments are wrong, too).

How to programmatically find a .java file in an Eclipse plugin from full classname?

Inside an Eclipse plugin, I'd like to open a file in editor.
I know the full package and class name
How can I determine the path of the .java file from this?
Take a look at IJavaProject.findType( name ) method. Once you have an IType, you can use getPath or getResource methods to locate the file. This method searches across a project and everything visible from that project.
To search the whole workspace, iterate through all the Java projects in the workspace, calling the findType method on each in turn.
You also need to know the source folder.
IProject prj = ResourcePlugin.getWorkspace().getRoot().getProject("project-name");
IFile theFile = prj.getFile(sourceFolder + packageName.replace('.','/') + className + ".java");
Generally you specify the file for an editor with an IFile. You can also ask an IFile for variants of the file's path.
I know this is a bit old but I had the same need and I had a look at how eclipse does it for stack trace elements (they have a hyperlink on them). The code is in org.eclipse.jdt.internal.debug.ui.console.JavaStackTraceHyperlink (the link is "lazy" so the editor to open is resolved only when you click on it).
What it does is it first searches for the type in the context of the launched application, then for in the whole workspace (method startSourceSearch) :
IType result = OpenTypeAction.findTypeInWorkspace(typeName, false);
And then opens the associated editor (method processSearchResult, source is the type retrieved above) :
protected void processSearchResult(Object source, String typeName, int lineNumber) {
IDebugModelPresentation presentation = JDIDebugUIPlugin.getDefault().getModelPresentation();
IEditorInput editorInput = presentation.getEditorInput(source);
if (editorInput != null) {
String editorId = presentation.getEditorId(editorInput, source);
if (editorId != null) {
try {
IEditorPart editorPart = JDIDebugUIPlugin.getActivePage().openEditor(editorInput, editorId);
if (editorPart instanceof ITextEditor && lineNumber >= 0) {
ITextEditor textEditor = (ITextEditor)editorPart;
IDocumentProvider provider = textEditor.getDocumentProvider();
provider.connect(editorInput);
IDocument document = provider.getDocument(editorInput);
try {
IRegion line = document.getLineInformation(lineNumber);
textEditor.selectAndReveal(line.getOffset(), line.getLength());
} catch (BadLocationException e) {
MessageDialog.openInformation(JDIDebugUIPlugin.getActiveWorkbenchShell(), ConsoleMessages.JavaStackTraceHyperlink_0, NLS.bind("{0}{1}{2}", new String[] {(lineNumber+1)+"", ConsoleMessages.JavaStackTraceHyperlink_1, typeName})); //$NON-NLS-2$ //$NON-NLS-1$
}
provider.disconnect(editorInput);
}
} catch (CoreException e) {
JDIDebugUIPlugin.statusDialog(e.getStatus());
}
}
}
}
Code has copyright from eclipse. Hopfully I'm allowed to reproduced it if this is mentionned.

Eclipe PDE: Jump to line X and highlight it

A qustion about Eclipse PDE development: I write a small plugin for Eclipse and have the following
* an org.eclipse.ui.texteditor.ITextEditor
* a line number
How can I automatically jump to that line and mark it? It's a pity that the API seems only to support offsets (see: ITextEditor.selectAndReveal()) within the document but no line numbers.
The best would be - although this doesn't work:
ITextEditor editor = (ITextEditor)IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, true );
editor.goto(line);
editor.markLine(line);
It this possible in some way? I did not find a solution
on the class DetailsView I found the following method.
private static void goToLine(IEditorPart editorPart, int lineNumber) {
if (!(editorPart instanceof ITextEditor) || lineNumber <= 0) {
return;
}
ITextEditor editor = (ITextEditor) editorPart;
IDocument document = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
if (document != null) {
IRegion lineInfo = null;
try {
// line count internaly starts with 0, and not with 1 like in
// GUI
lineInfo = document.getLineInformation(lineNumber - 1);
} catch (BadLocationException e) {
// ignored because line number may not really exist in document,
// we guess this...
}
if (lineInfo != null) {
editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
}
}
}
Even though org.eclipse.ui.texteditor.ITextEditor deals wiith offset, it should be able to take your line number with the selectAndReveal() method.
See this thread and this thread.
Try something along the line of:
((ITextEditor)org.eclipse.jdt.ui.JavaUI.openInEditor(compilationUnit)).selectAndReveal(int, int);

How can I obtain the version number of a custom Eclipse feature at runtime?

I would like to display the version number of a custom Eclipse feature I am developing in the title bar of its perspective. Is there a way to obtain the version number from the runtime plugin and/or workbench?
Something like:
Platform.getBundle("my.feature.id").getHeaders().get("Bundle-Version");
should do the trick.
Note (from this thread) that it can not be used anywhere within the plugin itself:
this.getBundle() is not valid until AFTER super.start(BundleContext) has been called on your plugin.
So if you are using this.getBundle() within your constructor or within your start(BundleContext) before calling super.start() then it will return null.
If that fails, you have here a more complete "version":
public static String getPlatformVersion() {
String version = null;
try {
Dictionary dictionary =
org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getBundle().getHeaders();
version = (String) dictionary.get("Bundle-Version"); //$NON-NLS-1$
} catch (NoClassDefFoundError e) {
version = getProductVersion();
}
return version;
}
public static String getProductVersion() {
String version = null;
try {
// this approach fails in "Rational Application Developer 6.0.1"
IProduct product = Platform.getProduct();
String aboutText = product.getProperty("aboutText"); //$NON-NLS-1$
String pattern = "Version: (.*)\n"; //$NON-NLS-1$
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(aboutText);
boolean found = m.find();
if (found) {
version = m.group(1);
}
} catch (Exception e) {
}
return version;
}
I use the first option:
protected void fillStatusLine(IStatusLineManager statusLine) {
statusItem = new StatusLineContributionItem("LastModificationDate"); //$NON-NLS-1$
statusItem.setText("Ultima Actualizaci\u00f3n: "); //$NON-NLS-1$
statusLine.add(statusItem);
Dictionary<String, String> directory = Platform.getBundle("ar.com.cse.balanza.core").getHeaders();
String version = directory.get("Bundle-Version");
statusItem = new StatusLineContributionItem("CopyRight"); //$NON-NLS-1$
statusItem.setText(Messages.AppActionBar_18);
statusLine.add(statusItem);
}
As #zvikico says above, the accepted answer does not work for Features, only Plug-ins (OSGi Bundles, which Features are not). The way to get info about installed features is via org.eclipse.core.runtime.Platform.getBundleGroupProviders() as described here.
A version of what VonC provided to retrieve the primary Eclipse version number, but one that doesn't reference internal classes (which you should avoid doing):
Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");