I made this a couple of weeks ago and I’ve found it to be really handy, so I thought i’d let others use/abuse it if they wanted as well ;D
It’s a singleton-esque log file, that outputs into Html form so important error messages can stand out more obviously than a straight text dump. Use is nice and simple:
Log.event("Your event here", Log.EVENT, Log.PRIORITY_HIGH);
This automatically creates and opens the file if needed and writes the string. The message type (initialisation, error, etc.) follows, and then the priority (high, normal or low). Text is coloured according to the message type and priority (shades of green for initialisation, bold red/orange for error types, blue for info and black for anything else). Debug messages are currently re-routed untouched to System.out for realtime debugging. Also, you can include the above code in an assert() to add extra event logging in a debug build
About the only thing you’ve got to remember to do is call getInstance().close() before you exit your program, but if you’re willing to put up with a little broken html you don’t even have to do that
package PrismEngine;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import OrangyTang.OpenGLToolkit.*;
public class Log
{
public static final int DEBUG = 0;
public static final int INIT = 1;
public static final int EVENT = 2;
public static final int INFO = 3;
public static final int ERROR = 4;
public static final int PRIORITY_LOW = 6;
public static final int PRIORITY_NORMAL = 7;
public static final int PRIORITY_HIGH = 8;
private PrintStream outputStream;
// ----------------------------------------- //
// Singleton implementation //
private static Log singleton;
private Log()
{
try
{
FileOutputStream fOut = new FileOutputStream("./Log.html");
outputStream = new PrintStream(fOut);
outputStream.println("<html>");
outputStream.println("<head> <title>Engine log file</title> </head>");
outputStream.println("<body bgcolor=\"#D0D0DD\">");
outputStream.println("<font size=\"+3\">");
outputStream.println("<p align=\"center\">Engine log file</p>");
outputStream.println("</font>");
}
catch (Exception e)
{
System.out.println("Error creating log file: "+e);
}
}
public static Log getInstance()
{
if (singleton == null)
{
singleton = new Log();
}
return singleton;
}
// End of singleton implementation //
// ----------------------------------------- //
public void close()
{
outputStream.println("</body>");
outputStream.println("</html>");
outputStream.close();
}
// Log.event("Some message here", Log.DEBUG)
public static boolean event(String message, int type, int level)
{
getInstance().logEvent(message, type, level);
return true; // Allows use in assert() to give extra messages in a debug build
}
private void logEvent(String message, int type, int level)
{
switch (type)
{
case DEBUG:
{
System.out.println(message);
break;
}
case INIT:
{
switch(level)
{
case PRIORITY_LOW:
{
outputStream.println("<font color=\"#005E00\">");
break;
}
case PRIORITY_NORMAL:
{
outputStream.println("<font color=\"#009000\">");
break;
}
case PRIORITY_HIGH:
{
outputStream.println("<font color=\"#00C000\">");
break;
}
default:
{
assert(false);
break;
}
}
outputStream.println("<i>"+message+"</i></font>
");
break;
}
case EVENT:
{
outputStream.println(" "+message+"
");
break;
}
case INFO:
{
switch(level)
{
case PRIORITY_LOW:
{
outputStream.println("<font color=\"#00005B\">");
break;
}
case PRIORITY_NORMAL:
{
outputStream.println("<font color=\"#00008A\">");
break;
}
case PRIORITY_HIGH:
{
outputStream.println("<font color=\"#0000D1\">");
break;
}
default:
{
assert(false);
break;
}
}
outputStream.println(" "+message+"</font>
");
break;
}
case ERROR:
{
switch(level)
{
case PRIORITY_LOW:
{
outputStream.println("<font color=\"#FFFF00\">");
break;
}
case PRIORITY_NORMAL:
{
outputStream.println("<font color=\"#FF8000\">");
break;
}
case PRIORITY_HIGH:
{
outputStream.println("<font color=\"#FF0000\">");
break;
}
default:
{
assert(false);
break;
}
}
outputStream.println("<b>"+message+"</b></font>
");
break;
}
default:
{
System.out.println("### "+ message +" ###
");
assert(false);
break;
}
}
}
}
Its not the best example of clean code, but its more than adaquate for my needs. I was toying with the idea of outputting xml and doing the formatting with a xsl transformation for something a bit more flexible, but that could easily be changed if someone wanted to…