krysalis-update/src/java/org/krysalis/depot/common/util/debug Dumpable.java,NONE,1.1 DebugUtils.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:31 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/common/util/debug
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/common/util/debug

Added Files:
	Dumpable.java DebugUtils.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: Dumpable.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.common.util.debug;

import java.io.PrintWriter;

/**
 * TODO: what is this class for ?
 * @author $Author: chalko $
 * @version $Revision: 1.1 $
 * 
 */
public interface Dumpable {
    void dump(PrintWriter out, int depth, boolean verbose);
}

--- NEW FILE: DebugUtils.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.common.util.debug;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.SystemUtils;
import org.krysalis.depot.common.util.Triple;

/**
 * @version $Revision: 1.1 $
 */
public class DebugUtils {

	private static Hashtable l_indents = new Hashtable();

	private static boolean l_verbose = false;

	public static void enableXercesParser() {
		// SAX ...
		System.setProperty(
			"org.xml.sax.driver",
			"org.apache.xerces.parsers.SAXParser");

		// DOM...
		//:TODO:
		//javax.xml.parsers.DocumentBuilderFactory 
	}

	public static void enableCommonsLogging() {
		System.setProperty(
			"org.apache.commons.logging.Log",
			"org.apache.commons.logging.impl.SimpleLog");

		System.setProperty(
			"org.apache.commons.logging.simplelog.showdatetime",
			"true");
	}

	public static void enableHttpClientDebug() {

		enableCommonsLogging();

		System.setProperty(
			"org.apache.commons.logging.simplelog.log.httpclient.wire",
			"debug");

		System.setProperty(
			"org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
			"debug");
	}

	public static void enableVerboseDumps() {
		l_verbose = true;
	}

	public static void indent(PrintWriter out, int x) {
		for (int i = 0; i < x; ++i) {
			out.print("  ");
		}
	}

	public static String getIndent(int x) {
		String idstr = (String) l_indents.get(new Integer(x));

		if (null == idstr) {
			StringBuffer indent = new StringBuffer();

			for (int i = 0; i < x; ++i)
				indent.append("  ");

			idstr = indent.toString();

			l_indents.put(new Integer(x), idstr);
		}

		return idstr;
	}

	public static String getDump(Throwable t) {
		return getDump(t, l_verbose);
	}

	public static String getDump(Throwable t, boolean verbose) {
		if (verbose) {
			StringWriter sw = new StringWriter();

			PrintWriter pw = new PrintWriter(sw, true);
			t.printStackTrace(pw);
			pw.flush();
			return sw.toString();
		}

		return t.getLocalizedMessage();
	}

	public static String getDump(Dumpable d) {
		return getDump(0, null, d, l_verbose);
	}

	public static String getDump(Dumpable d, boolean verbose) {
		return getDump(0, null, d, verbose);
	}

	public static String getDump(String title, Dumpable d) {
		return getDump(0, title, d, l_verbose);
	}

	public static String getDump(String title, Dumpable d, boolean verbose) {
		return getDump(0, title, d, verbose);
	}

	public static String getDump(int depth, String title, Dumpable d) {
		return getDump(depth, title, d, l_verbose);
	}

	public static String getDump(
		int depth,
		String title,
		Dumpable d,
		boolean verbose) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		dump(pw, depth, title, verbose, d);
		pw.flush();
		return sw.toString();
	}

	public static void dump(PrintWriter out, boolean verbose, Object d) {
		dump(out, 0, verbose, d);
	}

	public static void dump(String title, Dumpable d) {
		dump(SystemUtils.getSystemOut(), 0, title, l_verbose, d);
	}

	public static void dump(String title, Object o) {
		dump(SystemUtils.getSystemOut(), 0, title, l_verbose, o);
	}

	public static void dump(String title, boolean verbose, Dumpable d) {
		dump(SystemUtils.getSystemOut(), 0, title, verbose, d);
	}

	public static void printSeparator() {
		printSeparator(SystemUtils.getSystemOut(), 0);
	}
	public static void printSeparator(PrintWriter out, int depth) {
		out.print(DebugUtils.getIndent(depth));
		out.println(
			"--------------------------------------------------------------");
	}

	//	public static void dump(Dumpable d) {
	//		dump(System.out, 0, d, l_verbose);
	//	}
	public static void dump(Object d) {
		dump(SystemUtils.getSystemOut(), d);
	}

	public static void dump(boolean verbose, Dumpable d) {
		dump(SystemUtils.getSystemOut(), 0, null, verbose, d);
	}

	public static void dump(
		PrintStream out,
		int depth,
		Dumpable d,
		boolean verbose) {
		dump(new PrintWriter(out, true), depth, d, verbose);
	}

	public static void dump(
		PrintStream out,
		int depth,
		String title,
		boolean verbose,
		Dumpable d) {
		dump(new PrintWriter(out, true), depth, title, verbose, d);
	}

	public static void dump(boolean verbose, Object o) {
		dump(SystemUtils.getSystemOut(), 0, null, verbose, o);
	}

	public static void dump(PrintWriter out, Object o) {
		dump(out, 0, null, l_verbose, o);
	}

	public static void dump(
		PrintWriter out,
		int depth,
		boolean verbose,
		Object o) {
		dump(out, depth, null, verbose, o);
	}

	public static void dump(
		PrintWriter out,
		int depth,
		String title,
		boolean verbose,
		Object o) {
		final String indent = DebugUtils.getIndent(depth);

		if (null == o) {
			if (null != title) {
				out.print(indent);
				out.println(title);
			}

			out.print(indent);
			out.println("Null Object");
		}
		else if (o instanceof Dumpable) {
			if (null != title) {
				out.print(indent);
				out.println(title);
			}
			dump(out, depth, (Dumpable) o, verbose);
		}
		else if (o instanceof Map)
			dumpMap(out, depth, title, verbose, ((Map) o));
		else if (o instanceof List)
			dumpList(out, depth, title, verbose, ((List) o));
		else {
			if (null != title) {
				out.print(indent);
				out.println(title);
			}

			out.print(indent);
			out.print(SystemUtils.getShortName(o.getClass()));
			out.print(" : ");
			out.println(o.toString());
		}
	}

	//	public static void dump(PrintWriter out, Dumpable d) {
	//		dump(out, 0, d, l_verbose);
	//	}

	public static void dump(PrintWriter out, Dumpable d, boolean verbose) {
		dump(out, 0, d, verbose);
	}

	public static void dump(PrintWriter out, int depth, Dumpable d) {
		if (null != d)
			d.dump(out, depth, l_verbose);
		else {
			out.println(DebugUtils.getIndent(depth) + "NULL");
		}
	}

	public static void dump(
		PrintWriter out,
		int depth,
		Dumpable d,
		boolean verbose) {
		if (null != d)
			d.dump(out, depth, verbose);
		else {
			out.println(DebugUtils.getIndent(depth) + "NULL");
		}
	}

	public static void dump(
		PrintWriter out,
		int depth,
		String title,
		boolean verbose,
		Dumpable d) {
		final String indent = DebugUtils.getIndent(depth);

		if (null != title) {
			out.print(indent);
			out.println(title);
		}
		if (null != d)
			d.dump(out, depth, verbose);
		else {
			out.print(indent);
			out.println("NULL");
		}
	}

	//	public static void dumpList(PrintWriter out, int depth, List list) {
	//		dumpList(out, depth, null, l_verbose, list);
	//	}

	public static void dumpList(
		PrintWriter out,
		int depth,
		boolean verbose,
		List list) {
		dumpList(out, depth, null, verbose, list);
	}

	public static void dumpList(
		PrintWriter out,
		int depth,
		String title,
		boolean verbose,
		List list) {
		String indent = DebugUtils.getIndent(depth);
		String indent1 = DebugUtils.getIndent(depth + 1);

		if (null != title) {
			out.print(indent);
			out.println(title);
		}
		else {
			out.print(indent);
			out.println("List Type: " + SystemUtils.getShortName(list));
		}

		if (null != list) {
			if (0 != list.size()) {
				int ii = 0;
				for (Iterator i = list.iterator(); i.hasNext(); ++ii) {
					Object entry = i.next();

					out.print(indent1);
					out.print("Entry #");
					out.print(ii);
					if (entry instanceof Dumpable) {
						out.print(", class: ");
						out.println(entry.getClass().getName());
					}
					else
						out.print(" ");

					DebugUtils.dump(out, depth + 2, verbose, entry);

					// Separator for entries
					//if (i.hasNext() && verbose)
					//	DebugUtils.printSeparator(out,depth);
				}
			}
			else {
				out.print(indent);
				out.println("Empty List.");
			}
		}
		else {
			out.print(indent);
			out.println("No List.");
		}
	}

	public static void dumpMap(
		PrintWriter out,
		int depth,
		boolean verbose,
		Map map) {
		dumpMap(out, depth, null, verbose, map);
	}

	public static void dumpMap(
		PrintWriter out,
		int depth,
		String title,
		boolean verbose,
		Map map) {
		String indent = DebugUtils.getIndent(depth);
		String indent1 = DebugUtils.getIndent(depth + 1);

		if (null != title) {
			out.print(indent);
			out.println(title);
		}
		else {
			out.print(indent);
			out.println("Map Type: " + SystemUtils.getShortName(map));
		}

		if (null != map) {
			if (0 != map.size()) {
				for (Iterator i = map.keySet().iterator(); i.hasNext();) {
					Object key = i.next();
					Object entry = map.get(key);

					out.print(indent1);
					out.print("Key ");
					out.print(key);
					if (entry instanceof Dumpable) {
						out.print(", class: ");
						out.println(entry.getClass().getName());
					}
					else
						out.print(" ");

					DebugUtils.dump(out, depth + 1, verbose, entry);

					// Separator for entries
					//if (i.hasNext() && verbose)
					//	DebugUtils.printSeparator(out,depth);
				}
			}
			else {
				out.print(indent);
				out.println("Empty Map.");
			}
		}
		else {
			out.print(indent);
			out.println("No Map.");
		}
	}

	public static void logMemoryInformation() {
		logMemoryInformation(null);
	
	}
	
		public static void logMemoryInformation(String context) {
		
		Triple info = SystemUtils.getMemoryInformation();
		
		StringBuffer minfo = new StringBuffer();

		minfo.append("Memory Info: ");
		minfo.append("Total: ");
		minfo.append(info.getFirst());
		minfo.append(", Free: ");
		minfo.append(info.getSecond());
		minfo.append(", Max: ");
		minfo.append(info.getThird());
		minfo.append(".");
		
		if (null != context ){
			minfo.append(" @ ");
			minfo.append(context);
		}

		String message = minfo.toString();
		Logger.getLogger().info(message);
		System.out.println(message);		
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/