Static Error/Debug Outputter

I find myself creating this code over and over again, so I decided to create a static class that can do it for you. Basically, it lets you pass either a string or an exception to a method that prints this to an error log. It’s very useful in almost all games as a means of reporting debug information and errors without flooding the screen, and giving the user the ability to send the information to you. I’ll probably make something that sends error reports automatically as well, but I haven’t made one yet so I’m not posting it, obviously. :slight_smile: [EDIT]Made it better according to suggestions.[/EDIT]

Anyway, I hope you guys find this useful.


import java.io.PrintWriter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class ErrorLogger
{
	public static void log(Exception e)
	{        
		try
		{
			PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt",true)));
			e.printStackTrace(writer);
			writer.close();
		}
		catch (Exception ex) {}
	}
	
	public static void log(String e)
	{
		try
		{
			PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt",true)));
			writer.println(e);
			writer.close();
		}
		catch (Exception ex) {}
	}
}

Why does it read the entire contents of the file first before writing a line out? Maybe I’m just being stupid. Wouldn’t it be easier to just keep the stream open while the game is running?

This is what I use which is sort of similar:

http://www.cokeandcode.com/code/src/util/org/newdawn/util/Log.java

Kev

Or instead of


PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt")));

do


PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt", true)));

basically you tell the fileoutputstream to append to the end instead. (see http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.io.File,%20boolean))

and java from version 1.4 onward has logging build in:

http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/.

I hope I’m not too annoying with this, I mean, please please please share your code, don’t let me get to you :slight_smile: too few people take the time to share things they find usefull. So I am looking forward to more :slight_smile:

Logging built into java is a bit unwieldy though :slight_smile:

Kev

Because it’s a quick ohdge podge job. Okay, I’ll change it.

unwieldy? Is that another word for flexible? ;D It’s not so bad actually…

:slight_smile: :slight_smile: Yeah, I use it for enterprise stuff thats large scale, but when you’re working with a relatively simple game that doesn’t really require logging levels, potentially to log to lots of different places, ability to tween the log etc… i just go for something simple :slight_smile:

Kev

Hot damn, I had no idea about that incredibly useful function. I’ve always rewritten the entire file because I’ve only had to worry about it when the user presses Control-S, in which case a short load time is certainly expected. It serves me right for not learning all this stuff via the formal methods. Thanks a lot for the help guys, and I will definitely keep contributing code I’ve made that is useful, despite some constructive criticism. I don’t think I’ll ever consider myself along the same levels of expertise as some of the people on this site, so no doubt my code can always be improved.

Thanks for the help and comments.