# HG changeset patch # User xitiomet # Date 1304578143 14400 # Node ID fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 # Parent fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde Logging changes diff -r fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde -r fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 Makefile --- a/Makefile Sun Apr 10 19:44:44 2011 -0400 +++ b/Makefile Thu May 05 02:49:03 2011 -0400 @@ -73,6 +73,9 @@ build/org/openstatic/util/JTextAreaOutputStream.class: src/org/openstatic/util/JTextAreaOutputStream.java $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $< +build/org/openstatic/util/LiquidLogger.class: src/org/openstatic/util/LiquidLogger.java + $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $< + build/org/openstatic/util/JSONUtil.class: src/org/openstatic/util/JSONUtil.java build/org/json/JSONObject.class build/org/json/JSONArray.class $(JC) $(JC_FLAGS) --classpath=$(CLASS_PATH) -C $< @@ -103,7 +106,7 @@ # Main Builds # ------------------------------------------------------------------------------- -placebohttp.jar: build/org/openstatic/http/PlaceboHttpServer.class build/org/openstatic/http/PlaceboSession.class build/org/openstatic/http/HttpRequest.class build/org/openstatic/http/HttpRequestThread.class build/org/openstatic/http/HttpResponse.class build/PlaceboBase.class build/org/openstatic/placebo/ControlPanel.class build/org/openstatic/placebo/CoreServer.class build/org/openstatic/util/JTextAreaOutputStream.class build/org/openstatic/util/JSONUtil.class build/org/openstatic/placebo/PlaceboPlugin.class build/org/openstatic/placebo/plugins/Clock.class build/org/openstatic/placebo/plugins/KeyStore.class +placebohttp.jar: build/org/openstatic/http/PlaceboHttpServer.class build/org/openstatic/http/PlaceboSession.class build/org/openstatic/http/HttpRequest.class build/org/openstatic/http/HttpRequestThread.class build/org/openstatic/http/HttpResponse.class build/PlaceboBase.class build/org/openstatic/placebo/ControlPanel.class build/org/openstatic/placebo/CoreServer.class build/org/openstatic/util/JTextAreaOutputStream.class build/org/openstatic/util/JSONUtil.class build/org/openstatic/util/LiquidLogger.class build/org/openstatic/placebo/PlaceboPlugin.class build/org/openstatic/placebo/plugins/Clock.class build/org/openstatic/placebo/plugins/KeyStore.class $(JAR) -cvmf res/manifest.mf $@ -C build org -C build PlaceboBase.class www LICENSE clean: diff -r fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde -r fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 src/org/openstatic/http/PlaceboHttpServer.java --- a/src/org/openstatic/http/PlaceboHttpServer.java Sun Apr 10 19:44:44 2011 -0400 +++ b/src/org/openstatic/http/PlaceboHttpServer.java Thu May 05 02:49:03 2011 -0400 @@ -177,7 +177,9 @@ { ss = new ServerSocket(this.port); logln(this.myName,"Init on port " + String.valueOf(this.port)); - } catch (Exception n) {} + } catch (Exception n) { + logln(this.myName,"Init Failed on port " + String.valueOf(this.port) + " / " + n.getMessage()); + } logln(this.myName,"Entering Server Loop"); this.keep_running = true; while(this.keep_running) @@ -216,6 +218,11 @@ { this.debug_stream = new PrintStream(value); } + + public void setDebugStream(PrintStream value) + { + this.debug_stream = value; + } public PrintStream getDebugStream() { @@ -226,9 +233,7 @@ { if (this.debug_mode) { - SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); - String date_time = df.format(new Date()); - this.debug_stream.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(this.myName, 16) + " ] " + msg.replaceAll("\r","").replaceAll("\n","")); + this.debug_stream.println(msg.replaceAll("\r","").replaceAll("\n","")); } } @@ -236,9 +241,7 @@ { if (this.debug_mode) { - SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"); - String date_time = df.format(new Date()); - this.debug_stream.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(caption, 16) + " ] " + msg.replaceAll("\r","").replaceAll("\n","")); + this.debug_stream.println("[" + caption + "]" + msg.replaceAll("\r","").replaceAll("\n","")); } } diff -r fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde -r fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 src/org/openstatic/placebo/ControlPanel.java --- a/src/org/openstatic/placebo/ControlPanel.java Sun Apr 10 19:44:44 2011 -0400 +++ b/src/org/openstatic/placebo/ControlPanel.java Thu May 05 02:49:03 2011 -0400 @@ -51,6 +51,7 @@ import java.util.Hashtable; import java.util.Enumeration; import java.util.Properties; +import java.io.PrintStream; public class ControlPanel extends JFrame implements ActionListener { @@ -388,7 +389,7 @@ core.setWebRoot(root_field.getText()); core.setDebug(debug_field.isSelected()); core.setShowData(show_data_field.isSelected()); - core.setDebugStream(log_output); + core.getLiquidLogger().setDisplay(new PrintStream(log_output, true)); if (debug_field.isSelected()) { diff -r fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde -r fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 src/org/openstatic/placebo/CoreServer.java --- a/src/org/openstatic/placebo/CoreServer.java Sun Apr 10 19:44:44 2011 -0400 +++ b/src/org/openstatic/placebo/CoreServer.java Thu May 05 02:49:03 2011 -0400 @@ -22,6 +22,7 @@ import org.openstatic.http.HttpRequest; import org.openstatic.http.PlaceboSession; import org.openstatic.util.JSONUtil; +import org.openstatic.util.LiquidLogger; import java.io.File; import java.io.InputStream; @@ -47,12 +48,12 @@ private String web_root; private boolean debug; private boolean show_data; - private OutputStream debug_out; private boolean keep_running; private PlaceboHttpServer server; private Hashtable plugins; private Properties global_settings; private ControlPanel control_panel; + private LiquidLogger myLogger; public CoreServer() { @@ -68,7 +69,7 @@ { this.http_port = 80; this.web_root = "."; - this.debug_out = System.err; + this.myLogger = new LiquidLogger(System.err); this.keep_running = true; this.debug = false; this.show_data = false; @@ -126,15 +127,15 @@ PlaceboPlugin new_plugin = (PlaceboPlugin) cons.newInstance(); if (new_plugin.startup(this, path)) { - this.server.logln("Plugin Started (" + class_name + ")"); + this.myLogger.logln("Plugin Started (" + class_name + ")"); this.plugins.put(pl_path, new_plugin); - this.server.logln("Mounted (" + class_name + ") at " + pl_path); + this.myLogger.logln("Mounted (" + class_name + ") at " + pl_path); } else { - this.server.logln("Plugin (" + class_name + ") Failed to Startup"); + this.myLogger.logln("Plugin (" + class_name + ") Failed to Startup"); } this.pluginChange(); } catch (Exception e) { - this.server.logln("Couldn't Init (" + class_name + ") at " + pl_path + " because " + e.toString() + " / " + e.getMessage()); + this.myLogger.logln("Couldn't Init (" + class_name + ") at " + pl_path + " because " + e.toString() + " / " + e.getMessage()); } } @@ -165,6 +166,11 @@ return this.global_settings; } + public LiquidLogger getLiquidLogger() + { + return this.myLogger; + } + private String findPluginPath(String path) { for (Enumeration e = this.plugins.keys(); e.hasMoreElements(); ) @@ -188,7 +194,7 @@ return this.plugins.get(plugin_key); } } - this.server.logln("Did not find plugin for (" + path + ")"); + this.myLogger.logln("Did not find plugin for (" + path + ")"); return null; } @@ -211,12 +217,7 @@ { this.web_root = value; } - - public void setDebugStream(OutputStream value) - { - this.debug_out = value; - } - + public void quit() { this.keep_running = false; @@ -226,9 +227,9 @@ PlaceboPlugin this_plugin = this.plugins.get(plugin_key); if (this_plugin.shutdown()) { - this.server.logln("Shutdown Plugin (" + this_plugin.toString() + ") OK!"); + this.myLogger.logln("Shutdown Plugin (" + this_plugin.toString() + ") OK!"); } else { - this.server.logln("Shutdown Plugin (" + this_plugin.toString() + ") Plugin failed to shutdown!"); + this.myLogger.logln("Shutdown Plugin (" + this_plugin.toString() + ") Plugin failed to shutdown!"); } } server.shutdown(); @@ -245,7 +246,7 @@ // Create new server object server.setDebug(debug); server.setShowData(show_data); - server.setDebugStream(debug_out); + server.setDebugStream(this.myLogger.getPrintStream()); server.setPort(this.http_port); // Launch thread. server.start(); @@ -261,7 +262,7 @@ PlaceboPlugin plugin = findPlugin(request.getPath()); if (plugin != null) { - server.logln("Found Plugin for [" + request.getPath() + "]"); + myLogger.logln("Found Plugin for [" + request.getPath() + "]"); String plugin_path_prefix = findPluginPath(request.getPath()); String local_path = request.getPath().substring(plugin_path_prefix.length()-1); request.setRequestPrefix(plugin_path_prefix); @@ -284,7 +285,7 @@ // create new response object HttpResponse response = new HttpResponse(); String path = web_root + request.getPath(); - server.logln("Possible Resource [" + path + "]"); + this.myLogger.logln("Possible Resource [" + path + "]"); // this wacky thing is for finding out if we are inside a zip // and retrieving the proper content. StringTokenizer path_tokens = new StringTokenizer(path, "/"); @@ -346,8 +347,8 @@ { String ftype_upper = in_file_type.toUpperCase(); String zip_item = resolve_index(file_internal_path); - server.logln(ftype_upper + " File: " + in_file_path); - server.logln(ftype_upper + " Resource: " + zip_item); + myLogger.logln(ftype_upper + " File: " + in_file_path); + myLogger.logln(ftype_upper + " Resource: " + zip_item); ZipFile zipfile = new ZipFile(new File(in_file_path)); ZipEntry ze = zipfile.getEntry(zip_item); if (ze != null) @@ -363,8 +364,8 @@ file_internal_path += "?" + request.getRawGet(); // start proxying - server.logln("INI File: " + in_file_path); - server.logln("INI Resource: " + file_internal_path); + myLogger.logln("INI File: " + in_file_path); + myLogger.logln("INI Resource: " + file_internal_path); Properties pini = new Properties(); pini.load(new FileInputStream(new File(in_file_path))); String proxy_url = pini.getProperty("proxy_url"); @@ -375,19 +376,19 @@ { String proxy_path = proxy_url + file_internal_path; - server.logln("Proxy Path: " + proxy_path); + myLogger.logln("Proxy Path: " + proxy_path); response.setData(new URL(proxy_path)); } else if (buffered_proxy_url != null) { String proxy_path = buffered_proxy_url + file_internal_path; - server.logln("Buffered Proxy Path: " + proxy_path); + myLogger.logln("Buffered Proxy Path: " + proxy_path); response.setBufferedData(new URL(proxy_path)); } else if (json_to_html_url != null) { String proxy_path = json_to_html_url + file_internal_path; - server.logln("JSON to HTML Path: " + proxy_path); + myLogger.logln("JSON to HTML Path: " + proxy_path); URL json_loc = new URL(proxy_path); URLConnection urlc = json_loc.openConnection(); InputStream source_json = urlc.getInputStream(); @@ -399,7 +400,7 @@ } else { // we just have a normal file to serve String file_final_path = resolve_index(new_path.toString()); - server.logln("File: " + file_final_path); + myLogger.logln("File: " + file_final_path); File returnFile = new File(file_final_path); if (returnFile.exists()) { @@ -409,7 +410,7 @@ } } } catch (Exception n) { - server.logln("Exception: " + n.getMessage()); + myLogger.logln("Exception: " + n.getMessage()); //n.printStackTrace(System.err); } } else { @@ -425,6 +426,6 @@ } } } - server.logln("Core Server Loop Ended"); + myLogger.logln("Core Server Loop Ended"); } } diff -r fe0a8cdc0e82c93ab1ab1fa803b5967e3cfe9bde -r fb2f5e689d8d0aca72aeedc6fea406f650dc60e4 src/org/openstatic/util/LiquidLogger.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/openstatic/util/LiquidLogger.java Thu May 05 02:49:03 2011 -0400 @@ -0,0 +1,111 @@ +package org.openstatic.util; + +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; +import java.util.Date; +import java.util.StringTokenizer; +import java.text.SimpleDateFormat; + +public class LiquidLogger +{ + private PrintStream inbound_ps; + private ByteArrayOutputStream inbound; + private PrintStream outbound; + private String caption; + + public LiquidLogger(PrintStream display) + { + this.inbound = new ByteArrayOutputStream(); + this.inbound_ps = new PrintStream(inbound,true); + this.outbound = display; + this.caption = ""; + Thread t = new Thread() + { + public void run() + { + while(true) + { + if (LiquidLogger.this.inbound.size() > 0 && LiquidLogger.this.outbound != null) + { + StringTokenizer st = new StringTokenizer(LiquidLogger.this.inbound.toString(), "\n"); + LiquidLogger.this.inbound.reset(); + while (st.hasMoreTokens()) + { + 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 = LiquidLogger.this.caption; + if (msg.startsWith("[") && msg.contains("]")) + { + temp_caption = msg.substring(msg.indexOf("[")+1, msg.indexOf("]")); + msg = msg.substring(msg.indexOf("]")+1); + } + LiquidLogger.this.outbound.println("<" + setSizedString(date_time,23) + "> [ " + setSizedString(temp_caption, 16) + " ] " + msg.replaceAll("\r","")); + } + } + try + { + Thread.sleep(10); + } catch (Exception thread_sleep) {} + } + } + }; + t.start(); + } + + public void setDisplay(PrintStream display) + { + this.outbound = display; + } + + public void setCaption(String caption) + { + this.caption = caption; + } + + public PrintStream getPrintStream() + { + return this.inbound_ps; + } + + public void println(String text) + { + this.inbound_ps.println(text); + } + + public void logln(String text) + { + this.inbound_ps.println(text); + } + + 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(); + } +}