KickJava   Java API By Example, From Geeks To Geeks.

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


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: FileLogger.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 import java.util.Vector JavaDoc;
29
30
31
32
33 /** Implements a simple writer that serves as an output mechanism for
34  * script file output messages.
35  *
36  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
37  */

38 public class FileLogger implements Log {
39   /** Holds the logging level. */
40   private int logFileLevel;
41   
42   
43   /** Holds the current instance of the logger. */
44   private static FileLogger theInstance = null;
45   /** Holds the print writer that will be used for printing the log messages.
46    * The default setting is to discard all logging messages.
47    */

48   private static PrintWriter JavaDoc logFileWriter = null;
49   /** A vector containing warning messages about methods. */
50   private Vector JavaDoc methodWarnings;
51   /** A vector containing general warning messages. */
52   private Vector JavaDoc warnings;
53   
54   
55   
56   
57   /** Returns the current instance of the logger.
58    * @return the current instance of the file logger
59    */

60   public static FileLogger getInstance() {
61     if (null == theInstance) {
62       theInstance = new FileLogger();
63     }
64     return theInstance;
65   }
66   
67   
68   
69   
70   /** Defines the new print writer that will be used when writing log messages.
71    * @param pw the print writer
72    */

73   public static void setWriter(PrintWriter JavaDoc pw) {
74     logFileWriter = pw;
75   }
76   
77   
78   
79   
80   /** Private constructor that initializes the logger. */
81   private FileLogger() {
82     warnings = new Vector JavaDoc();
83     methodWarnings = new Vector JavaDoc();
84     setLoggingLevel(NORMAL);
85   }
86   
87   
88   /** Flushes the logger and closes the output stream.
89    */

90   protected void finalize() {
91     close();
92   }
93   
94   
95   /** Closes the output streams.
96    */

97   public void close() {
98     if (null != logFileWriter) {
99       logFileWriter.flush();
100       logFileWriter.close();
101     }
102   }
103   
104   
105   
106   
107   /** Sets the logging level. Future log messages must have a logging level
108    * equal or higher to the one specified in the parameter.
109    * @param level the new logging level; one of the constants
110    * <code>NORMAL</code>, <code>INFO</code>, <code>VERBOSE</code> or <code>DEBUG</code>
111    * @see #getLoggingLevel
112    */

113   public void setLoggingLevel(int level) {
114     logFileLevel = level;
115   }
116   
117   
118   /** Returns the current logging level.
119    * @return the current logging level
120    */

121   public int getLoggingLevel() {
122     return logFileLevel;
123   }
124   
125   
126   /** Increment the current logging level to be more verbose.
127    */

128   public void incrementLoggingLevel() {
129     setLoggingLevel(getLoggingLevel() + 1);
130   }
131   
132   
133   
134   
135   /** Prints an empty line to the logger using the <code>NORMAL</code> logging
136    * level.
137    * @see #log
138    */

139   public void println() {
140     println("");
141   }
142   
143   
144   /** Prints the given string to the logger using the <code>NORMAL</code>
145    * logging level and terminates the current line.
146    * @param msg the message to log
147    */

148   public void println(String JavaDoc msg) {
149     log(msg);
150   }
151   
152   
153   /** Prints the given string to the logger using the <code>NORMAL</code>
154    * logging level and terminates the current line.
155    * @param msg the message to log
156    */

157   public void log(String JavaDoc msg) {
158     log(NORMAL, msg);
159   }
160   
161   
162   /** Prints the given string to the logger using the <code>NORMAL</code>
163    * logging level.
164    * @param msg the message to log
165    */

166   public void print(String JavaDoc msg) {
167     log(NORMAL, msg, false);
168   }
169   
170   
171   /** Prints a logging message and terminates the line if the specified logging
172    * level is lower or equal than the current logging level.
173    * @param level the logging level
174    * @param msg the log message
175    */

176   public void log(int level, String JavaDoc msg) {
177     log(level, msg, true);
178   }
179   
180   
181   /** Prints a logging message if the specified logging level is lower or equal
182    * than the current logging level. Depending on the <code>lineFeed</code>
183    * parameter the output line is terminated.
184    * @param level the logging level
185    * @param msg the log message
186    * @param lineFeed true if the output line is terminated; false else
187    */

188   public void log(int level, String JavaDoc msg, boolean lineFeed) {
189     if (getLoggingLevel() >= level) {
190       if (null != logFileWriter) {
191         if (lineFeed) {
192           logFileWriter.println(msg);
193         } else {
194           logFileWriter.print(msg);
195         }
196       }
197     }
198   }
199   
200   
201   
202   
203   /** Logs the stack trace of the given exception.
204    * @param ex the exception whose stack trace should be logged
205    */

206   public void printStackTrace(Exception JavaDoc ex) {
207     ex.printStackTrace(logFileWriter);
208   }
209   
210   
211   
212   
213   /** Append a warning message to the current list of warnings.
214    * @param msg the warning message
215    * @see #printMethodWarnings
216    */

217   public void addMethodWarning(String JavaDoc msg) {
218     methodWarnings.addElement(msg);
219   }
220   
221   
222   /** Print the warning messages to the log file.
223    * @see #addMethodWarning
224    */

225   public void printMethodWarnings() {
226     for (int i=0; i<methodWarnings.size(); i++) {
227       println((String JavaDoc) methodWarnings.elementAt(i));
228     }
229   }
230   
231   
232   
233   
234   /** Append a general warning message to the current list of warnings.
235    * @param msg the warning message
236    * @see #printWarnings
237    */

238   public void addWarning(String JavaDoc msg) {
239     warnings.addElement(msg);
240   }
241   
242   
243   /** Print the warning messages to the log file.
244    * @see #addWarning
245    */

246   public void printWarnings() {
247     for (int i=0; i<warnings.size(); i++) {
248       println((String JavaDoc) warnings.elementAt(i));
249     }
250   }
251 }
252
Popular Tags