001: /**
002: * JavaGuard -- an obfuscation package for Java classfiles.
003: *
004: * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * The author may be contacted at theit@gmx.de.
021: *
022: *
023: * $Id: FileLogger.java,v 1.2 2002/04/23 11:18:39 glurk Exp $
024: */package net.sf.javaguard.log;
025:
026: import java.io.PrintWriter;
027: import java.util.Vector;
028:
029: /** Implements a simple writer that serves as an output mechanism for
030: * script file output messages.
031: *
032: * @author <a href="mailto:theit@gmx.de">Thorsten Heit</a>
033: */
034: public class FileLogger implements Log {
035: /** Holds the logging level. */
036: private int logFileLevel;
037:
038: /** Holds the current instance of the logger. */
039: private static FileLogger theInstance = null;
040: /** Holds the print writer that will be used for printing the log messages.
041: * The default setting is to discard all logging messages.
042: */
043: private static PrintWriter logFileWriter = null;
044: /** A vector containing warning messages about methods. */
045: private Vector methodWarnings;
046: /** A vector containing general warning messages. */
047: private Vector warnings;
048:
049: /** Returns the current instance of the logger.
050: * @return the current instance of the file logger
051: */
052: public static FileLogger getInstance() {
053: if (null == theInstance) {
054: theInstance = new FileLogger();
055: }
056: return theInstance;
057: }
058:
059: /** Defines the new print writer that will be used when writing log messages.
060: * @param pw the print writer
061: */
062: public static void setWriter(PrintWriter pw) {
063: logFileWriter = pw;
064: }
065:
066: /** Private constructor that initializes the logger. */
067: private FileLogger() {
068: warnings = new Vector();
069: methodWarnings = new Vector();
070: setLoggingLevel(NORMAL);
071: }
072:
073: /** Flushes the logger and closes the output stream.
074: */
075: protected void finalize() {
076: close();
077: }
078:
079: /** Closes the output streams.
080: */
081: public void close() {
082: if (null != logFileWriter) {
083: logFileWriter.flush();
084: logFileWriter.close();
085: }
086: }
087:
088: /** Sets the logging level. Future log messages must have a logging level
089: * equal or higher to the one specified in the parameter.
090: * @param level the new logging level; one of the constants
091: * <code>NORMAL</code>, <code>INFO</code>, <code>VERBOSE</code> or <code>DEBUG</code>
092: * @see #getLoggingLevel
093: */
094: public void setLoggingLevel(int level) {
095: logFileLevel = level;
096: }
097:
098: /** Returns the current logging level.
099: * @return the current logging level
100: */
101: public int getLoggingLevel() {
102: return logFileLevel;
103: }
104:
105: /** Increment the current logging level to be more verbose.
106: */
107: public void incrementLoggingLevel() {
108: setLoggingLevel(getLoggingLevel() + 1);
109: }
110:
111: /** Prints an empty line to the logger using the <code>NORMAL</code> logging
112: * level.
113: * @see #log
114: */
115: public void println() {
116: println("");
117: }
118:
119: /** Prints the given string to the logger using the <code>NORMAL</code>
120: * logging level and terminates the current line.
121: * @param msg the message to log
122: */
123: public void println(String msg) {
124: log(msg);
125: }
126:
127: /** Prints the given string to the logger using the <code>NORMAL</code>
128: * logging level and terminates the current line.
129: * @param msg the message to log
130: */
131: public void log(String msg) {
132: log(NORMAL, msg);
133: }
134:
135: /** Prints the given string to the logger using the <code>NORMAL</code>
136: * logging level.
137: * @param msg the message to log
138: */
139: public void print(String msg) {
140: log(NORMAL, msg, false);
141: }
142:
143: /** Prints a logging message and terminates the line if the specified logging
144: * level is lower or equal than the current logging level.
145: * @param level the logging level
146: * @param msg the log message
147: */
148: public void log(int level, String msg) {
149: log(level, msg, true);
150: }
151:
152: /** Prints a logging message if the specified logging level is lower or equal
153: * than the current logging level. Depending on the <code>lineFeed</code>
154: * parameter the output line is terminated.
155: * @param level the logging level
156: * @param msg the log message
157: * @param lineFeed true if the output line is terminated; false else
158: */
159: public void log(int level, String msg, boolean lineFeed) {
160: if (getLoggingLevel() >= level) {
161: if (null != logFileWriter) {
162: if (lineFeed) {
163: logFileWriter.println(msg);
164: } else {
165: logFileWriter.print(msg);
166: }
167: }
168: }
169: }
170:
171: /** Logs the stack trace of the given exception.
172: * @param ex the exception whose stack trace should be logged
173: */
174: public void printStackTrace(Exception ex) {
175: ex.printStackTrace(logFileWriter);
176: }
177:
178: /** Append a warning message to the current list of warnings.
179: * @param msg the warning message
180: * @see #printMethodWarnings
181: */
182: public void addMethodWarning(String msg) {
183: methodWarnings.addElement(msg);
184: }
185:
186: /** Print the warning messages to the log file.
187: * @see #addMethodWarning
188: */
189: public void printMethodWarnings() {
190: for (int i = 0; i < methodWarnings.size(); i++) {
191: println((String) methodWarnings.elementAt(i));
192: }
193: }
194:
195: /** Append a general warning message to the current list of warnings.
196: * @param msg the warning message
197: * @see #printWarnings
198: */
199: public void addWarning(String msg) {
200: warnings.addElement(msg);
201: }
202:
203: /** Print the warning messages to the log file.
204: * @see #addWarning
205: */
206: public void printWarnings() {
207: for (int i = 0; i < warnings.size(); i++) {
208: println((String) warnings.elementAt(i));
209: }
210: }
211: }
|