mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-05-30 07:09:41 +08:00
GP-507: Debug agents display log messages.
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
##VERSION: 2.0
|
##VERSION: 2.0
|
||||||
Module.manifest||GHIDRA||||END|
|
Module.manifest||GHIDRA||||END|
|
||||||
data/ExtensionPoint.manifest||GHIDRA||||END|
|
data/ExtensionPoint.manifest||GHIDRA||||END|
|
||||||
|
src/main/resources/agent.log4j.xml||GHIDRA||||END|
|
||||||
|
src/main/resources/log4j-appender-console.xml||GHIDRA||||END|
|
||||||
|
src/main/resources/log4j-appender-logpanel.xml||GHIDRA||||END|
|
||||||
src/test/resources/ghidra/dbg/model/test_schema.xml||GHIDRA||||END|
|
src/test/resources/ghidra/dbg/model/test_schema.xml||GHIDRA||||END|
|
||||||
|
|||||||
@@ -23,27 +23,18 @@ import java.net.SocketAddress;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.core.LogEvent;
|
import org.apache.logging.log4j.core.Appender;
|
||||||
import org.apache.logging.log4j.core.LoggerContext;
|
import org.apache.logging.log4j.core.LoggerContext;
|
||||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
import org.apache.logging.log4j.core.config.Configuration;
|
||||||
import org.apache.logging.log4j.core.config.Property;
|
|
||||||
|
|
||||||
public class AgentWindow extends JFrame implements WindowListener {
|
import ghidra.framework.LoggingInitialization;
|
||||||
public static final int MAX_LOG_CHARS = 10000;
|
import ghidra.util.Msg;
|
||||||
|
import ghidra.util.Swing;
|
||||||
|
import log.LogListener;
|
||||||
|
import log.LogPanelAppender;
|
||||||
|
|
||||||
protected class WindowAppender extends AbstractAppender {
|
public class AgentWindow extends JFrame implements WindowListener, LogListener {
|
||||||
protected WindowAppender() {
|
public static final int MAX_LOG_CHARS = 100000;
|
||||||
super("agentAppender", null, null, true, Property.EMPTY_ARRAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void append(LogEvent event) {
|
|
||||||
String allText = logArea.getText() + "\n" + event.getMessage().getFormattedMessage();
|
|
||||||
logArea.setText(
|
|
||||||
allText.substring(Math.max(0, allText.length() - MAX_LOG_CHARS), allText.length()));
|
|
||||||
// TODO: Scroll to bottom
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final JTextArea logArea = new JTextArea();
|
protected final JTextArea logArea = new JTextArea();
|
||||||
protected final JScrollPane logScroll = new JScrollPane(logArea);
|
protected final JScrollPane logScroll = new JScrollPane(logArea);
|
||||||
@@ -55,15 +46,38 @@ public class AgentWindow extends JFrame implements WindowListener {
|
|||||||
add(new JLabel("<html>This agent is listening at <b>" + localAddress +
|
add(new JLabel("<html>This agent is listening at <b>" + localAddress +
|
||||||
"</b>. Close this window to terminate it.</html>"), BorderLayout.NORTH);
|
"</b>. Close this window to terminate it.</html>"), BorderLayout.NORTH);
|
||||||
logArea.setEditable(false);
|
logArea.setEditable(false);
|
||||||
logArea.setFont(Font.getFont(Font.MONOSPACED));
|
logArea.setFont(Font.decode(Font.MONOSPACED));
|
||||||
logArea.setAutoscrolls(true);
|
logArea.setAutoscrolls(true);
|
||||||
logScroll.setAutoscrolls(true);
|
logScroll.setAutoscrolls(true);
|
||||||
add(logScroll);
|
add(logScroll);
|
||||||
setMinimumSize(new Dimension(400, 300));
|
setMinimumSize(new Dimension(400, 300));
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
|
System.setProperty("log4j.configuration", "agent.log4j.xml");
|
||||||
|
LoggingInitialization.initializeLoggingSystem();
|
||||||
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
||||||
ctx.getConfiguration().addAppender(new WindowAppender());
|
Configuration config = ctx.getConfiguration();
|
||||||
|
Appender appender = config.getAppender("logPanel");
|
||||||
|
if (!(appender instanceof LogPanelAppender)) {
|
||||||
|
Msg.error(this, "Couldn't find LogPanelAppender instance in the Log4j context. " +
|
||||||
|
"Nothing will be logged to the agent's window.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LogPanelAppender logPanelAppender = (LogPanelAppender) appender;
|
||||||
|
logPanelAppender.setLogListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messageLogged(String message, boolean isError) {
|
||||||
|
String fMessage = isError ? "<font color=\"red\">" + message + "</font>" : message;
|
||||||
|
Swing.runIfSwingOrRunLater(() -> {
|
||||||
|
String allText = logArea.getText() + fMessage + "\n";
|
||||||
|
logArea.setText(
|
||||||
|
allText.substring(Math.max(0, allText.length() - MAX_LOG_CHARS), allText.length()));
|
||||||
|
JScrollBar vScroll = logScroll.getVerticalScrollBar();
|
||||||
|
vScroll.setValue(vScroll.getMaximum());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Basic Log4j configuration.
|
||||||
|
|
||||||
|
Note the 'packages' attribute; this is required if an appender needs to reference a class
|
||||||
|
in the codebase. For this configuration it's necessary for the 'logPanel' appender, which
|
||||||
|
references LogPanelAppender.java.
|
||||||
|
-->
|
||||||
|
<Configuration monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude" packages="log">
|
||||||
|
<Appenders>
|
||||||
|
<xi:include href="log4j-appender-console.xml"/>
|
||||||
|
<xi:include href="log4j-appender-logpanel.xml"/>
|
||||||
|
</Appenders>
|
||||||
|
|
||||||
|
<Loggers>
|
||||||
|
<Logger name="db" level="DEBUG"/>
|
||||||
|
<Logger name="docking" level="DEBUG"/>
|
||||||
|
<Logger name="ghidra" level="DEBUG"/>
|
||||||
|
<Logger name="reverse" level="DEBUG"/>
|
||||||
|
<Logger name="util" level="DEBUG"/>
|
||||||
|
|
||||||
|
<Root level="ALL">
|
||||||
|
<AppenderRef ref="console" level="DEBUG"/>
|
||||||
|
<AppenderRef ref="logPanel" level="DEBUG"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- Log4j appender that writes messages to the console. -->
|
||||||
|
<Console
|
||||||
|
name="console"
|
||||||
|
target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%-5p %m (%c{1}) %ex %n"/>
|
||||||
|
</Console>
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- Log4j appender that writes messages to the {@link LogPanelAppender}. -->
|
||||||
|
<LogPanelAppender name="logPanel">
|
||||||
|
<PatternLayout pattern="%-5p %m %ex"/>
|
||||||
|
</LogPanelAppender>
|
||||||
|
|
||||||
Reference in New Issue
Block a user