Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  Singleton Html log file  (Read 1890 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Posted 2003-01-20 17:24:27 »

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  Grin

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  Smiley

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  Wink

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;
     
     
      // ----------------------------------------- //
     //         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><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..

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline augusto

Senior Newbie





« Reply #1 - Posted 2003-01-21 03:52:53 »

Well, 1.4 already has a logger API. And if you don't like that one, there's always log4j.

http://jakarta.apache.org/log4j/docs/

I did a similar log class like yours, of course, it's not obsolete. :-)
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.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!