简体   繁体   English

RCP中的Eclipse控制台缺少输出

[英]Eclipse console in RCP missing output

In my RCP I show the Console and want to output different categories of output (not just STDERR and STDOUT). 在我的RCP中,我显示了控制台,并希望输出不同类别的输出(不仅仅是STDERR和STDOUT)。 I have looked at and used code from: 我查看并使用了以下代码:

Writing to the Eclipse console 写入Eclipse控制台

I go through all the actions as prescribed: find or create a console, create the view, display the view, using the ConsoleManager. 我按照规定执行了所有操作:使用ConsoleManager查找或创建控制台,创建视图,显示视图。 I set it up so there a handful of "categories" that the logs write to, each corresponding to a different console stored in the IConsoleManager. 我对其进行了设置,以便将日志写入其中的几个“类别”,每个类别都对应于存储在IConsoleManager中的不同控制台。

The problem is, output rarely/unpredictably makes it to the consoles: when each new Console is created, I write a message on it. 问题是,输出很少/无法预测地输出到控制台:创建每个新的控制台时,我都会在其上写一条消息。 This only appears sometimes during debugging. 有时仅在调试期间出现。 All the other consoles seem to be created but never written to. 所有其他控制台似乎都已创建但从未写入。 Unless I make everything write to STDERR (!?). 除非我将所有内容都写到STDERR(!?)。

The code below is what I have done: 下面的代码是我所做的:

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;
   }
 }

}

Any ideas? 有任何想法吗?

Writing the output ( stderr or stdout ) maybe happening through a callback or some other thread. 编写输出( stderrstdout )可能是通过回调或其他线程进行的。 Do the writing to the console as follows - it may help: 按照以下步骤对控制台进行写操作-这可能会有所帮助:

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
       }
   )};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM