Ghidra is a software reverse engineering (SRE) framework
GP-7142 - Logging - Added default log file values to prevent bad log files from getting created during testing
dragonmacher5b7879750428
diff --git a/Ghidra/Framework/Generic/certification.manifest b/Ghidra/Framework/Generic/certification.manifestindex 94dc2ae741..e010885e02 100644--- a/Ghidra/Framework/Generic/certification.manifest+++ b/Ghidra/Framework/Generic/certification.manifest@@ -21,11 +21,12 @@ src/main/java/ghidra/util/map/package.html||GHIDRA||||END|src/main/java/ghidra/util/xml/package.html||GHIDRA||||END|src/main/resources/generic.log4j.xml||GHIDRA||||END|src/main/resources/generic.log4jdev.xml||GHIDRA||||END|-src/main/resources/generic.log4jfile.xml||GHIDRA||||END|src/main/resources/generic.log4jtest.xml||GHIDRA||||END|src/main/resources/log4j-appender-console-with-links.xml||GHIDRA||||END|src/main/resources/log4j-appender-console.xml||GHIDRA||||END|src/main/resources/log4j-appender-logpanel.xml||GHIDRA||||END|+src/main/resources/log4j-appender-rolling-file-default.xml||GHIDRA||||END|+src/main/resources/log4j-appender-rolling-file-scripts-default.xml||GHIDRA||||END|src/main/resources/log4j-appender-rolling-file-scripts.xml||GHIDRA||||END|src/main/resources/log4j-appender-rolling-file.xml||GHIDRA||||END|src/main/resources/log4j-appender-test-console-with-links.xml||GHIDRA||||END|diff --git a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.javaindex 611bb4a35b..2913711479 100644--- a/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java+++ b/Ghidra/Framework/Generic/src/main/java/ghidra/framework/LoggingInitialization.java@@ -15,11 +15,16 @@*/package ghidra.framework;-import java.io.File;+import java.io.*;import java.net.*;+import java.util.List;+import org.apache.logging.log4j.Level;import org.apache.logging.log4j.LogManager;+import org.apache.logging.log4j.core.Appender;import org.apache.logging.log4j.core.LoggerContext;+import org.apache.logging.log4j.core.config.*;+import org.apache.logging.log4j.core.config.xml.XmlConfiguration;import ghidra.util.Msg;import ghidra.util.SystemUtilities;@@ -63,14 +68,54 @@ public class LoggingInitialization {// Ensure this property is set. Some code paths set the property, but some do not.System.setProperty(LOG4J2_CONFIGURATION_PROPERTY, configFileUrl.toURI().toString());- // force the log system to initialize- LogManager.getContext(false);+ // Simply requesting the context will force the log system to initialize. Make the call+ // so that it will pick up the config file property we just set.+ LoggerContext ctx = (LoggerContext) LogManager.getContext(false);++ replaceDefaultAppenders(ctx);+return configFileUrl;}catch (URISyntaxException e) {Msg.error(LoggingInitialization.class, "Unable to convert URL to URI", e);return null;}+ catch (IOException e) {+ Msg.error(LoggingInitialization.class, "Unable to load file appenders", e);+ return null;+ }+ }++ /**+ * Our logging system works with a few different config files, one of which is chosen by the+ * application configuration. Each config file will use multiple appenders to send log output+ * to various places: the system console, the UI log display, an application log file and a+ * script log file for script messages.+ * <P>+ * Each application, including unit tests, is responsible for making sure that+ * {@link #initializeLoggingSystem()} is called before clients use any logging. If this call is+ * not made, and the log4j system gets initialized indirectly, such as through class loading,+ * then log4j may create poorly named files in Java's working directory. To prevent this, we+ * have our file appenders default to using the console instead of a file. When+ * {@link #initializeLoggingSystem()} is called, this method will replace those default+ * appenders with the desired file appenders.+ *+ * @param ctx the logging context+ * @throws IOException if there is a problem reading the appender xml configuration files+ */+ private static void replaceDefaultAppenders(LoggerContext ctx) throws IOException {++ ApplicationAppenderPlaceholder applicationAppender = new ApplicationAppenderPlaceholder();+ ScriptAppenderPlaceholder scriptAppender = new ScriptAppenderPlaceholder();++ applicationAppender.install(ctx);+ scriptAppender.install(ctx);++ ctx.updateLoggers(); // Refreshes the context+ }++ private static URL getResource(String relativeName) {+ return LoggingInitialization.class.getClassLoader().getResource(relativeName);}private static URL getLoggingConfigFileUrl() {@@ -89,7 +134,7 @@ public class LoggingInitialization {loggingConfigFilename = DEVELOPMENT_LOGGING_CONFIGURATION_FILE;}- return LoggingInitialization.class.getClassLoader().getResource(loggingConfigFilename);+ return getResource(loggingConfigFilename);}private static URL getLogFileFromSystemProperty() {@@ -99,7 +144,7 @@ public class LoggingInitialization {}// first see if the given filename is something that is in our classpath- URL resource = LoggingInitialization.class.getClassLoader().getResource(configString);+ URL resource = getResource(configString);if (resource != null) {return resource;}@@ -223,4 +268,103 @@ public class LoggingInitialization {((LoggerContext) LogManager.getContext(false)).reconfigure();}}++//=================================================================================================+// Inner Classes+//=================================================================================================++ private static class AppenderPlaceholder {++ private String name;+ private String configFilename;+ private Level level;++ AppenderPlaceholder(String name, String configFilename) {+ this.name = name;+ this.configFilename = configFilename;+ }++ void install(LoggerContext ctx) throws IOException {++ if (!loadDefaultAppender(ctx)) {+ return;+ }++ Appender newAppender = createReplacementAppender(ctx);+ replaceAppender(ctx, newAppender);+ }++ private boolean loadDefaultAppender(LoggerContext ctx) {++ Configuration config = ctx.getConfiguration();+ LoggerConfig rootLoggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);+ List<AppenderRef> refs = rootLoggerConfig.getAppenderRefs();++ for (AppenderRef ref : refs) {+ String appenderName = ref.getRef();+ if (appenderName.equals(name)) {+ level = ref.getLevel();+ return true;+ }+ }++ error("Unable to find '%' default appender".formatted(name));+ return false;+ }++ private Appender createReplacementAppender(LoggerContext ctx) throws IOException {+ URL url = getResource(configFilename);+ if (url == null) {+ // Logging not initialized; can't use logging+ error("Unable to find appender config '%s'".formatted(configFilename));+ return null;+ }++ try (InputStream fis = url.openStream()) {+ ConfigurationSource source = new ConfigurationSource(fis);+ XmlConfiguration tempConfig = new XmlConfiguration(ctx, source);+ tempConfig.initialize(); // trigger the xml parsing++ Appender appender = tempConfig.getAppender(name);+ if (appender == null) {+ error("Could not find an appender named '%s' in '%s'".formatted(name,+ configFilename));+ return null;+ }+ return appender;+ }+ }++ private void replaceAppender(LoggerContext ctx, Appender newAppender) {+ if (newAppender == null) {+ return; // already printed an error messages before this call+ }++ // remove old appender+ Configuration config = ctx.getConfiguration();+ LoggerConfig rootLoggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);+ rootLoggerConfig.removeAppender(name);++ newAppender.start();+ config.addAppender(newAppender);+ config.getRootLogger().addAppender(newAppender, level, null);+ }++ private void error(String s) {+ // Logging not initialized; can't use logging+ System.err.println(s);+ }+ }++ private static class ApplicationAppenderPlaceholder extends AppenderPlaceholder {+ ApplicationAppenderPlaceholder() {+ super("detail", "log4j-appender-rolling-file.xml");+ }+ }++ private static class ScriptAppenderPlaceholder extends AppenderPlaceholder {+ ScriptAppenderPlaceholder() {+ super("script", "log4j-appender-rolling-file-scripts.xml");+ }+ }}diff --git a/Ghidra/Framework/Generic/src/main/resources/generic.log4j.xml b/Ghidra/Framework/Generic/src/main/resources/generic.log4j.xmlindex d0a5261670..2260039291 100644--- a/Ghidra/Framework/Generic/src/main/resources/generic.log4j.xml+++ b/Ghidra/Framework/Generic/src/main/resources/generic.log4j.xml@@ -10,8 +10,8 @@<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-rolling-file.xml"/>- <xi:include href="log4j-appender-rolling-file-scripts.xml"/>+ <xi:include href="log4j-appender-rolling-file-default.xml"/>+ <xi:include href="log4j-appender-rolling-file-scripts-default.xml"/><xi:include href="log4j-appender-logpanel.xml"/></Appenders>diff --git a/Ghidra/Framework/Generic/src/main/resources/generic.log4jdev.xml b/Ghidra/Framework/Generic/src/main/resources/generic.log4jdev.xmlindex 2f5b2584a1..f05ff43a52 100644--- a/Ghidra/Framework/Generic/src/main/resources/generic.log4jdev.xml+++ b/Ghidra/Framework/Generic/src/main/resources/generic.log4jdev.xml@@ -15,8 +15,8 @@<Configuration monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude" packages="log"><Appenders><xi:include href="log4j-appender-console-with-links.xml"/>- <xi:include href="log4j-appender-rolling-file.xml"/>- <xi:include href="log4j-appender-rolling-file-scripts.xml"/>+ <xi:include href="log4j-appender-rolling-file-default.xml"/>+ <xi:include href="log4j-appender-rolling-file-scripts-default.xml"/><xi:include href="log4j-appender-logpanel.xml"/></Appenders>diff --git a/Ghidra/Framework/Generic/src/main/resources/generic.log4jfile.xml b/Ghidra/Framework/Generic/src/main/resources/generic.log4jfile.xmldeleted file mode 100644index 9f45c94ff5..0000000000--- a/Ghidra/Framework/Generic/src/main/resources/generic.log4jfile.xml+++ /dev/null@@ -1,21 +0,0 @@-<?xml version="1.0" encoding="UTF-8"?>--<Configuration monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude">- <Appenders>- <xi:include href="log4j-appender-rolling-file.xml"/>- <xi:include href="log4j-appender-rolling-file-scripts.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="detail" level="DEBUG"/>- <AppenderRef ref="script" level="DEBUG"/>- </Root>- </Loggers>-</Configuration>diff --git a/Ghidra/Framework/Generic/src/main/resources/generic.log4jtest.xml b/Ghidra/Framework/Generic/src/main/resources/generic.log4jtest.xmlindex 63af59a33a..eed404bb78 100644--- a/Ghidra/Framework/Generic/src/main/resources/generic.log4jtest.xml+++ b/Ghidra/Framework/Generic/src/main/resources/generic.log4jtest.xml@@ -15,8 +15,8 @@<Configuration monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude" packages="log"><Appenders><xi:include href="log4j-appender-test-console-with-links.xml"/>- <xi:include href="log4j-appender-rolling-file.xml"/>- <xi:include href="log4j-appender-rolling-file-scripts.xml"/>+ <xi:include href="log4j-appender-rolling-file-default.xml"/>+ <xi:include href="log4j-appender-rolling-file-scripts-default.xml"/><xi:include href="log4j-appender-logpanel.xml"/></Appenders>diff --git a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-default.xml b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-default.xmlnew file mode 100644index 0000000000..2a54410386--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-default.xml@@ -0,0 +1,17 @@+<?xml version="1.0" encoding="UTF-8"?>+++<!--++ Log4j appender that sends messages to the console. This is a default configuration that is only+ used when the logging system is not correctly initialized by the application.++-->+<Console+ name="detail"+ target="SYSTEM_OUT">+ <PatternLayout pattern="%-5p %m (%c{1}) %ex %n"/>+</Console>+++diff --git a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts-default.xml b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts-default.xmlnew file mode 100644index 0000000000..7c3e591c1b--- /dev/null+++ b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts-default.xml@@ -0,0 +1,15 @@+<?xml version="1.0" encoding="UTF-8"?>++<!--++ Log4j appender that sends messages to the console. This is a default configuration that is only+ used when the logging system is not correctly initialized by the application.++-->+<Console+ name="script"+ target="SYSTEM_OUT">+ <PatternLayout pattern="%-5p %m (%c{1}) %ex %n"/>+</Console>++diff --git a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts.xml b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts.xmlindex 0660de440a..9297db2301 100644--- a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts.xml+++ b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file-scripts.xml@@ -9,21 +9,26 @@StringFormattedMessage class. We then emit the original message when writing to the file.-->-<RollingFile- name="script"- fileName="${sys:scriptLogFilename}"- filePattern="${sys:scriptLogFilename}-%d{yyyy-MM-dd}-%i.log">-- <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m %ex %n"/>-- <!--- Only allow messages from scripts (see the note above).- -->- <RegexFilter regex=".*Format:GhidraScript.*" onMatch="ACCEPT" onMismatch="DENY" useRawMsg="true" />-- <Policies>- <SizeBasedTriggeringPolicy size="50000 KB"/>- </Policies>- <DefaultRolloverStrategy max="20"/>-</RollingFile>+<Configuration>+ <Appenders>+ <RollingFile+ name="script"+ fileName="${sys:scriptLogFilename}"+ filePattern="${sys:scriptLogFilename}-%d{yyyy-MM-dd}-%i.log">++ <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m %ex %n"/>++ <!--+ Only allow messages from scripts (see the note above).+ -->+ <RegexFilter regex=".*Format:GhidraScript.*" onMatch="ACCEPT" onMismatch="DENY" useRawMsg="true" />++ <Policies>+ <SizeBasedTriggeringPolicy size="50000 KB"/>+ </Policies>+ <DefaultRolloverStrategy max="20"/>+ </RollingFile>++ </Appenders>+</Configuration>diff --git a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file.xml b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file.xmlindex f9cf8a6284..4062c3b3b2 100644--- a/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file.xml+++ b/Ghidra/Framework/Generic/src/main/resources/log4j-appender-rolling-file.xml@@ -1,13 +1,21 @@<?xml version="1.0" encoding="UTF-8"?>+<!-- Log4j appender that writes messages to a file. -->-<RollingFile- name="detail"- fileName="${sys:logFilename}"- filePattern="${sys:logFilename}-%d{yyyy-MM-dd}-%i.log">- <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p (%c{1}) %m %ex %n"/>- <Policies>- <SizeBasedTriggeringPolicy size="50000 KB"/>- </Policies>- <DefaultRolloverStrategy max="20"/>-</RollingFile>++<Configuration>+ <Appenders>+ <RollingFile+ name="detail"+ fileName="${sys:logFilename}"+ filePattern="${sys:logFilename}-%d{yyyy-MM-dd}-%i.log">+ <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p (%c{1}) %m %ex %n"/>+ <Policies>+ <SizeBasedTriggeringPolicy size="50000 KB"/>+ </Policies>+ <DefaultRolloverStrategy max="20"/>+ </RollingFile>++ </Appenders>+</Configuration>+