Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1721 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2006-01-24 08: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

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2006-01-24 16: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


Medals: 1


In a mad world only the mad are sane.


« Reply #2 - Posted 2006-01-24 16: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

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


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

Logging built into java is a bit unwieldy though Smiley

Kev

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


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

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

See my work:
OTC Software
Offline tusaki

Junior Member


Medals: 1


In a mad world only the mad are sane.


« Reply #5 - Posted 2006-01-24 17: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

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2006-01-24 18: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2006-01-24 19: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.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!