gpt4 book ai didi

java - RCP 中的 Eclipse 控制台缺少输出

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:31 26 4
gpt4 key购买 nike

在我的 RCP 中,我显示控制台并希望输出不同类别的输出(不仅仅是 STDERR 和 STDOUT)。我查看并使用了以下代码:

Writing to the Eclipse console

我按照规定完成了所有操作:查找或创建控制台、创建 View 、显示 View ,使用 ConsoleManager。我对其进行了设置,以便日志写入一些“类别”,每个类别对应于 IConsoleManager 中存储的不同控制台。

问题是,输出很少/不可预测地到达控制台:创建每个新控制台时,我都会在上面写一条消息。这仅在调试过程中有时会出现。所有其他控制台似乎都已创建但从未写入。除非我将所有内容写入 STDERR (!?)。

下面的代码是我所做的:

package com.Jasper.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public final class Console {
private static Console mInstance = null;

public static enum ConsoleType { DEFAULT, STDERR, STDOUT, OTHER, ERRORS }

private Console () { }

/**
* Singleton pattern to match Eclipse's console plugin's behavior.
* @return
*/
public static synchronized Console getInstance() {
if (mInstance == null)
mInstance = new Console ();

return mInstance;
}

/**
* With the given ConsoleType enum, look for an existing/open console
* and return it, or if it does not exist, create a new one and return it.
* @param name One of the members of the static enum defined in Console
* @return A non null message console
*/
private static MessageConsole findOrCreateConsole(final String name) {
final ConsolePlugin plugin = ConsolePlugin.getDefault();
final IConsoleManager conMan = plugin.getConsoleManager();
final IConsole[] existing = conMan.getConsoles();

for (final IConsole element : existing)
if (name.toString().compareTo(element.getName()) == 0)
return (MessageConsole) element;

// failed to find existing console, create one:
final MessageConsole myConsole = new MessageConsole(name.toString(), null);
conMan.addConsoles(new IConsole[] { myConsole });

try {
Write (myConsole.newMessageStream(), "Initializing: " + name + "\n");
} catch (Exception e) {
// Platform might not be up yet, silently ignore for now...
}

return myConsole;
}

/**
* Used for quick writes to consoles, this is not in place of std err/out.
* If the given console does not exist it will be created.
*
* @param name
* @param msg
*/
public static void Write (final String name, String msg) {
MessageConsole myConsole = findOrCreateConsole (name);
MessageConsoleStream out = myConsole.newMessageStream ();
out.println(msg);
}

public static void Write (MessageConsoleStream stream, String msg) {
try {
stream.write(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* Return a new output stream for a given console. If the console does
* not exist it will be created.
* @param type A console type defined in Console's ConsoleType enum.
* @return An open stream for writing to.
*/
public static OutputStream GetOutputStream(final String type) {
MessageConsole ib = findOrCreateConsole (type);
IOConsoleOutputStream mcs = ib.newOutputStream();
mcs.setActivateOnWrite(true);
return mcs;
}

/**
* Create consoles for STDERR and STDOUT, redirect all output
* to the in-application console. Should only be called once.
*
*/
public static void init () {
MessageConsole outConsole = findOrCreateConsole (ConsoleType.STDOUT.toString());
IOConsoleOutputStream outStream = outConsole.newOutputStream();
System.setOut(new PrintStream (outStream));

MessageConsole errConsole = findOrCreateConsole (ConsoleType.STDERR.toString());
IOConsoleOutputStream errStream = errConsole.newOutputStream();
System.setErr(new PrintStream (errStream));
}

/**
* Alternate way to bring up the console view. Don't know
* which one is better / the differences.
* @param myConsole Console to show, must not be null.
*/
public static void ShowConsole (IConsole myConsole) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();

IWorkbenchPage page = window.getActivePage();
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view;
try {
view = (IConsoleView) page.showView(id);
view.display(myConsole);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* Show the application console. If the given console is null, attempt
* to find an existing console and use it. If the given console is null
* and no existing consoles exist, exit without doing anything.
*
* @param myConsole An existing console.
*/
public static void DisplayConsole (IConsole myConsole) {

// try to grab any old console and display it if given null
if (myConsole == null) {
final ConsolePlugin plugin = ConsolePlugin.getDefault();
final IConsoleManager conMan = plugin.getConsoleManager();
final IConsole[] existing = conMan.getConsoles();

if (existing.length == 0)
return;

for (final IConsole element : existing)
myConsole = element;
}

ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {myConsole});
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(myConsole);
}

/**
* Show the console view with the given ConsoleType. Will not
* create one if one does not already exist.
*
* @param type Non-null enum of existing console (stdout/err probably safe)
*/
public static void DisplayConsole (final String type) {

final ConsolePlugin plugin = ConsolePlugin.getDefault();
final IConsoleManager conMan = plugin.getConsoleManager();
final IConsole[] existing = conMan.getConsoles();

for (final IConsole element : existing)
if (type.toString().compareTo(element.getName()) == 0) {
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {element});
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(element);
return;
}
}

}

有什么想法吗?

最佳答案

写入输出(stderrstdout)可能是通过回调或其他线程发生的。按如下方式写入控制台 - 它可能会有所帮助:

public void writeToConsole(String lineToConsole) {
final String lineToWrite = lineToConsole;
Display.getDefault().asyncExec(new IRunnable({

public void run() {
try {
if (!stream.isClosed())
stream.write(lineToWrite);
} catch (Exception ex) {
///if you want to - do something
}
)};
}

关于java - RCP 中的 Eclipse 控制台缺少输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1563326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com