# HG changeset patch # User xitiomet # Date 1323455938 18000 # Node ID 400f90c8aa46e8cdaad366b1639581726d2b7dcd # Parent 45513e8a4d4060026a4bbeda798d3c5d31aadba9 logger 2.0 diff -r 45513e8a4d4060026a4bbeda798d3c5d31aadba9 -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd .hgignore --- a/.hgignore Wed Nov 30 18:06:26 2011 -0500 +++ b/.hgignore Fri Dec 09 13:38:58 2011 -0500 @@ -5,6 +5,7 @@ *.orig *.ini *.app +._* doc build jvm-build diff -r 45513e8a4d4060026a4bbeda798d3c5d31aadba9 -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd src/org/openstatic/placebo/ControlPanel.java --- a/src/org/openstatic/placebo/ControlPanel.java Wed Nov 30 18:06:26 2011 -0500 +++ b/src/org/openstatic/placebo/ControlPanel.java Fri Dec 09 13:38:58 2011 -0500 @@ -389,7 +389,7 @@ core.setWebRoot(root_field.getText()); core.setDebug(debug_field.isSelected()); core.setShowData(show_data_field.isSelected()); - core.getPlaceboLogger().setDisplay(new PrintStream(log_output, true)); + core.getPlaceboLogger().addDisplay(new PrintStream(log_output, true)); if (debug_field.isSelected()) { diff -r 45513e8a4d4060026a4bbeda798d3c5d31aadba9 -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd src/org/openstatic/placebo/CoreServer.java --- a/src/org/openstatic/placebo/CoreServer.java Wed Nov 30 18:06:26 2011 -0500 +++ b/src/org/openstatic/placebo/CoreServer.java Fri Dec 09 13:38:58 2011 -0500 @@ -69,7 +69,8 @@ { this.http_port = 80; this.web_root = "."; - this.myLogger = new PlaceboLogger(System.err); + this.myLogger = new PlaceboLogger(); + this.myLogger.addDisplay(System.err); this.keep_running = true; this.debug = false; this.show_data = false; diff -r 45513e8a4d4060026a4bbeda798d3c5d31aadba9 -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd src/org/openstatic/util/PlaceboLogEvent.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/openstatic/util/PlaceboLogEvent.java Fri Dec 09 13:38:58 2011 -0500 @@ -0,0 +1,112 @@ +/* + Copyright (C) 2010 Brian Dunigan + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package org.openstatic.util; + +import java.util.Date; +import java.util.StringTokenizer; +import java.text.SimpleDateFormat; + +public class PlaceboLogEvent +{ + private Date timestamp; + private String caption; + private String message; + + public PlaceboLogEvent(String line) + { + this.timestamp = new Date(); + if (line.startsWith("[") && line.contains("]")) + { + this.caption = line.substring(line.indexOf("[")+1, line.indexOf("]")); + this.message = line.substring(line.indexOf("]")+1); + } else { + this.caption = "debug"; + this.message = line; + } + } + + public PlaceboLogEvent(String caption, String message) + { + this.timestamp = new Date(); + this.caption = caption; + this.message = message; + } + + public void setDate(Date date) + { + this.timestamp = date; + } + + public void setCaption(String caption) + { + this.caption = caption; + } + + public void setMessage(String message) + { + this.message = message; + } + + public Date getDate() + { + return this.timestamp; + } + + public String getCaption() + { + return this.caption; + } + + public String getMessage() + { + return this.message; + } + + public String toString() + { + SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); + String date_time = df.format(this.timestamp); + return "<" + setSizedString(date_time,23) + "> [ " + setSizedString(this.caption, 16) + " ] " + message.replaceAll("\r","").replaceAll("\n",""); + } + + public static String setSizedString(String value, int size) + { + if (value == null) + { + return getPaddingSpace(size); + } else if (value.length() == size) { + return value; + } else if (value.length() > size) { + return value.substring(0, size); + } else if (value.length() < size) { + return value + getPaddingSpace(size - value.length()); + } else { + return null; + } + } + + public static String getPaddingSpace(int value) + { + StringBuffer x = new StringBuffer(""); + for (int n = 0; n < value; n++) + { + x.append(" "); + } + return x.toString(); + } +} diff -r 45513e8a4d4060026a4bbeda798d3c5d31aadba9 -r 400f90c8aa46e8cdaad366b1639581726d2b7dcd src/org/openstatic/util/PlaceboLogger.java --- a/src/org/openstatic/util/PlaceboLogger.java Wed Nov 30 18:06:26 2011 -0500 +++ b/src/org/openstatic/util/PlaceboLogger.java Fri Dec 09 13:38:58 2011 -0500 @@ -19,50 +19,50 @@ import java.io.PrintStream; import java.io.ByteArrayOutputStream; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Date; import java.util.StringTokenizer; -import java.util.Hashtable; +import java.util.Vector; import java.util.Enumeration; import java.text.SimpleDateFormat; +import java.util.concurrent.LinkedBlockingQueue; +import java.net.ServerSocket; +import java.net.Socket; public class PlaceboLogger { - private Hashtable inbound; - private PrintStream outbound; + private Vector outbound; + private LinkedBlockingQueue main_log; private PrintStream inbound_ps; - - public PlaceboLogger(PrintStream display) + + public PlaceboLogger() { - this.inbound = new Hashtable(); - this.inbound_ps = this.getPrintStream("debug"); - this.outbound = display; + this.outbound = new Vector(); + this.main_log = new LinkedBlockingQueue(); + beginThread(); + } + + private void beginThread() + { Thread t = new Thread() { public void run() { while(true) { - for (Enumeration keys = PlaceboLogger.this.inbound.keys(); keys.hasMoreElements(); ) + PlaceboLogEvent line = PlaceboLogger.this.main_log.poll(); + if (line != null) { - String caption = keys.nextElement(); - ByteArrayOutputStream baos = PlaceboLogger.this.inbound.get(caption); - if (baos.size() > 0 && PlaceboLogger.this.outbound != null) + for (Enumeration e = PlaceboLogger.this.outbound.elements(); e.hasMoreElements();) { - StringTokenizer st = new StringTokenizer(baos.toString(), "\n"); - baos.reset(); - while (st.hasMoreTokens()) + PrintStream out = e.nextElement(); + try { - SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); - String date_time = df.format(new Date()); - String msg = st.nextToken(); - String temp_caption = caption; - if (msg.startsWith("[") && msg.contains("]")) - { - temp_caption = msg.substring(msg.indexOf("[")+1, msg.indexOf("]")); - msg = msg.substring(msg.indexOf("]")+1); - } - PlaceboLogger.this.outbound.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(temp_caption, 16) + " ] " + msg.replaceAll("\r","")); - } + out.println(line.toString()); + } catch (Exception pr) {} } } try @@ -75,62 +75,126 @@ t.start(); } - public void setDisplay(PrintStream display) + public void startServer(final int port) { - this.outbound = display; + Thread t = new Thread() + { + public void run() + { + try + { + ServerSocket ss = new ServerSocket(port); + while(true) + { + try + { + Socket new_connection = ss.accept(); + PlaceboLogger.this.addDisplay(new_connection.getOutputStream()); + } catch (Exception x) {} + } + } catch (Exception n) { + } + } + }; + t.start(); + } + + public void addDisplay(OutputStream display) + { + this.outbound.add(new PrintStream(display,true)); } + public void addDisplay(PrintStream display) + { + this.outbound.add(display); + } + + public void addSource(final String caption, final InputStream source) + { + Thread t = new Thread() + { + public void run() + { + try + { + String line = ""; + BufferedReader br = new BufferedReader(new InputStreamReader(source)); + while(line != null) + { + line = br.readLine(); + PlaceboLogEvent ple = new PlaceboLogEvent(line); + if (ple.getCaption().equals("debug") & caption != null) + ple.setCaption(caption); + PlaceboLogger.this.log(ple); + } + } catch (Exception e) {} + } + }; + t.start(); + } + public PrintStream getPrintStream() { - return this.inbound_ps; + return getPrintStream(null); } - - public PrintStream getPrintStream(String caption) + + public PrintStream getPrintStream(final String caption) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream z = new PrintStream(baos, true); - this.inbound.put(caption, baos); + Thread t = new Thread() + { + public void run() + { + while (true) + { + if (baos.size() > 0) + { + StringTokenizer st = new StringTokenizer(baos.toString(), "\n"); + baos.reset(); + while (st.hasMoreTokens()) + { + String line = st.nextToken(); + PlaceboLogEvent ple = new PlaceboLogEvent(line); + if (ple.getCaption().equals("debug") & caption != null) + ple.setCaption(caption); + PlaceboLogger.this.log(ple); + } + } else { + try + { + Thread.sleep(100); + } catch (Exception e) {} + } + } + } + }; + t.start(); return z; } public void println(String text) { - this.inbound_ps.println(text); + this.log(new PlaceboLogEvent(text)); } public void logln(String text) { - this.inbound_ps.println(text); + this.log(new PlaceboLogEvent(text)); + } + + public void log(PlaceboLogEvent event) + { + try + { + this.main_log.put(event); + } catch (Exception e) { + + } } public void logln(String caption, String text) { - this.inbound_ps.println("[" + caption + "]" + text); - } - - public static String setSizedString(String value, int size) - { - if (value == null) - { - return getPaddingSpace(size); - } else if (value.length() == size) { - return value; - } else if (value.length() > size) { - return value.substring(0, size); - } else if (value.length() < size) { - return value + getPaddingSpace(size - value.length()); - } else { - return null; - } - } - - public static String getPaddingSpace(int value) - { - StringBuffer x = new StringBuffer(""); - for (int n = 0; n < value; n++) - { - x.append(" "); - } - return x.toString(); + this.log(new PlaceboLogEvent(caption, text)); } }