package org.gametrack.util; import java.io.*; import java.util.Vector; /** *
Title: BFTracker
*Description: Tracks online Battlefield 1942 games, outputs game records
*Copyright: Copyright (c) 2003
*Company: bf1942.gametrack.org
* @author Brian Cairns * @version 1.0 */ // debugging functions public class Debug { // table to convert a nibble to a hex char. static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public static int sizeOf( Serializable obj ) { // serializes the object to a byte array and reports its length (for an estimate of how much space the object takes up in memory) try { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); ObjectOutputStream ooStream = new ObjectOutputStream( byteStream ); ooStream.writeObject( obj ); // MUST flush output stream before converting to byte array ( close() will flush it ) ooStream.close(); return byteStream.size(); } catch( Exception x ) { Log.err( "Debug.sizeOf() - " + x.toString() ); } return -1; } // convert a byte to a 2-digit hex code public static String byte2Hex( byte b ) { return String.valueOf( hexChar[ ( b & 0xf0 ) >>> 4] ) + // look up high nibble char hexChar[ b & 0x0f ]; // look up low nibble char } /** * append a 2-digit hex code to the given buffer * @param b * @param buf */ public static StringBuffer appendHex( byte b, StringBuffer buf ) { return buf.append( hexChar[ ( b & 0xf0 ) >>> 4] ).append( hexChar[ b & 0x0f ] ); } /** * Prints a backtrace /** * Prints a backtrace * @param steps the number of methods to backtrace */ public static void backtrace( final int steps ) { try { throw new Exception(); } catch ( Exception x ) { synchronized ( System.out ) { System.out.print( "BACKTRACE(" + steps + ") " ); x.printStackTrace( new PrintWriter( new CharArrayWriter() ) { int count = -1; public void println( char[] ch ) { println( new String( ch ) ); } public void println( String str ) { if ( count >= 0 & count++ <= steps ) { System.out.println( str.trim() ); } } } ); } } } /** * Prints thread info? */ public static void printThreads() { PrintStream out = null; Thread ta[] = new Thread[Thread.activeCount()]; Vector names = new Vector(), count = new Vector(); String stName; StringBuffer sbSummary = new StringBuffer( "*** Threads: " ); int pos, n; try { n = Thread.enumerate( ta ); for ( int i = 0; i < n; i++ ) { stName = ta[i].getClass().getName(); if ( ( pos = names.indexOf( stName ) ) == -1 ) { names.add( stName ); count.add( new Integer( 1 ) ); } else { count.set( pos, new Integer( ( ( Integer ) count.get( pos ) ).intValue() + 1 ) ); } Log.debug( "*** #" + i + ": " + ta[i].getName() + ", P" + ta[i].getPriority() + " - " + stName + " - " + ta[i].toString() ); } for ( int i = 0; i < names.size(); i++ ) sbSummary.append( names.get( i ) + "-" + count.get( i ) + " " ); // log( sbSummary.toString() ); } catch ( Exception x ) { Log.err( "Debug.printThreads(): Error writing thread dump file - " + x.toString() ); } } }