Use java.util Logging API instead of a debug mode and System.out.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 5 Jan 2009 22:51:39 +0000 (23:51 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 1 Jun 2009 14:14:42 +0000 (16:14 +0200)
src/de/todesbaum/jsite/application/ProjectInserter.java
src/de/todesbaum/jsite/application/UpdateChecker.java
src/de/todesbaum/jsite/gui/ProjectInsertPage.java
src/de/todesbaum/jsite/main/Main.java

index 46cdbd4..b7d448a 100644 (file)
@@ -30,6 +30,8 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -54,15 +56,15 @@ import de.todesbaum.util.io.StreamCopier;
  */
 public class ProjectInserter implements FileScannerListener, Runnable {
 
+       /** The logger. */
+       private static final Logger logger = Logger.getLogger(ProjectInserter.class.getName());
+
        /** Random number for FCP instances. */
        private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
 
        /** Counter for FCP connection identifier. */
        private static int counter = 0;
 
-       /** Whether debug mode is set. */
-       private boolean debug = false;
-
        /** The list of insert listeners. */
        private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
 
@@ -173,17 +175,6 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        }
 
        /**
-        * Sets the debug mode.
-        *
-        * @param debug
-        *            <code>true</code> to activate debug mode, <code>false</code>
-        *            to deactivate
-        */
-       public void setDebug(boolean debug) {
-               this.debug = debug;
-       }
-
-       /**
         * Sets the project to insert.
         *
         * @param project
@@ -434,13 +425,11 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                while (!finished) {
                        Message message = client.readMessage();
                        finished = (message == null) || (disconnected = client.isDisconnected());
-                       if (debug) {
-                               System.out.println(message);
-                       }
                        if (firstMessage) {
                                fireProjectUploadFinished();
                                firstMessage = false;
                        }
+                       logger.log(Level.FINE, "Received message: " + message);
                        if (!finished) {
                                @SuppressWarnings("null")
                                String messageName = message.getName();
index a87beab..445421c 100644 (file)
@@ -24,6 +24,8 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import de.todesbaum.jsite.main.Main;
 import de.todesbaum.jsite.main.Version;
@@ -43,6 +45,9 @@ import de.todesbaum.util.io.Closer;
  */
 public class UpdateChecker implements Runnable {
 
+       /** The logger. */
+       private static final Logger logger = Logger.getLogger(UpdateChecker.class.getName());
+
        /** Counter for connection names. */
        private static int counter = 0;
 
@@ -199,7 +204,7 @@ public class UpdateChecker implements Runnable {
                int currentEdition = lastUpdateEdition;
                while (!shouldStop()) {
                        checkNow = false;
-                       System.out.println("Trying " + constructUpdateKey(currentEdition));
+                       logger.log(Level.FINE, "Trying " + constructUpdateKey(currentEdition));
                        ClientGet clientGet = new ClientGet("get-update-key");
                        clientGet.setUri(constructUpdateKey(currentEdition));
                        clientGet.setPersistence(Persistence.CONNECTION);
@@ -210,7 +215,7 @@ public class UpdateChecker implements Runnable {
                                boolean stop = false;
                                while (!stop) {
                                        Message message = client.readMessage();
-                                       System.out.println(message);
+                                       logger.log(Level.FINEST, "Received message: " + message);
                                        if ("GetFailed".equals(message.getName())) {
                                                if ("27".equals(message.get("code"))) {
                                                        String editionString = message.get("redirecturi").split("/")[2];
@@ -221,7 +226,7 @@ public class UpdateChecker implements Runnable {
                                                                /* ignore. */
                                                        }
                                                        if (editionNumber != -1) {
-                                                               System.out.println("Found new edition " + editionNumber);
+                                                               logger.log(Level.INFO, "Found new edition " + editionNumber);
                                                                currentEdition = editionNumber;
                                                                lastUpdateEdition = editionNumber;
                                                                checkNow = true;
@@ -230,7 +235,7 @@ public class UpdateChecker implements Runnable {
                                                }
                                        }
                                        if ("AllData".equals(message.getName())) {
-                                               System.out.println("Update data found.");
+                                               logger.log(Level.FINE, "Update data found.");
                                                InputStream dataInputStream = null;
                                                Properties properties = new Properties();
                                                try {
@@ -246,7 +251,7 @@ public class UpdateChecker implements Runnable {
                                                        if (foundVersion != null) {
                                                                lastVersion = foundVersion;
                                                                String versionTimestampString = properties.getProperty("jSite.Date");
-                                                               System.out.println(versionTimestampString);
+                                                               logger.log(Level.FINEST, "Version timestamp: " + versionTimestampString);
                                                                long versionTimestamp = -1;
                                                                try {
                                                                        versionTimestamp = Long.parseLong(versionTimestampString);
@@ -262,7 +267,7 @@ public class UpdateChecker implements Runnable {
                                        }
                                }
                        } catch (IOException e) {
-                               System.out.println("Got IOException: " + e.getMessage());
+                               logger.log(Level.INFO, "Got IOException: " + e.getMessage());
                                e.printStackTrace();
                        }
                        if (!checkNow && !shouldStop()) {
index 5fe946e..dc82f53 100644 (file)
@@ -209,17 +209,6 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
        }
 
        /**
-        * Sets whether to activate the debug mode.
-        *
-        * @param debug
-        *            <code>true</code> to activate the debug mode,
-        *            <code>false</code> to deactivate.
-        */
-       public void setDebug(boolean debug) {
-               projectInserter.setDebug(debug);
-       }
-
-       /**
         * Sets the project to insert.
         *
         * @param project
index 1138dc2..0f5e5ae 100644 (file)
@@ -30,6 +30,10 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.Map.Entry;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
@@ -69,9 +73,6 @@ import de.todesbaum.util.swing.WizardListener;
  */
 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
 
-       /** Whether the debug mode is activated. */
-       private static boolean debug = false;
-
        /** The version. */
        private static final Version VERSION = new Version(0, 7);
 
@@ -304,7 +305,6 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
 
                ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
-               projectInsertPage.setDebug(debug);
                projectInsertPage.setName("page.project.insert");
                projectInsertPage.setFreenetInterface(freenetInterface);
                pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
@@ -611,6 +611,10 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
         *            The command-line arguments
         */
        public static void main(String[] args) {
+               /* initialize logger. */
+               Logger logger = Logger.getLogger("de.todesbaum");
+               Handler handler = new ConsoleHandler();
+               logger.addHandler(handler);
                String configFilename = null;
                boolean nextIsConfigFilename = false;
                for (String argument : args) {
@@ -622,7 +626,8 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                                printHelp();
                                return;
                        } else if ("--debug".equals(argument)) {
-                               debug = true;
+                               logger.setLevel(Level.ALL);
+                               handler.setLevel(Level.ALL);
                        } else if ("--config-file".equals(argument)) {
                                nextIsConfigFilename = true;
                        }