KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > log > ScreenLogger


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * The author may be contacted at theit@gmx.de.
21  *
22  *
23  * $Id: ScreenLogger.java,v 1.2 2002/04/23 11:18:39 glurk Exp $
24  */

25 package net.sf.javaguard.log;
26
27 import java.io.PrintWriter JavaDoc;
28
29
30
31
32 /** Represents a simple Logger that prints logging messages to the screen or
33  * a given print writer.
34  *
35  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
36  */

37 public class ScreenLogger implements Log {
38   /** Holds the logging level. */
39   private int logLevel;
40   
41   
42   /** Holds the current instance of the logger. */
43   private static ScreenLogger theInstance = null;
44   /** A string containing whitespace characters. Used when printing messages. */
45   private static String JavaDoc whitespaces = " ";
46   
47   
48   
49   
50   /** Returns the current instance of the logger.
51    * @return current instance of the screen logger
52    */

53   public static ScreenLogger getInstance() {
54     if (null == theInstance) {
55       theInstance = new ScreenLogger();
56     }
57     return theInstance;
58   }
59   
60   
61   
62   
63   /** Private constructor that initializes the logger. */
64   private ScreenLogger() {
65     setLoggingLevel(NORMAL);
66   }
67   
68   
69   
70   
71   /** Sets the logging level. Future log messages must have a logging level
72    * equal or higher to the one specified in the parameter.
73    * @param level the new logging level; one of the constants
74    * <code>NORMAL</code>, <code>INFO</code>, <code>VERBOSE</code> or <code>DEBUG</code>
75    * @see #getLoggingLevel
76    */

77   public void setLoggingLevel(int level) {
78     logLevel = level;
79   }
80   
81   
82   /** Returns the current logging level.
83    * @return the current logging level
84    */

85   public int getLoggingLevel() {
86     return logLevel;
87   }
88   
89   
90   /** Increment the current logging level to be more verbose.
91    */

92   public void incrementLoggingLevel() {
93     setLoggingLevel(getLoggingLevel() + 1);
94   }
95   
96   
97   
98   
99   /** Prints an empty line to the logger using the <code>NORMAL</code> logging
100    * level.
101    * @see #log
102    */

103   public void println() {
104     println("");
105   }
106   
107   
108   /** Prints the given string to the logger using the <code>NORMAL</code>
109    * logging level and terminates the current line.
110    * @param msg the message to log
111    */

112   public void println(String JavaDoc msg) {
113     log(msg);
114   }
115   
116   
117   /** Prints the given string to the logger using the <code>NORMAL</code>
118    * logging level.
119    * @param msg the message to log
120    */

121   public void print(String JavaDoc msg) {
122     log(NORMAL, msg, false);
123   }
124   
125   
126   /** Prints the given string to the logger using the <code>NORMAL</code>
127    * logging level and terminates the current line.
128    * @param msg the message to log
129    */

130   public void log(String JavaDoc msg) {
131     log(NORMAL, msg);
132   }
133   
134   
135   /** Prints a logging message and terminates the line if the specified logging
136    * level is lower or equal than the current logging level.
137    * @param level the logging level
138    * @param msg the log message
139    */

140   public void log(int level, String JavaDoc msg) {
141     log(level, msg, true);
142   }
143   
144   
145   /** Prints a logging message and terminates the line if the specified logging
146    * level is lower or equal than the current logging level.
147    * @param level the logging level
148    * @param msg the log message
149    * @param lineFeed true if the output line is terminated; false else
150    */

151   public void log(int level, String JavaDoc msg, boolean lineFeed) {
152     if (getLoggingLevel() >= level) {
153       if (level > 0) {
154         if (level > whitespaces.length()) {
155           level = whitespaces.length();
156         }
157         System.out.print(whitespaces.substring(0, level));
158       }
159       if (lineFeed) {
160         System.out.println(msg);
161       } else {
162         System.out.print(msg);
163       }
164     }
165   }
166   
167   
168   
169   
170   /** Logs the stack trace of the given exception.
171    * @param ex the exception whose stack trace should be logged
172    */

173   public void printStackTrace(Exception JavaDoc ex) {
174     /* only available in JDK 1.4:
175     StackTraceElement[] ste = ex.getStackTrace();
176     for (int i=0; i<ste.length; i++) {
177       println(ste[i].toString());
178     }
179      */

180     ex.printStackTrace();
181   }
182 }
183
Popular Tags