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: ScreenLogger.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:
028: /** Represents a simple Logger that prints logging messages to the screen or
029: * a given print writer.
030: *
031: * @author <a href="mailto:theit@gmx.de">Thorsten Heit</a>
032: */
033: public class ScreenLogger implements Log {
034: /** Holds the logging level. */
035: private int logLevel;
036:
037: /** Holds the current instance of the logger. */
038: private static ScreenLogger theInstance = null;
039: /** A string containing whitespace characters. Used when printing messages. */
040: private static String whitespaces = " ";
041:
042: /** Returns the current instance of the logger.
043: * @return current instance of the screen logger
044: */
045: public static ScreenLogger getInstance() {
046: if (null == theInstance) {
047: theInstance = new ScreenLogger();
048: }
049: return theInstance;
050: }
051:
052: /** Private constructor that initializes the logger. */
053: private ScreenLogger() {
054: setLoggingLevel(NORMAL);
055: }
056:
057: /** Sets the logging level. Future log messages must have a logging level
058: * equal or higher to the one specified in the parameter.
059: * @param level the new logging level; one of the constants
060: * <code>NORMAL</code>, <code>INFO</code>, <code>VERBOSE</code> or <code>DEBUG</code>
061: * @see #getLoggingLevel
062: */
063: public void setLoggingLevel(int level) {
064: logLevel = level;
065: }
066:
067: /** Returns the current logging level.
068: * @return the current logging level
069: */
070: public int getLoggingLevel() {
071: return logLevel;
072: }
073:
074: /** Increment the current logging level to be more verbose.
075: */
076: public void incrementLoggingLevel() {
077: setLoggingLevel(getLoggingLevel() + 1);
078: }
079:
080: /** Prints an empty line to the logger using the <code>NORMAL</code> logging
081: * level.
082: * @see #log
083: */
084: public void println() {
085: println("");
086: }
087:
088: /** Prints the given string to the logger using the <code>NORMAL</code>
089: * logging level and terminates the current line.
090: * @param msg the message to log
091: */
092: public void println(String msg) {
093: log(msg);
094: }
095:
096: /** Prints the given string to the logger using the <code>NORMAL</code>
097: * logging level.
098: * @param msg the message to log
099: */
100: public void print(String msg) {
101: log(NORMAL, msg, false);
102: }
103:
104: /** Prints the given string to the logger using the <code>NORMAL</code>
105: * logging level and terminates the current line.
106: * @param msg the message to log
107: */
108: public void log(String msg) {
109: log(NORMAL, msg);
110: }
111:
112: /** Prints a logging message and terminates the line if the specified logging
113: * level is lower or equal than the current logging level.
114: * @param level the logging level
115: * @param msg the log message
116: */
117: public void log(int level, String msg) {
118: log(level, msg, true);
119: }
120:
121: /** Prints a logging message and terminates the line if the specified logging
122: * level is lower or equal than the current logging level.
123: * @param level the logging level
124: * @param msg the log message
125: * @param lineFeed true if the output line is terminated; false else
126: */
127: public void log(int level, String msg, boolean lineFeed) {
128: if (getLoggingLevel() >= level) {
129: if (level > 0) {
130: if (level > whitespaces.length()) {
131: level = whitespaces.length();
132: }
133: System.out.print(whitespaces.substring(0, level));
134: }
135: if (lineFeed) {
136: System.out.println(msg);
137: } else {
138: System.out.print(msg);
139: }
140: }
141: }
142:
143: /** Logs the stack trace of the given exception.
144: * @param ex the exception whose stack trace should be logged
145: */
146: public void printStackTrace(Exception ex) {
147: /* only available in JDK 1.4:
148: StackTraceElement[] ste = ex.getStackTrace();
149: for (int i=0; i<ste.length; i++) {
150: println(ste[i].toString());
151: }
152: */
153: ex.printStackTrace();
154: }
155: }
|