gpt4 book ai didi

java - 如何以编程方式限制 Eclipse (Message)Console 的缓冲区大小?

转载 作者:行者123 更新时间:2023-12-02 11:36:59 24 4
gpt4 key购买 nike

我开发了一个 Eclipse 插件,它提供了一个监视 View ,其中包含 org.eclipse.ui.console.MessageConsole 的多个实例,另请参阅

https://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F

为了避免内存问题,我想设置控制台缓冲区的最大大小,类似于 Eclipse 对“普通控制台 View ”的设置:

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 附加程序的当前状态,它将消息写入我的 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.

关于java - 如何以编程方式限制 Eclipse (Message)Console 的缓冲区大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869004/

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