Software Programming

Kunuk Nykjaer

Simple debug framework example in Java

leave a comment »


Just a simple your own quick and dirty debug/log framework.
Ability to turn on/off debug prints and setting debugging level.
Implemented in Java

D.java

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author Kunuk Nykjaer
 * DebugFramework
 */

public class D {
	private static boolean debug = true;
	public final static int DEBUG_LEVEL_FATAL = 7;
	public final static int DEBUG_LEVEL_ERROR = 6;
	public final static int DEBUG_LEVEL_WARNING = 5;
	public final static int DEBUG_LEVEL_INFO = 4;
	public final static int DEBUG_LEVEL_FINE = 3;
	public final static int DEBUG_LEVEL_FINER = 2;
	public final static int DEBUG_LEVEL_FINEST = 1;
	private static int debugLevel = DEBUG_LEVEL_INFO; // default
	private static long begin = System.currentTimeMillis();	
	private final static NumberFormat nf = new DecimalFormat("#.##");
	
	public static void setDebugLevel(int level)
	{
		debugLevel = level;
	}
	public static void setDebug(boolean b)
	{
		debug = b;
	}
	public static void resetTime()
	{
		begin = System.currentTimeMillis();
	}
	
	
	public static void debug(Object o)
	{
		debug(o, DEBUG_LEVEL_INFO); //default
	}
	public static void debug(Object o, int level)
	{
		
		if(debug && debugLevel <= level){
			double secs = (System.currentTimeMillis() - begin) / 1000.0;
			String time =  nf.format(secs) + " sec: ";
			System.out.println("debug "+time +o);
		}
	}
}

Test.java

public class Test {
		public static void main(String[] args) {

			D.setDebug(true);
			D.resetTime();

			String one = "one";
			String two = "two";
			String three = "three";	

			D.setDebugLevel(D.DEBUG_LEVEL_INFO);
			System.out.println("-- DEBUG_LEVEL_INFO");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);	
			
			D.setDebugLevel(D.DEBUG_LEVEL_WARNING);
			System.out.println("-- DEBUG_LEVEL_WARNING");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);

			D.setDebugLevel(D.DEBUG_LEVEL_ERROR);
			System.out.println("-- DEBUG_LEVEL_ERROR");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);	

			// turn off debug
			D.setDebug(false);
			System.out.println("-- DEBUG set to false");
			D.debug(one);	// implicit INFO level
			D.debug(two, D.DEBUG_LEVEL_WARNING);
			D.debug(three, D.DEBUG_LEVEL_ERROR);
		}		
}

Result:

— DEBUG_LEVEL_INFO
debug 0 sec: one
debug 0 sec: two
debug 0 sec: three
— DEBUG_LEVEL_WARNING
debug 0 sec: two
debug 0 sec: three
— DEBUG_LEVEL_ERROR
debug 0 sec: three
— DEBUG set to false

Advertisements

Written by kunuk Nykjaer

September 28, 2010 at 12:50 am

Posted in Framework, Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: