如何以编程方式限制 Eclipse (Message)Console 的缓冲区大小?
How to programmatically limit buffer size for Eclipse (Message)Console?
我开发了一个 Eclipse 插件,它提供了一个包含多个 org.eclipse.ui.console.MessageConsole
实例的监控视图,另请参阅
https://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
为了避免内存问题,我想为控制台缓冲区设置一个最大大小,类似于 "normal console view" 的 Eclipse 设置:
Limit console output, Console buffer size (characters)
如果超出限制,我希望我的控制台的内容会滚动,以便清除最旧的行并在控制台中显示最新的行。
=>如何设置缓冲区大小的限制? MessageConsole
好像没有提供方法setBufferSize(80000)
左右。它只提供了方法clearConsole()
,并没有提供判断当前书写文本大小的方法。
是否有提供所需功能的 IOConsole 的另一种实现?我在包 org.eclipse.ui.console
中找不到类似 RollingMessageConsole
的东西:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Fconsole%2Fpackage-summary.html
在哪里可以找到 Eclipse 中用于限制控制台输出的代码?
下面是我的自定义 Log4J appender 的当前状态,它将消息写入我的 MessageConsoles
。
package org.treez.core.console;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.treez.core.atom.uisynchronizing.AbstractUiSynchronizingAtom;
import org.treez.core.monitor.TreezMonitor;
/**
* For writing to the eclipse console
*/
public class TreezConsoleAppender extends AppenderSkeleton {
private static final String CONSOLE_NAME = "TreezConsole";
private static MessageConsole treezConsole = null;
@Override
protected void append(LoggingEvent event) {
//get formatted message
Layout layout = this.getLayout();
String message = layout.format(event);
String treezMonitorId = event.getNDC();
MessageConsole console = getConsole(treezMonitorId);
if (console != null) {
AbstractUiSynchronizingAtom.runUiTaskNonBlocking(() -> {
Level level = event.getLevel();
try (
MessageConsoleStream stream = console.newMessageStream();) {
if (level.equals(Level.WARN)) {
stream.setColor(TreezMonitor.ORANGE);
} else if (level.equals(Level.ERROR)) {
stream.setColor(TreezMonitor.RED);
}
stream.println(message);
} catch (IOException exception) {
exception.printStackTrace();
}
ThrowableInformation throwableInformation = event.getThrowableInformation();
if (throwableInformation != null) {
Throwable throwable = throwableInformation.getThrowable();
try (
MessageConsoleStream stream = console.newMessageStream();) {
if (level.equals(Level.WARN)) {
stream.setColor(TreezMonitor.ORANGE);
} else if (level.equals(Level.ERROR)) {
stream.setColor(TreezMonitor.RED);
}
throwable.printStackTrace(new PrintStream(stream));
} catch (IOException exception) {
exception.printStackTrace();
}
}
});
}
}
@Override
public void close() {
//not used here
}
@Override
public boolean requiresLayout() {
return true;
}
/**
* If a non-null jobId is specified: returns the console for the given jobId or null if no corresponding console has
* been registered for the TreezMonitors. If the given jobId is null, the (single) TreezConsole is returned.
*/
private static MessageConsole getConsole(String treezMonitorId) {
if (treezMonitorId == null) {
if (treezConsole == null) {
createTreezConsole();
}
return treezConsole;
} else {
return TreezMonitor.getConsole(treezMonitorId);
}
}
/**
* Creates the console
*/
private static void createTreezConsole() {
IConsoleManager consoleManager = getConsoleManager();
if (consoleManager != null) {
IConsole[] existingConsoles = consoleManager.getConsoles();
//check if console already exists and save it if so
for (IConsole currentConsole : existingConsoles) {
String currentConsoleName = currentConsole.getName();
boolean isWantedConsole = CONSOLE_NAME.equals(currentConsoleName);
if (isWantedConsole) {
treezConsole = (MessageConsole) currentConsole;
return;
}
}
//console does not already exist: create new one
treezConsole = new MessageConsole(CONSOLE_NAME, null);
consoleManager.addConsoles(new IConsole[] { treezConsole });
}
}
/**
* Gets the eclipse console manager
*/
private static IConsoleManager getConsoleManager() {
ConsolePlugin plugin = ConsolePlugin.getDefault();
if (plugin != null) {
IConsoleManager consoleManager = plugin.getConsoleManager();
return consoleManager;
} else {
return null;
}
}
}
使用
设置限制
public void setWaterMarks(int low, int high)
IOConsole
的方法(MessageConsole
扩展)。
JavaDoc 说
Sets the text buffer size for this console. The high water mark
indicates the maximum number of characters stored in the buffer. The
low water mark indicates the number of characters remaining in the
buffer when the high water mark is exceeded.
我开发了一个 Eclipse 插件,它提供了一个包含多个 org.eclipse.ui.console.MessageConsole
实例的监控视图,另请参阅
https://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
为了避免内存问题,我想为控制台缓冲区设置一个最大大小,类似于 "normal console view" 的 Eclipse 设置:
Limit console output, Console buffer size (characters)
如果超出限制,我希望我的控制台的内容会滚动,以便清除最旧的行并在控制台中显示最新的行。
=>如何设置缓冲区大小的限制? MessageConsole
好像没有提供方法setBufferSize(80000)
左右。它只提供了方法clearConsole()
,并没有提供判断当前书写文本大小的方法。
是否有提供所需功能的 IOConsole 的另一种实现?我在包 org.eclipse.ui.console
中找不到类似 RollingMessageConsole
的东西:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Fconsole%2Fpackage-summary.html
在哪里可以找到 Eclipse 中用于限制控制台输出的代码?
下面是我的自定义 Log4J appender 的当前状态,它将消息写入我的 MessageConsoles
。
package org.treez.core.console;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.treez.core.atom.uisynchronizing.AbstractUiSynchronizingAtom;
import org.treez.core.monitor.TreezMonitor;
/**
* For writing to the eclipse console
*/
public class TreezConsoleAppender extends AppenderSkeleton {
private static final String CONSOLE_NAME = "TreezConsole";
private static MessageConsole treezConsole = null;
@Override
protected void append(LoggingEvent event) {
//get formatted message
Layout layout = this.getLayout();
String message = layout.format(event);
String treezMonitorId = event.getNDC();
MessageConsole console = getConsole(treezMonitorId);
if (console != null) {
AbstractUiSynchronizingAtom.runUiTaskNonBlocking(() -> {
Level level = event.getLevel();
try (
MessageConsoleStream stream = console.newMessageStream();) {
if (level.equals(Level.WARN)) {
stream.setColor(TreezMonitor.ORANGE);
} else if (level.equals(Level.ERROR)) {
stream.setColor(TreezMonitor.RED);
}
stream.println(message);
} catch (IOException exception) {
exception.printStackTrace();
}
ThrowableInformation throwableInformation = event.getThrowableInformation();
if (throwableInformation != null) {
Throwable throwable = throwableInformation.getThrowable();
try (
MessageConsoleStream stream = console.newMessageStream();) {
if (level.equals(Level.WARN)) {
stream.setColor(TreezMonitor.ORANGE);
} else if (level.equals(Level.ERROR)) {
stream.setColor(TreezMonitor.RED);
}
throwable.printStackTrace(new PrintStream(stream));
} catch (IOException exception) {
exception.printStackTrace();
}
}
});
}
}
@Override
public void close() {
//not used here
}
@Override
public boolean requiresLayout() {
return true;
}
/**
* If a non-null jobId is specified: returns the console for the given jobId or null if no corresponding console has
* been registered for the TreezMonitors. If the given jobId is null, the (single) TreezConsole is returned.
*/
private static MessageConsole getConsole(String treezMonitorId) {
if (treezMonitorId == null) {
if (treezConsole == null) {
createTreezConsole();
}
return treezConsole;
} else {
return TreezMonitor.getConsole(treezMonitorId);
}
}
/**
* Creates the console
*/
private static void createTreezConsole() {
IConsoleManager consoleManager = getConsoleManager();
if (consoleManager != null) {
IConsole[] existingConsoles = consoleManager.getConsoles();
//check if console already exists and save it if so
for (IConsole currentConsole : existingConsoles) {
String currentConsoleName = currentConsole.getName();
boolean isWantedConsole = CONSOLE_NAME.equals(currentConsoleName);
if (isWantedConsole) {
treezConsole = (MessageConsole) currentConsole;
return;
}
}
//console does not already exist: create new one
treezConsole = new MessageConsole(CONSOLE_NAME, null);
consoleManager.addConsoles(new IConsole[] { treezConsole });
}
}
/**
* Gets the eclipse console manager
*/
private static IConsoleManager getConsoleManager() {
ConsolePlugin plugin = ConsolePlugin.getDefault();
if (plugin != null) {
IConsoleManager consoleManager = plugin.getConsoleManager();
return consoleManager;
} else {
return null;
}
}
}
使用
设置限制public void setWaterMarks(int low, int high)
IOConsole
的方法(MessageConsole
扩展)。
JavaDoc 说
Sets the text buffer size for this console. The high water mark indicates the maximum number of characters stored in the buffer. The low water mark indicates the number of characters remaining in the buffer when the high water mark is exceeded.