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

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:
1
| 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| 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; 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; } public void close() { outputStream.println("</body>"); outputStream.println("</html>"); outputStream.close(); } public static boolean event(String message, int type, int level) { getInstance().logEvent(message, type, level); return true; } 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><br>"); break; } case EVENT: { outputStream.println(" "+message+"<br>"); 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><br>"); 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><br>"); break; } default: { System.out.println("### "+ message +" ###<br>"); 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..