Refactoring of LoggingPanel and logger in general

This commit is contained in:
Fabian Becker 2013-10-11 21:26:07 +02:00
parent 2e9954e99b
commit e9d9709788
4 changed files with 51 additions and 56 deletions

View File

@ -25,12 +25,11 @@ import javax.swing.JPopupMenu;
public final class LoggingLevelLabel extends JLabel { public final class LoggingLevelLabel extends JLabel {
private JPopupMenu menu; private JPopupMenu menu;
private String[] options; 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"}; options = new String[]{"Info", "Warning", "Severe", "Fine", "Finer", "Finest", "All"};
this.logger = logger;
setToolTipText("Click to change current logging level"); setToolTipText("Click to change current logging level");
createPopupMenu(); createPopupMenu();
@ -77,10 +76,10 @@ public final class LoggingLevelLabel extends JLabel {
*/ */
private void updateText() { private void updateText() {
/* Get the current logging Level */ /* Get the current logging Level */
Level lvl = logger.getLevel(); Level lvl = LOGGER.getLevel();
/* Level could be null, fetch parent level */ /* Level could be null, fetch parent level */
if (lvl == null) { if (lvl == null) {
lvl = logger.getParent().getLevel(); lvl = LOGGER.getParent().getLevel();
} }
/* Show the updated text */ /* Show the updated text */
setText("<html><b>Level</b>: " + lvl.getName()); setText("<html><b>Level</b>: " + lvl.getName());
@ -94,7 +93,7 @@ public final class LoggingLevelLabel extends JLabel {
private void setLoggerLevel(Level level) { private void setLoggerLevel(Level level) {
// Recursively set logging level for all classes under eva2 // Recursively set logging level for all classes under eva2
Logger.getLogger("eva2").setLevel(level); 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()); Level level = Level.parse(levelName.toUpperCase());
LoggingLevelLabel.this.setLoggerLevel(level); LoggingLevelLabel.this.setLoggerLevel(level);
} catch (IllegalArgumentException ex) { } 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(); LoggingLevelLabel.this.updateText();

View File

@ -1,14 +1,4 @@
package eva2.gui; 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.BorderLayout;
import java.awt.Color; import java.awt.Color;
@ -24,20 +14,17 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
/** /**
* * Simple logging panel that shows logs produced by EvA2
*/ */
public class LoggingPanel extends JPanel { 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 JTextArea loggingTextArea = new JTextArea(10, 20);
protected boolean firstMessage = true;
protected Handler loggingHandler; protected Handler loggingHandler;
protected JPopupMenu loggingLevelMenu;
/** /**
* *
*/ */
public LoggingPanel(Logger logger) { public LoggingPanel() {
this.logger = logger;
loggingTextArea.setEditable(false); loggingTextArea.setEditable(false);
loggingTextArea.setLineWrap(true); loggingTextArea.setLineWrap(true);
loggingTextArea.setBorder(BorderFactory.createEmptyBorder()); loggingTextArea.setBorder(BorderFactory.createEmptyBorder());
@ -47,7 +34,10 @@ public class LoggingPanel extends JPanel {
add(new JLabel("Info"), BorderLayout.PAGE_START); add(new JLabel("Info"), BorderLayout.PAGE_START);
this.loggingHandler = new LoggingHandler(this); 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); final JScrollPane scrollpane = new JScrollPane(loggingTextArea);
scrollpane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); scrollpane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

View File

@ -24,6 +24,7 @@ import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -371,7 +372,9 @@ public class Main extends JFrame implements OptimizationStateListener {
System.out.println("Error" + e.getMessage()); System.out.println("Error" + e.getMessage());
} }
LoggingPanel logPanel = new LoggingPanel(LOGGER);
LoggingPanel logPanel = new LoggingPanel();
logPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); logPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
@ -418,7 +421,7 @@ public class Main extends JFrame implements OptimizationStateListener {
statusBarControls.add(Box.createHorizontalGlue()); statusBarControls.add(Box.createHorizontalGlue());
/* Logging settings drop down */ /* Logging settings drop down */
LoggingLevelLabel loggingOption = new LoggingLevelLabel(LOGGER); LoggingLevelLabel loggingOption = new LoggingLevelLabel();
statusBarControls.add(loggingOption); statusBarControls.add(loggingOption);
@ -565,14 +568,19 @@ public class Main extends JFrame implements OptimizationStateListener {
if (unknownArgs.length > 0) { if (unknownArgs.length > 0) {
System.err.println("Unrecognized command line options: "); System.err.println("Unrecognized command line options: ");
for (int i = 0; i < unknownArgs.length; i++) { for (Integer unknownArg : unknownArgs) {
System.err.println(" " + args[unknownArgs[i]]); System.err.println(" " + args[unknownArg]);
} }
if (values[0] == null) { if (values[0] == null) {
System.err.println("Try --help as argument."); 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) { if (values[0] != null) {
System.out.println(usage()); System.out.println(usage());
} else { } else {
@ -580,10 +588,9 @@ public class Main extends JFrame implements OptimizationStateListener {
boolean nosplash = (values[2] != null); boolean nosplash = (values[2] != null);
boolean nogui = (values[3] != null); boolean nogui = (values[3] != null);
boolean treeView = (values[6] != 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); 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);
} }
} }

View File

@ -21,24 +21,24 @@ import simpleprobs.SimpleProblemDouble;
public class SimpleProblemWrapper extends AbstractOptimizationProblem { public class SimpleProblemWrapper extends AbstractOptimizationProblem {
InterfaceSimpleProblem<?> simProb = new SimpleF1(); InterfaceSimpleProblem<?> simProb = new SimpleF1();
protected double m_DefaultRange = 10; protected double defaultRange = 10;
protected double m_Noise = 0; protected double noise = 0;
private int repaintMinWait = 20; private int repaintMinWait = 20;
private int repaintCnt = 0; private int repaintCnt = 0;
transient Plot m_plot = null; transient Plot plot = null;
transient AbstractEAIndividual bestIndy = null; transient AbstractEAIndividual bestIndy = null;
String plotFunc = "plotBest"; String plotFunc = "plotBest";
transient Class[] plotFuncSig = new Class[]{Plot.class, AbstractEAIndividual.class}; transient Class[] plotFuncSig = new Class[]{Plot.class, AbstractEAIndividual.class};
transient private boolean resetTemplate = true; transient private boolean resetTemplate = true;
public SimpleProblemWrapper() { public SimpleProblemWrapper() {
m_plot = null; plot = null;
initTemplate(); initTemplate();
} }
public SimpleProblemWrapper(SimpleProblemWrapper other) { public SimpleProblemWrapper(SimpleProblemWrapper other) {
other.m_DefaultRange = m_DefaultRange; other.defaultRange = defaultRange;
other.m_Noise = m_Noise; other.noise = noise;
// warning! this does no deep copy! // warning! this does no deep copy!
setSimpleProblem(other.simProb); setSimpleProblem(other.simProb);
} }
@ -59,8 +59,8 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
// evaluate the vector // evaluate the vector
fitness = ((SimpleProblemDouble) simProb).eval(x); fitness = ((SimpleProblemDouble) simProb).eval(x);
// if indicated, add Gaussian noise // if indicated, add Gaussian noise
if (m_Noise != 0) { if (noise != 0) {
RNG.addNoise(fitness, m_Noise); RNG.addNoise(fitness, noise);
} }
// set the fitness // set the fitness
individual.setFitness(fitness); individual.setFitness(fitness);
@ -80,7 +80,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
@Override @Override
public void evaluatePopulationStart(Population population) { public void evaluatePopulationStart(Population population) {
if (m_plot != null && (!m_plot.isValid())) { if (plot != null && (!plot.isValid())) {
openPlot(); openPlot();
} }
} }
@ -89,15 +89,14 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
public void evaluatePopulationEnd(Population population) { public void evaluatePopulationEnd(Population population) {
super.evaluatePopulationEnd(population); super.evaluatePopulationEnd(population);
repaintCnt += population.size(); repaintCnt += population.size();
if (m_plot != null) { if (plot != null) {
if (repaintCnt >= repaintMinWait) { // dont repaint always for small pops if (repaintCnt >= repaintMinWait) { // dont repaint always for small pops
if ((bestIndy == null) || (population.getBestEAIndividual().isDominant(bestIndy.getFitness()))) { if ((bestIndy == null) || (population.getBestEAIndividual().isDominant(bestIndy.getFitness()))) {
// only paint improvement // only paint improvement
bestIndy = population.getBestEAIndividual(); bestIndy = population.getBestEAIndividual();
Object[] args = new Object[2]; Object[] args = new Object[2];
args[0] = m_plot; args[0] = plot;
args[1] = bestIndy; args[1] = bestIndy;
// System.out.println(population.getBestEAIndividual().getStringRepresentation());
BeanInspector.callIfAvailable(simProb, plotFunc, args); BeanInspector.callIfAvailable(simProb, plotFunc, args);
} }
repaintCnt = 0; repaintCnt = 0;
@ -147,11 +146,11 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
} }
protected double getRangeLowerBound(int dim) { protected double getRangeLowerBound(int dim) {
return -m_DefaultRange; return -defaultRange;
} }
protected double getRangeUpperBound(int dim) { protected double getRangeUpperBound(int dim) {
return m_DefaultRange; return defaultRange;
} }
/** /**
@ -162,7 +161,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
} }
private void openPlot() { 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(), "noise", (simProb instanceof SimpleProblemDouble));
GenericObjectEditor.setShowProperty(getClass(), "defaultRange", (simProb instanceof SimpleProblemDouble)); GenericObjectEditor.setShowProperty(getClass(), "defaultRange", (simProb instanceof SimpleProblemDouble));
if (BeanInspector.hasMethod(simProb, plotFunc, plotFuncSig) != null) { if (BeanInspector.hasMethod(simProb, plotFunc, plotFuncSig) != null) {
if (m_plot == null) { if (plot == null) {
openPlot(); openPlot();
} else { } else {
if (!m_plot.isValid()) { if (!plot.isValid()) {
m_plot.dispose(); plot.dispose();
openPlot(); openPlot();
} else { } else {
m_plot.clearAll(); plot.clearAll();
} }
} }
} else if (m_plot != null) { } else if (plot != null) {
m_plot.dispose(); plot.dispose();
m_plot = null; plot = null;
} }
} }
@ -207,11 +206,11 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
if (noise < 0) { if (noise < 0) {
noise = 0; noise = 0;
} }
this.m_Noise = noise; this.noise = noise;
} }
public double getNoise() { public double getNoise() {
return this.m_Noise; return this.noise;
} }
public String noiseTipText() { public String noiseTipText() {
@ -225,7 +224,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
* @return value of the absolute range limit * @return value of the absolute range limit
*/ */
public double getDefaultRange() { public double getDefaultRange() {
return m_DefaultRange; return defaultRange;
} }
/** /**
@ -234,7 +233,7 @@ public class SimpleProblemWrapper extends AbstractOptimizationProblem {
* @param defaultRange * @param defaultRange
*/ */
public void setDefaultRange(double defaultRange) { public void setDefaultRange(double defaultRange) {
this.m_DefaultRange = defaultRange; this.defaultRange = defaultRange;
initTemplate(); initTemplate();
} }