From 700440d0338816ca10e85ede4aaa2e05417952eb Mon Sep 17 00:00:00 2001 From: 4pr0n Date: Wed, 11 Jun 2014 22:21:09 -0700 Subject: [PATCH] 1.0.61 - Logging is optional via configuration Closes #66 --- pom.xml | 2 +- src/main/java/com/rarchives/ripme/App.java | 4 ++- .../ripme/ripper/AbstractRipper.java | 10 +++++--- .../com/rarchives/ripme/ui/MainWindow.java | 23 +++++++++++++---- .../com/rarchives/ripme/ui/UpdateUtils.java | 2 +- .../java/com/rarchives/ripme/utils/Utils.java | 25 +++++++++++++++++++ src/main/resources/log4j.file.properties | 18 +++++++++++++ src/main/resources/log4j.properties | 10 +------- 8 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 src/main/resources/log4j.file.properties diff --git a/pom.xml b/pom.xml index 58816614..a3595b53 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.rarchives.ripme ripme jar - 1.0.60 + 1.0.61 ripme http://rip.rarchives.com diff --git a/src/main/java/com/rarchives/ripme/App.java b/src/main/java/com/rarchives/ripme/App.java index 00a56644..21781a97 100644 --- a/src/main/java/com/rarchives/ripme/App.java +++ b/src/main/java/com/rarchives/ripme/App.java @@ -24,11 +24,13 @@ import com.rarchives.ripme.utils.Utils; */ public class App { - public static final Logger logger = Logger.getLogger(App.class); + public static Logger logger; public static void main(String[] args) throws MalformedURLException { + Utils.configureLogger(); System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", "RipMe"); + logger = Logger.getLogger(App.class); logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion()); if (args.length > 0) { diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 23cd368d..0296fccc 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -69,13 +69,15 @@ public abstract class AbstractRipper } this.url = sanitizeURL(url); } - + public void setup() throws IOException { setWorkingDir(this.url); Logger rootLogger = Logger.getRootLogger(); FileAppender fa = (FileAppender) rootLogger.getAppender("FILE"); - fa.setFile(this.workingDir + File.separator + "log.txt"); - fa.activateOptions(); + if (fa != null) { + fa.setFile(this.workingDir + File.separator + "log.txt"); + fa.activateOptions(); + } this.threadPool = new DownloadThreadPool(); } @@ -83,7 +85,7 @@ public abstract class AbstractRipper public void setObserver(RipStatusHandler obs) { this.observer = obs; } - + /** * Queues image to be downloaded and saved. * @param url diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index 0282cff4..6e863ac5 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -109,6 +109,7 @@ public class MainWindow implements Runnable, RipStatusHandler { private static JCheckBox configPlaySound; private static JCheckBox configSaveOrderCheckbox; private static JCheckBox configShowPopup; + private static JCheckBox configSaveLogs; private static TrayIcon trayIcon; private static MenuItem trayMenuMain; @@ -174,6 +175,7 @@ public class MainWindow implements Runnable, RipStatusHandler { Utils.setConfigBoolean("play.sound", configPlaySound.isSelected()); Utils.setConfigBoolean("download.save_order", configSaveOrderCheckbox.isSelected()); Utils.setConfigBoolean("download.show_popup", configShowPopup.isSelected()); + Utils.setConfigBoolean("log.save", configSaveLogs.isSelected()); saveHistory(); Utils.saveConfig(); } @@ -317,12 +319,15 @@ public class MainWindow implements Runnable, RipStatusHandler { configPlaySound = new JCheckBox("Sound when rip completes", Utils.getConfigBoolean("play.sound", false)); configPlaySound.setHorizontalAlignment(JCheckBox.RIGHT); configPlaySound.setHorizontalTextPosition(JCheckBox.LEFT); - configSaveOrderCheckbox = new JCheckBox("Save images in order", Utils.getConfigBoolean("download.save_order", true)); + configSaveOrderCheckbox = new JCheckBox("Preserve order", Utils.getConfigBoolean("download.save_order", true)); configSaveOrderCheckbox.setHorizontalAlignment(JCheckBox.RIGHT); configSaveOrderCheckbox.setHorizontalTextPosition(JCheckBox.LEFT); configShowPopup = new JCheckBox("Notification when rip starts", Utils.getConfigBoolean("download.show_popup", false)); configShowPopup.setHorizontalAlignment(JCheckBox.RIGHT); configShowPopup.setHorizontalTextPosition(JCheckBox.LEFT); + configSaveLogs = new JCheckBox("Save logs", Utils.getConfigBoolean("log.save", false)); + configSaveLogs.setHorizontalAlignment(JCheckBox.RIGHT); + configSaveLogs.setHorizontalTextPosition(JCheckBox.LEFT); configSaveDirLabel = new JLabel(); try { String workingDir = (Utils.shortenPath(Utils.getWorkingDirectory())); @@ -341,10 +346,11 @@ public class MainWindow implements Runnable, RipStatusHandler { gbc.gridy = 4; gbc.gridx = 0; configurationPanel.add(configRetriesLabel, gbc); gbc.gridx = 1; configurationPanel.add(configRetriesText, gbc); gbc.gridy = 5; gbc.gridx = 0; configurationPanel.add(configOverwriteCheckbox, gbc); + gbc.gridx = 1; configurationPanel.add(configSaveOrderCheckbox, gbc); gbc.gridy = 6; gbc.gridx = 0; configurationPanel.add(configPlaySound, gbc); - gbc.gridy = 7; gbc.gridx = 0; configurationPanel.add(configSaveOrderCheckbox, gbc); - gbc.gridy = 8; gbc.gridx = 0; configurationPanel.add(configShowPopup, gbc); - gbc.gridy = 9; gbc.gridx = 0; configurationPanel.add(configSaveDirLabel, gbc); + gbc.gridx = 1; configurationPanel.add(configSaveLogs, gbc); + gbc.gridy = 7; gbc.gridx = 0; configurationPanel.add(configShowPopup, gbc); + gbc.gridy = 8; gbc.gridx = 0; configurationPanel.add(configSaveDirLabel, gbc); gbc.gridx = 1; configurationPanel.add(configSaveDirButton, gbc); gbc.gridy = 0; pane.add(ripPanel, gbc); @@ -538,6 +544,13 @@ public class MainWindow implements Runnable, RipStatusHandler { Utils.setConfigBoolean("download.save_order", configSaveOrderCheckbox.isSelected()); } }); + configSaveLogs.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + Utils.setConfigBoolean("log.save", configSaveLogs.isSelected()); + Utils.configureLogger(); + } + }); } private void setupTrayIcon() { @@ -728,7 +741,7 @@ public class MainWindow implements Runnable, RipStatusHandler { ripper.setup(); } catch (Exception e) { failed = true; - logger.error("Could not find ripper for URL " + url); + logger.error("Could not find ripper for URL " + url, e); error("Could not find ripper for given URL"); } if (!failed) { diff --git a/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java b/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java index 28ab54e1..e25bfe18 100644 --- a/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java +++ b/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java @@ -21,7 +21,7 @@ import com.rarchives.ripme.utils.Utils; public class UpdateUtils { private static final Logger logger = Logger.getLogger(UpdateUtils.class); - private static final String DEFAULT_VERSION = "1.0.60"; + private static final String DEFAULT_VERSION = "1.0.61"; private static final String updateJsonURL = "http://rarchives.com/ripme.json"; private static final String updateJarURL = "http://rarchives.com/ripme.jar"; private static final String mainFileName = "ripme.jar"; diff --git a/src/main/java/com/rarchives/ripme/utils/Utils.java b/src/main/java/com/rarchives/ripme/utils/Utils.java index 1914ff23..50a828ff 100644 --- a/src/main/java/com/rarchives/ripme/utils/Utils.java +++ b/src/main/java/com/rarchives/ripme/utils/Utils.java @@ -2,6 +2,7 @@ package com.rarchives.ripme.utils; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Constructor; import java.net.URISyntaxException; import java.net.URL; @@ -20,7 +21,9 @@ import javax.sound.sampled.LineListener; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.log4j.LogManager; import org.apache.log4j.Logger; +import org.apache.log4j.PropertyConfigurator; import com.rarchives.ripme.ripper.AbstractRipper; @@ -308,4 +311,26 @@ public class Utils { logger.error("Failed to play sound " + filename, e); } } + + /** + * Configures root logger, either for FILE output or just console. + */ + public static void configureLogger() { + LogManager.shutdown(); + String logFile; + if (getConfigBoolean("log.save", false)) { + logFile = "log4j.file.properties"; + } + else { + logFile = "log4j.properties"; + } + InputStream stream = Utils.class.getClassLoader().getResourceAsStream(logFile); + if (stream == null) { + PropertyConfigurator.configure("src/main/resources/" + logFile); + } else { + PropertyConfigurator.configure(stream); + } + logger.info("Loaded " + logFile); + } + } \ No newline at end of file diff --git a/src/main/resources/log4j.file.properties b/src/main/resources/log4j.file.properties new file mode 100644 index 00000000..26752ead --- /dev/null +++ b/src/main/resources/log4j.file.properties @@ -0,0 +1,18 @@ +# define the file appender +log4j.appender.FILE = org.apache.log4j.RollingFileAppender +log4j.appender.FILE.File = ripme.log +log4j.appender.FILE.ImmediateFlush = true +log4j.appender.FILE.Threshold = debug +log4j.appender.FILE.maxFileSize = 20MB +log4j.appender.FILE.layout = org.apache.log4j.PatternLayout +log4j.appender.FILE.layout.ConversionPattern = %d %-4r [%t] %-5p %c{2} %x - %m%n + +# define the console appender +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target = System.out +log4j.appender.stdout.Threshold = info +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern = %m%n + +# now map our console appender as a root logger, means all log messages will go to this appender +log4j.rootLogger = debug, stdout, FILE \ No newline at end of file diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 26752ead..409dd303 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -1,11 +1,3 @@ -# define the file appender -log4j.appender.FILE = org.apache.log4j.RollingFileAppender -log4j.appender.FILE.File = ripme.log -log4j.appender.FILE.ImmediateFlush = true -log4j.appender.FILE.Threshold = debug -log4j.appender.FILE.maxFileSize = 20MB -log4j.appender.FILE.layout = org.apache.log4j.PatternLayout -log4j.appender.FILE.layout.ConversionPattern = %d %-4r [%t] %-5p %c{2} %x - %m%n # define the console appender log4j.appender.stdout = org.apache.log4j.ConsoleAppender @@ -15,4 +7,4 @@ log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %m%n # now map our console appender as a root logger, means all log messages will go to this appender -log4j.rootLogger = debug, stdout, FILE \ No newline at end of file +log4j.rootLogger = debug, stdout \ No newline at end of file