Newer
Older
hanabi-networking / src / server / Utils.java
package server;

import java.io.PrintStream;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
	public static final int ERROR = 1;
	public static final int WARNING = 2;
	public static final int DEBUG = 3;
	public static final int INFO = 4;
	public static final int VERBOSE = 5;

	private static PrintStream logStream;
	private static int logLevel;
	private static boolean logTimeStamp;

	public static void init(PrintStream log, int lLevel, boolean timeStamp) {
		logStream = log;
		logLevel = lLevel;
		logTimeStamp = timeStamp;
	}

	/* Output errors are silently ignored, because Java sucks... TODO? */
	public static void log(int level, String msg) {
		PrintStream str = logStream;
		if(str == null) return; // TODO: use System.err in this case?
		if(level > logLevel) return;
		if(logTimeStamp) {
			SimpleDateFormat fmt = new SimpleDateFormat("[HH:mm:ss]: ");
			str.print( fmt.format(new Date()) );
		}
		str.println(msg);
	}

	public static void sleep(long ms) {
		try {
			Thread.sleep(ms);
		} catch(InterruptedException ex) {
			log(INFO, Msgs.sleepInterrupted());
		}
	}
}