Hatch logging properties file / print fixes
authorBill Erickson <berickxx@gmail.com>
Fri, 18 Nov 2016 21:00:20 +0000 (16:00 -0500)
committerBill Erickson <berickxx@gmail.com>
Fri, 18 Nov 2016 21:00:20 +0000 (16:00 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
logging.properties [new file with mode: 0644]
run.sh
src/org/evergreen_ils/hatch/FileIO.java
src/org/evergreen_ils/hatch/Hatch.java
src/org/evergreen_ils/hatch/MessageIO.java
src/org/evergreen_ils/hatch/PrintManager.java
src/org/evergreen_ils/hatch/RequestHandler.java
src/org/evergreen_ils/hatch/TestHatch.java

diff --git a/logging.properties b/logging.properties
new file mode 100644 (file)
index 0000000..8aab437
--- /dev/null
@@ -0,0 +1,20 @@
+
+# Log to console and file
+handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
+
+# This format is more consice than the default -- one line per message.
+java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n
+java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
+
+# log files go to $SYSTEM_TMP/hatch.log
+java.util.logging.FileHandler.pattern = %t/hatch.log
+java.util.logging.FileHandler.limit = 50000
+
+# Log everything everywhre
+org.evergreen_ils.hatch.level=ALL
+java.util.logging.ConsoleHandler.level=ALL
+java.util.logging.FileHandler.level=ALL
+
+# Avoid duplicate logs
+java.util.logging.ConsoleHandler.useParentHandlers=false
+java.util.logging.FileHandler.useParentHandlers=false
diff --git a/run.sh b/run.sh
index 272bfe0..4ef34dd 100755 (executable)
--- a/run.sh
+++ b/run.sh
@@ -2,7 +2,8 @@
 
 JAVA_HOME=jdk1.8
 JAVA=$JAVA_HOME/bin/java
-LOGS=-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n'
+#LOGS=-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n'
+LOGS=-Djava.util.logging.config.file=logging.properties
 
 COMMAND="$1"
 
index 5d857f0..355c242 100644 (file)
@@ -59,7 +59,7 @@ public class FileIO {
     // -------------------------------------------------- 
 
     // logger
-    private static final Logger logger = Hatch.getLogger();
+    static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
 
     /**
      * Constructs a new FileIO with the provided base path.
index 12b32c0..9a8759b 100644 (file)
@@ -49,12 +49,11 @@ public class Hatch extends Application {
     /** BrowserView requires a stage for rendering */
     private Stage primaryStage;
     
-    /** Our logger instance */
-    static Logger logger;
-
     /** Queue of incoming print requests */
     private static LinkedBlockingQueue<JSONObject> 
         printRequestQueue = new LinkedBlockingQueue<JSONObject>();
+
+    static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
     
     /**
      * Printable region containing a browser.
@@ -146,24 +145,6 @@ public class Hatch extends Application {
         }
     }
 
-    /* TODO: make me configurable via config file
-     */
-    public static Logger getLogger() {
-        if (logger != null) return logger;
-
-        logger = Logger.getLogger("org.evergreen_ils.hatch");
-        logger.setLevel(Level.ALL);
-
-        ConsoleHandler handler = new ConsoleHandler();
-        handler.setLevel(Level.ALL);
-        handler.setFormatter(new SimpleFormatter());
-        logger.addHandler(handler);
-
-        logger.setUseParentHandlers(false);
-
-        return logger;
-    }
-
 
     /**
      * Hatch main.
index caa3863..5e2b3cd 100644 (file)
@@ -36,7 +36,7 @@ public class MessageIO {
     private MessageReader reader;
     private MessageWriter writer;
 
-    static final Logger logger = Hatch.getLogger();
+    static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
 
     public MessageIO() {
         inQueue = new LinkedBlockingQueue<JSONObject>();
@@ -128,7 +128,7 @@ public class MessageIO {
 
             String message = new String(msgBytes, "UTF-8");
 
-            logger.info("MessageReader read: " + message.length());
+            logger.finest("MessageReader read: " + message);
 
             return message;
         }
@@ -166,8 +166,6 @@ public class MessageIO {
                 }
 
                 inQueue.offer(jsonMsg);
-
-                logger.finest("inQueue contains " + inQueue.size() + " messages");
             }
         }
     }
index c751ca4..4121dd5 100644 (file)
@@ -45,7 +45,7 @@ import org.json.*;
 public class PrintManager {
 
     /** Our logger instance */
-    static final Logger logger = Hatch.getLogger();
+   static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
 
     /**
      * Returns all known Printer's.
@@ -118,7 +118,6 @@ public class PrintManager {
         response.put("message", "OK");
 
         try {
-
             response.put("clientid", request.getLong("clientid"));
             response.put("msgid", request.getLong("msgid"));
 
@@ -212,41 +211,56 @@ public class PrintManager {
     protected PageLayout buildPageLayout(
         JSONObject settings, Printer printer) {
 
-        JSONObject layoutMap = settings.optJSONObject("pageLayout");
+        PrinterAttributes printerAttrs = printer.getPrinterAttributes();
 
-        if (layoutMap == null) {
-            // Start with a sane default. The Java default is wonky.
-            return printer.createPageLayout(
-                Paper.NA_LETTER,
-                PageOrientation.PORTRAIT,
-                Printer.MarginType.DEFAULT
-            );
-        }
+        // Start with default page layout options, replace where possible.
+        Paper paper = printerAttrs.getDefaultPaper();
+        PageOrientation orientation = printerAttrs.getDefaultPageOrientation();
 
-        PrinterAttributes printerAttrs = printer.getPrinterAttributes();
+        String paperName = settings.optString("paper", null);
+        String orientationName = settings.optString("pageOrientation", null);
+        String marginName = settings.optString("marginType", null);
 
-        // find the paper by name
-        Paper paper = null;
-        String paperName = (String) layoutMap.get("paper");
-        Set<Paper> papers = printerAttrs.getSupportedPapers();
-        for (Paper source : papers) {
-            if (source.getName().equals(paperName)) {
-                //logger.info("Found matching paper for " + paperName);
-                paper = source;
-                break;
+        if (paperName != null) {
+            for (Paper source : printerAttrs.getSupportedPapers()) {
+                if (source.getName().equals(paperName)) {
+                    logger.finer("Found matching paper: " + paperName);
+                    paper = source;
+                    break;
+                }
             }
         }
 
-        if (paper == null) 
-            paper = printerAttrs.getDefaultPaper();
+        if (orientationName != null) {
+            orientation = PageOrientation.valueOf(orientationName);
+        }
 
+        if (settings.optBoolean("autoMargins", true)) {
+            // Using a pre-defined, automatic margin option
+
+            Printer.MarginType margin = Printer.MarginType.DEFAULT;
+            if (marginName != null) {
+                // An auto-margin type has been specified
+                for (Printer.MarginType marg : Printer.MarginType.values()) {
+                    if (marg.toString().equals(marginName)) {
+                        logger.finer("Found matching margin: " + marginName);
+                        margin = marg;
+                        break;
+                    }
+                }
+            }
+
+            return printer.createPageLayout(paper, orientation, margin);
+        } 
+
+        // Using manual margins
+        // Any un-specified margins default to 54 == 0.75 inches.
         return printer.createPageLayout(
-            paper,
-            PageOrientation.valueOf((String) layoutMap.get("pageOrientation")),
-            ((Number) layoutMap.get("leftMargin")).doubleValue(),
-            ((Number) layoutMap.get("rightMargin")).doubleValue(),
-            ((Number) layoutMap.get("topMargin")).doubleValue(),
-            ((Number) layoutMap.get("bottomMargin")).doubleValue()
+            paper, orientation, 
+            settings.optDouble("leftMargin", 54),
+            settings.optDouble("rightMargin", 54),
+            settings.optDouble("topMargin", 54),
+            settings.optDouble("bottomMargin", 54)
         );
     }
 
@@ -289,16 +303,13 @@ public class PrintManager {
         }
 
         String paperSource = settings.optString("paperSource");
+        logger.finer("paper source = " + paperSource);
 
         if (paperSource != null) {
-            // find the paperSource by name
-
-            Set<PaperSource> paperSources = 
-                printerAttrs.getSupportedPaperSources();
-
-            for (PaperSource source : paperSources) {
+            for (PaperSource source : printerAttrs.getSupportedPaperSources()) {
+                logger.info("looking at paper source: " + source.getName());
                 if (source.getName().equals(paperSource)) {
-                    logger.fine("Found paper source: " + paperSource);
+                    logger.finer("Found paper source: " + paperSource);
                     jobSettings.setPaperSource(source);
                     break;
                 }
@@ -352,7 +363,7 @@ public class PrintManager {
             printerAttrs.getSupportedPaperSources()) {
             paperSourcesArray.put(source.getName());
         }
-        options.put("paperSource", papersArray);
+        options.put("paperSource", paperSourcesArray);
         options.put("defaultPaperSource", 
             printerAttrs.getDefaultPaperSource().getName());
 
index e290c9e..f221fb6 100644 (file)
@@ -28,7 +28,7 @@ public class RequestHandler extends Thread {
     /** STDIN/STDOUT handler */
     private static MessageIO io = new MessageIO();
 
-    static final Logger logger = Hatch.getLogger();
+    static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
 
     /** Root directory for all FileIO operations */
     private static String profileDirectory = null;
index 96624af..3389ffc 100644 (file)
@@ -20,7 +20,7 @@ import org.json.*;
 
 public class TestHatch {
     static MessageIO io;
-    static final Logger logger = Hatch.getLogger();
+    static final Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
 
     public static void pause() {
         try {