From e9d9709788dd89848d3a08f513781d1252cec1a1 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 11 Oct 2013 21:26:07 +0200 Subject: [PATCH] Refactoring of LoggingPanel and logger in general --- src/eva2/gui/LoggingLevelLabel.java | 13 +++-- src/eva2/gui/LoggingPanel.java | 24 +++------ src/eva2/gui/Main.java | 19 ++++--- .../problems/SimpleProblemWrapper.java | 51 +++++++++---------- 4 files changed, 51 insertions(+), 56 deletions(-) diff --git a/src/eva2/gui/LoggingLevelLabel.java b/src/eva2/gui/LoggingLevelLabel.java index b5d4e33c..58b4a81c 100644 --- a/src/eva2/gui/LoggingLevelLabel.java +++ b/src/eva2/gui/LoggingLevelLabel.java @@ -25,12 +25,11 @@ import javax.swing.JPopupMenu; public final class LoggingLevelLabel extends JLabel { private JPopupMenu menu; private String[] options; - private Logger logger; + private static final Logger LOGGER = Logger.getLogger(LoggingLevelLabel.class.getName()); - public LoggingLevelLabel(final Logger logger) { + public LoggingLevelLabel() { options = new String[]{"Info", "Warning", "Severe", "Fine", "Finer", "Finest", "All"}; - this.logger = logger; setToolTipText("Click to change current logging level"); createPopupMenu(); @@ -77,10 +76,10 @@ public final class LoggingLevelLabel extends JLabel { */ private void updateText() { /* Get the current logging Level */ - Level lvl = logger.getLevel(); + Level lvl = LOGGER.getLevel(); /* Level could be null, fetch parent level */ if (lvl == null) { - lvl = logger.getParent().getLevel(); + lvl = LOGGER.getParent().getLevel(); } /* Show the updated text */ setText("Level: " + lvl.getName()); @@ -94,7 +93,7 @@ public final class LoggingLevelLabel extends JLabel { private void setLoggerLevel(Level level) { // Recursively set logging level for all classes under eva2 Logger.getLogger("eva2").setLevel(level); - logger.log(Level.INFO, "Logging Level changed to {0}", level.getName()); + LOGGER.log(Level.INFO, "Logging Level changed to {0}", level.getName()); } /** @@ -111,7 +110,7 @@ public final class LoggingLevelLabel extends JLabel { Level level = Level.parse(levelName.toUpperCase()); LoggingLevelLabel.this.setLoggerLevel(level); } catch (IllegalArgumentException ex) { - logger.log(Level.INFO, "Could not determine new logging level!", ex); + LOGGER.log(Level.INFO, "Could not determine new logging level!", ex); } LoggingLevelLabel.this.updateText(); diff --git a/src/eva2/gui/LoggingPanel.java b/src/eva2/gui/LoggingPanel.java index b966f15d..16f624b0 100644 --- a/src/eva2/gui/LoggingPanel.java +++ b/src/eva2/gui/LoggingPanel.java @@ -1,14 +1,4 @@ package eva2.gui; -/* - * Title: EvA2 - * Description: - * Copyright: Copyright (c) 2003 - * Company: University of Tuebingen, Computer Architecture - * @author Holger Ulmer, Felix Streichert, Hannes Planatscher - * @version: $Revision: 191 $ - * $Date: 2007-10-23 12:56:51 +0200 (Tue, 23 Oct 2007) $ - * $Author: mkron $ - */ import java.awt.BorderLayout; import java.awt.Color; @@ -24,20 +14,17 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /** - * + * Simple logging panel that shows logs produced by EvA2 */ public class LoggingPanel extends JPanel { - protected static Logger logger; + protected static Logger LOGGER = Logger.getLogger(LoggingPanel.class.getName()); protected JTextArea loggingTextArea = new JTextArea(10, 20); - protected boolean firstMessage = true; protected Handler loggingHandler; - protected JPopupMenu loggingLevelMenu; /** * */ - public LoggingPanel(Logger logger) { - this.logger = logger; + public LoggingPanel() { loggingTextArea.setEditable(false); loggingTextArea.setLineWrap(true); loggingTextArea.setBorder(BorderFactory.createEmptyBorder()); @@ -47,7 +34,10 @@ public class LoggingPanel extends JPanel { add(new JLabel("Info"), BorderLayout.PAGE_START); this.loggingHandler = new LoggingHandler(this); - logger.addHandler(loggingHandler); + + // Create default logger at namespace root eva2 + Logger rootLogger = Logger.getLogger("eva2"); + rootLogger.addHandler(loggingHandler); final JScrollPane scrollpane = new JScrollPane(loggingTextArea); scrollpane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); diff --git a/src/eva2/gui/Main.java b/src/eva2/gui/Main.java index 30785e77..979a171a 100644 --- a/src/eva2/gui/Main.java +++ b/src/eva2/gui/Main.java @@ -24,6 +24,7 @@ import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.*; +import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; @@ -371,7 +372,9 @@ public class Main extends JFrame implements OptimizationStateListener { System.out.println("Error" + e.getMessage()); } - LoggingPanel logPanel = new LoggingPanel(LOGGER); + + + LoggingPanel logPanel = new LoggingPanel(); logPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); @@ -418,7 +421,7 @@ public class Main extends JFrame implements OptimizationStateListener { statusBarControls.add(Box.createHorizontalGlue()); /* Logging settings drop down */ - LoggingLevelLabel loggingOption = new LoggingLevelLabel(LOGGER); + LoggingLevelLabel loggingOption = new LoggingLevelLabel(); statusBarControls.add(loggingOption); @@ -565,14 +568,19 @@ public class Main extends JFrame implements OptimizationStateListener { if (unknownArgs.length > 0) { System.err.println("Unrecognized command line options: "); - for (int i = 0; i < unknownArgs.length; i++) { - System.err.println(" " + args[unknownArgs[i]]); + for (Integer unknownArg : unknownArgs) { + System.err.println(" " + args[unknownArg]); } if (values[0] == null) { System.err.println("Try --help as argument."); } } + // Set up logging + Logger rootLogger = Logger.getLogger("eva2"); + rootLogger.setLevel(Level.INFO); + rootLogger.setUseParentHandlers(false); + if (values[0] != null) { System.out.println(usage()); } else { @@ -580,10 +588,9 @@ public class Main extends JFrame implements OptimizationStateListener { boolean nosplash = (values[2] != null); boolean nogui = (values[3] != null); boolean treeView = (values[6] != null); - String hostName = StringTools.checkSingleStringArg(keys[4], values[4], arities[4] - 1); String paramsFile = StringTools.checkSingleStringArg(keys[5], values[5], arities[5] - 1); - new Main(hostName, paramsFile, autorun, nosplash, nogui, treeView); + new Main("", paramsFile, autorun, nosplash, nogui, treeView); } } diff --git a/src/eva2/optimization/problems/SimpleProblemWrapper.java b/src/eva2/optimization/problems/SimpleProblemWrapper.java index 9793ded3..f6407600 100644 --- a/src/eva2/optimization/problems/SimpleProblemWrapper.java +++ b/src/eva2/optimization/problems/SimpleProblemWrapper.java @@ -21,24 +21,24 @@ import simpleprobs.SimpleProblemDouble; public class SimpleProblemWrapper extends AbstractOptimizationProblem { InterfaceSimpleProblem simProb = new SimpleF1(); - protected double m_DefaultRange = 10; - protected double m_Noise = 0; + protected double defaultRange = 10; + protected double noise = 0; private int repaintMinWait = 20; private int repaintCnt = 0; - transient Plot m_plot = null; + transient Plot plot = null; transient AbstractEAIndividual bestIndy = null; String plotFunc = "plotBest"; transient Class[] plotFuncSig = new Class[]{Plot.class, AbstractEAIndividual.class}; transient private boolean resetTemplate = true; public SimpleProblemWrapper() { - m_plot = null; + plot = null; initTemplate(); } public SimpleProblemWrapper(SimpleProblemWrapper other) { - other.m_DefaultRange = m_DefaultRange; - other.m_Noise = m_Noise; + other.defaultRange = defaultRange; + other.noise = noise; // warning! this does no deep copy! setSimpleProblem(other.simProb); } @@ -59,8 +59,8 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { // evaluate the vector fitness = ((SimpleProblemDouble) simProb).eval(x); // if indicated, add Gaussian noise - if (m_Noise != 0) { - RNG.addNoise(fitness, m_Noise); + if (noise != 0) { + RNG.addNoise(fitness, noise); } // set the fitness individual.setFitness(fitness); @@ -80,7 +80,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { @Override public void evaluatePopulationStart(Population population) { - if (m_plot != null && (!m_plot.isValid())) { + if (plot != null && (!plot.isValid())) { openPlot(); } } @@ -89,15 +89,14 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { public void evaluatePopulationEnd(Population population) { super.evaluatePopulationEnd(population); repaintCnt += population.size(); - if (m_plot != null) { + if (plot != null) { if (repaintCnt >= repaintMinWait) { // dont repaint always for small pops if ((bestIndy == null) || (population.getBestEAIndividual().isDominant(bestIndy.getFitness()))) { // only paint improvement bestIndy = population.getBestEAIndividual(); Object[] args = new Object[2]; - args[0] = m_plot; + args[0] = plot; args[1] = bestIndy; -// System.out.println(population.getBestEAIndividual().getStringRepresentation()); BeanInspector.callIfAvailable(simProb, plotFunc, args); } repaintCnt = 0; @@ -147,11 +146,11 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { } protected double getRangeLowerBound(int dim) { - return -m_DefaultRange; + return -defaultRange; } protected double getRangeUpperBound(int dim) { - return m_DefaultRange; + return defaultRange; } /** @@ -162,7 +161,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { } private void openPlot() { - m_plot = new Plot("SimpleProblemWrapper", "x", "y", true); + plot = new Plot("SimpleProblemWrapper", "x", "y", true); } /** @@ -174,19 +173,19 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { GenericObjectEditor.setShowProperty(getClass(), "noise", (simProb instanceof SimpleProblemDouble)); GenericObjectEditor.setShowProperty(getClass(), "defaultRange", (simProb instanceof SimpleProblemDouble)); if (BeanInspector.hasMethod(simProb, plotFunc, plotFuncSig) != null) { - if (m_plot == null) { + if (plot == null) { openPlot(); } else { - if (!m_plot.isValid()) { - m_plot.dispose(); + if (!plot.isValid()) { + plot.dispose(); openPlot(); } else { - m_plot.clearAll(); + plot.clearAll(); } } - } else if (m_plot != null) { - m_plot.dispose(); - m_plot = null; + } else if (plot != null) { + plot.dispose(); + plot = null; } } @@ -207,11 +206,11 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { if (noise < 0) { noise = 0; } - this.m_Noise = noise; + this.noise = noise; } public double getNoise() { - return this.m_Noise; + return this.noise; } public String noiseTipText() { @@ -225,7 +224,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { * @return value of the absolute range limit */ public double getDefaultRange() { - return m_DefaultRange; + return defaultRange; } /** @@ -234,7 +233,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem { * @param defaultRange */ public void setDefaultRange(double defaultRange) { - this.m_DefaultRange = defaultRange; + this.defaultRange = defaultRange; initTemplate(); }