Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Static Error/Debug Outputter  (Read 1435 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Posted 2006-01-24 09:11:40 »

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. Smiley [EDIT]Made it better according to suggestions.[/EDIT]

Anyway, I hope you guys find this useful.

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  
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) {}
   }
}

See my work:
OTC Software
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #1 - Posted 2006-01-24 17:10:17 »

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

Offline tusaki

Junior Member




In a mad world only the mad are sane.


« Reply #2 - Posted 2006-01-24 17:47:48 »

Or instead of

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


do

1  
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 Smiley too few people take the time to share things they find usefull. So I am looking forward to more ^_^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #3 - Posted 2006-01-24 17:57:50 »

Logging built into java is a bit unwieldy though Smiley

Kev

Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #4 - Posted 2006-01-24 18:00:34 »

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

See my work:
OTC Software
Offline tusaki

Junior Member




In a mad world only the mad are sane.


« Reply #5 - Posted 2006-01-24 18:03:31 »

Logging built into java is a bit unwieldy though Smiley

Kev

unwieldy? Is that another word for flexibleGrin It's not so bad actually...
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #6 - Posted 2006-01-24 19:41:51 »

Smiley Smiley 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 Smiley

Kev

Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #7 - Posted 2006-01-24 20:09:05 »

Or instead of

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


do

1  
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 Smiley too few people take the time to share things they find usefull. So I am looking forward to more ^_^

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.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (126 views)
2013-05-17 21:29:12

alaslipknot (134 views)
2013-05-16 21:24:48

gouessej (165 views)
2013-05-16 00:53:38

gouessej (157 views)
2013-05-16 00:17:58

theagentd (169 views)
2013-05-15 15:01:13

theagentd (154 views)
2013-05-15 15:00:54

StreetDoggy (198 views)
2013-05-14 15:56:26

kutucuk (222 views)
2013-05-12 17:10:36

kutucuk (221 views)
2013-05-12 15:36:09

UnluckyDevil (225 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.165 seconds with 20 queries.