chiark / gitweb /
debug logging: replace logprogress and if(dtxt!=null)... with debuglog() function
[jarrg-ian.git] / src / net / chiark / yarrg / MarketUploader.java
index dda15380e0b0d4ab1b6074dca9ddc999a7786213..4f50228a7b77f8131902fb5e19228500322f3725 100644 (file)
@@ -86,7 +86,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     }
   }
 
-  private void progresslog(String s) {
+  private void debuglog(String s) {
     if (dtxt == null) return;
     long now = new Date().getTime();
     dtxt.println("progress "+(now - startTime)+"ms "+s);
@@ -288,8 +288,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     uploadToPCTB=prefs.getBoolean("uploadToPCTB", true);
     showArbitrage=prefs.getBoolean("showArbitrage", true);
 
-    if (dtxt!=null) dtxt.println("main on dispatch thread: "+
-                                EventQueue.isDispatchThread());
+    debuglog("main on dispatch thread: "+EventQueue.isDispatchThread());
     EventQueue.invokeLater(this);
   }
 
@@ -298,12 +297,12 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
    * every top-level window, and if it
    */
   public void run() {
-    if (dtxt!=null) dtxt.println("MarketUploader run()...");
+    debuglog("MarketUploader run()...");
     if (EventQueueMonitor.isGUIInitialized()) {
-      if (dtxt!=null) dtxt.println("MarketUploader GUI already ready");
+      debuglog("MarketUploader GUI already ready");
       guiInitialized();
     } else {
-      if (dtxt!=null) dtxt.println("MarketUploader waiting for GUI");
+      debuglog("MarketUploader waiting for GUI");
       EventQueueMonitor.addGUIInitializedListener(this);
     }
   }
@@ -312,13 +311,13 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     Window ws[]= EventQueueMonitor.getTopLevelWindows();
     EventQueueMonitor.addTopLevelWindowListener(this);
     for (int i=0; i<ws.length; i++) {
-      if (dtxt!=null) dtxt.println("MarketUploader existing toplevel "+i);
+      debuglog("MarketUploader existing toplevel "+i);
       topLevelWindowCreated(ws[i]);
     }
   }
 
   public void topLevelWindowDestroyed(Window w) {
-    if (dtxt!=null) dtxt.println("MarketUploader destroyed toplevel");
+    debuglog("MarketUploader destroyed toplevel");
   }
        
   public void topLevelWindowCreated(Window w) {
@@ -326,10 +325,10 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       // already got it
       return;
     String name = w.getAccessibleContext().getAccessibleName();
-    if (dtxt!=null) dtxt.println("MarketUploader new toplevel "+name);
+    debuglog("MarketUploader new toplevel "+name);
     if (!name.equals("Puzzle Pirates"))
       return;
-    if (dtxt!=null) dtxt.println("MarketUploader found toplevel, creating gui");
+    debuglog("MarketUploader found toplevel, creating gui");
     window = w;
     createGUI();
     frame.setVisible(true);
@@ -454,7 +453,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     }
     String whole_msg = "<html><h1>Error</h1>"+msg
       +"<h1>PCTB Server said:</h1><blockquote>"+html+"</blockquote>";
-    if (dtxt!=null) dtxt.println("###" + whole_msg + "###");
+    debuglog("###" + whole_msg + "###");
   
     error(whole_msg);
   }
@@ -487,7 +486,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        int index = text.indexOf(":");
        String name = text.substring(0,index);
        islandName = name;
-       // if (dtxt!=null) dtxt.println(islandName);
+       // debuglog(islandName);
        sidePanel.removePropertyChangeListener(this);
        latch.countDown();
       }
@@ -569,7 +568,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     public void run() {
       try {
        ts = getYarrgTimestamp();
-       progresslog("(async) yarrg timestamp ready.");
+       debuglog("(async) yarrg timestamp ready.");
       } catch(Exception e) {
        error("Error getting YARRG timestamp: "+e);
       }
@@ -582,10 +581,10 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     boolean doneyarrg = false, donepctb = false;
     YarrgTimestampFetcher yarrgts_thread = null;
 
-    progresslog("starting");
+    debuglog("starting");
 
     if (uploadToYarrg) {
-      progresslog("(async) yarrg timestamp...");
+      debuglog("(async) yarrg timestamp...");
       yarrgts_thread = new YarrgTimestampFetcher();
       yarrgts_thread.start();
     }
@@ -612,11 +611,11 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        return null;
       }
 
-      progresslog("(async) getisland...");
+      debuglog("(async) getisland...");
       getIsland();
-      progresslog("getocean...");
+      debuglog("getocean...");
       getOcean();
-      progresslog("getocean done");
+      debuglog("getocean done");
 
       return at;
     }}.exec();
@@ -625,13 +624,13 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     if (latch != null) {
       latch.await(2, java.util.concurrent.TimeUnit.SECONDS);
     }
-    progresslog("(async) getisland done");
+    debuglog("(async) getisland done");
 
     String yarrgts = null;
     if (yarrgts_thread != null) {
-      progresslog("(async) yarrg timestamp join...");
+      debuglog("(async) yarrg timestamp join...");
       yarrgts_thread.join();
-      progresslog("(async) yarrg timestamp joined.");
+      debuglog("(async) yarrg timestamp joined.");
       yarrgts = yarrgts_thread.ts;
     }
 
@@ -640,7 +639,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       return;
     }
 
-    progresslog("table check...");
+    debuglog("table check...");
 
     final ArrayList<ArrayList<String>> data =
     new UIXR<ArrayList<ArrayList<String>>>
@@ -671,20 +670,20 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        }
       }
 
-      progresslog("table read...");
+      debuglog("table read...");
 
       return getData(accesstable);
     }}.exec();
     if (data == null) return;
 
     if (showArbitrage) {
-      progresslog("arbitrage...");
+      debuglog("arbitrage...");
       calculateArbitrage(data);
-      progresslog("arbitrage done.");
+      debuglog("arbitrage done.");
     }
 
     if (uploadToYarrg && yarrgts != null) {
-      progresslog("yarrg prepare...");
+      debuglog("yarrg prepare...");
       progressNote("Yarrg: Preparing data");
       setProgress(10);
 
@@ -706,14 +705,14 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       yarrgdata = yarrgsb.toString();
 
       progressNote("Yarrg: Uploading");
-      progresslog("yarrg upload...");
+      debuglog("yarrg upload...");
 
       doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
-      progresslog("yarrg done.");
+      debuglog("yarrg done.");
     }
 
     if (uploadToPCTB) {
-      progresslog("pctb prepare...");
+      debuglog("pctb prepare...");
       progressNote("PCTB: Getting stall names");
       setProgress(20);
       if(isCanceled()) {
@@ -729,15 +728,15 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       }
       // get commod map
                
-      progresslog("pctb commodmap...");
+      debuglog("pctb commodmap...");
       HashMap<String,Integer> commodMap = getCommodMap();
       if(commodMap == null) {
        return;
       }
-      progresslog("pctb commodmap done.");
+      debuglog("pctb commodmap done.");
       int[] offerCount = getBuySellMaps(data,buys,sells,stallMap,commodMap);
-      // if (dtxt!=null) dtxt.println(sells);
-      // if (dtxt!=null) dtxt.println("\n\n\n"+buys);
+      // debuglog(sells);
+      // debuglog("\n\n\n"+buys);
 
       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
       setProgress(60);
@@ -752,22 +751,22 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       dos.writeBytes(getAbbrevStallList(stallMap));
       writeBuySellOffers(buys,sells,offerCount,out);
       out.finish();
-      progresslog("pctb send...");
+      debuglog("pctb send...");
 
       byte[] ba = outStream.toByteArray();
       debug_write_bytes("pctb-marketdata.gz", ba);
 
       InputStream in = sendInitialData(new ByteArrayInputStream(ba));
-      progresslog("pctb sent.");
+      debuglog("pctb sent.");
       if (in == null) return;
       setProgress(80);
       if(isCanceled()) {
        return;
       }
       progressNote("PCTB: Waiting ...");
-      progresslog("pctb finish...");
+      debuglog("pctb finish...");
       donepctb = finishUpload(in);
-      progresslog("pctb done.");
+      debuglog("pctb done.");
     }
     setProgress(99);
 
@@ -787,7 +786,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       resultSummary.setText(summary_final);
     }}.exec();
 
-    progresslog("done.");
+    debuglog("done.");
   }
        
   /**
@@ -821,14 +820,14 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     Accessible node = descendNodes(node1,new int[] {0,1,0,0,0,0,1,0,0,1,0,0}); 
       // commod market
     // commod market: {0,1,0,0,0,0,1,0,0,1,0}  {0,1,0,0,0,0,1,0,1,0,0,1,0,0})
-    // if (dtxt!=null) dtxt.println(node);
+    // debuglog(node);
     if (!(node instanceof JTable)) {
       node = descendNodes(node1,new int[] {0,1,0,0,0,0,1,0,1,0,0,1,0,0});
         // commod market
     }
     if (!(node instanceof JTable)) return null;
     AccessibleTable table = node.getAccessibleContext().getAccessibleTable();
-    // if (dtxt!=null) dtxt.println(table);
+    // debuglog(table);
     return table;
   }
        
@@ -865,14 +864,12 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     if (parent == null) return null;
     int children = parent.getAccessibleContext().getAccessibleChildrenCount();
     if (childNum >= children) {
-      if (dtxt!=null) dtxt.println("DESCEND "+childNum+" > "
-                                  +children+" NOT FOUND");
+      debuglog("DESCEND "+childNum+" > "+children+" NOT FOUND");
       return null;
     }
     Accessible child = parent.getAccessibleContext()
       .getAccessibleChild(childNum);
-    if (dtxt!=null) dtxt.println("DESCEND "+childNum+" "
-                                +child.getClass().getName()+" OK");
+    debuglog("DESCEND "+childNum+" "+child.getClass().getName()+" OK");
     return child;
   }
 
@@ -891,11 +888,11 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
     for (int i=0; i<children; i++) {
       Accessible child = ac.getAccessibleChild(i);
       if (child.getClass().getName() == classname) {
-       if (dtxt!=null) dtxt.println("DESCEND CLASS "+classname+" OK");
+       debuglog("DESCEND CLASS "+classname+" OK");
        return child;
       }
     }
-    if (dtxt!=null) dtxt.println("DESCEND CLASS "+classname+" NOT FOUND");
+    debuglog("DESCEND CLASS "+classname+" NOT FOUND");
     return null;
   }
 
@@ -1030,8 +1027,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        }
       } catch(IllegalArgumentException e) {
        unknownPCTBcommods++;
-       if (dtxt!=null) dtxt.println("Error: Unsupported Commodity \""
-                                    + offer.get(0) + "\"");
+       debuglog("Error: Unsupported Commodity \"" + offer.get(0) + "\"");
       }
     }
     if (buySellCount[0]==0 && buySellCount[1]==0) {
@@ -1329,7 +1325,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
   }
 
   private int calculateArbitrageCommodity(ArrayList<SortedSet<int[]>> arb_bs) {
-    // if (dtxt!=null) dtxt.println("ARBITRAGE?");
+    // debuglog("ARBITRAGE?");
     int profit = 0;
     SortedSet<int[]> buys = arb_bs.get(0);
     SortedSet<int[]> sells = arb_bs.get(1);
@@ -1345,9 +1341,9 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
 
       int unitprofit = buy[0] - sell[0];
       int count = buy[1] < sell[1] ? buy[1] : sell[1];
-      // if (dtxt!=null) dtxt.println(" sell @"+sell[0]+" x"+sell[1]
-      //                       +" buy @"+buy[0]+" x"+buy[1]
-      //                      +" => x"+count+" @"+unitprofit);
+      // debuglog(" sell @"+sell[0]+" x"+sell[1]
+      //          +" buy @"+buy[0]+" x"+buy[1]
+      //         +" => x"+count+" @"+unitprofit);
 
       if (unitprofit <= 0)
        break;
@@ -1358,7 +1354,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
       if (buy[1]==0) buys.remove(buy);
       if (sell[1]==0) sells.remove(sell);
     }
-    // if (dtxt!=null) dtxt.println(" PROFIT "+profit);
+    // debuglog(" PROFIT "+profit);
     return profit;
   }
 
@@ -1380,15 +1376,15 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
 
     for (ArrayList<String> row : data) {
       String thiscommod = row.get(0);
-      // if (dtxt!=null) dtxt.println("ROW "+row.toString());
+      // debuglog("ROW "+row.toString());
       if (lastcommod == null || !thiscommod.equals(lastcommod)) {
        if (lastcommod != null)
          arbitrage += calculateArbitrageCommodity(arb_bs);
-       // if (dtxt!=null) dtxt.println("ROW rdy");
+       // debuglog("ROW rdy");
        arb_bs = new ArrayList<SortedSet<int[]>>(2);
        arb_bs.add(0, new TreeSet<int[]>(compar));
        arb_bs.add(1, new TreeSet<int[]>(compar));
-       // if (dtxt!=null) dtxt.println("ROW init");
+       // debuglog("ROW init");
        lastcommod = thiscommod;
       }
       for (int bs = 0; bs < 2; bs++) {
@@ -1396,7 +1392,7 @@ implements Runnable, TopLevelWindowListener, GUIInitializedListener {
        if (pricestr == null)
          continue;
        int[] entry = new int[2];
-       // if (dtxt!=null) dtxt.println("ROW BS "+bs);
+       // debuglog("ROW BS "+bs);
        entry[0] = parseQty(pricestr);
        entry[1] = parseQty(row.get(bs*2 + 3));
        arb_bs.get(bs).add(entry);