001: /*
002: ** Copyright (c) 1997 by Timothy Gerard Endres
003: **
004: ** This program is free software.
005: **
006: ** You may redistribute it and/or modify it under the terms of the GNU
007: ** General Public License as published by the Free Software Foundation.
008: ** Version 2 of the license should be included with this distribution in
009: ** the file LICENSE, as well as License.html. If the license is not
010: ** included with this distribution, you may find a copy at the FSF web
011: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
012: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
013: **
014: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
015: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
016: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
017: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
018: ** REDISTRIBUTION OF THIS SOFTWARE.
019: **
020: */
021:
022: package com.ice.util;
023:
024: import java.io.*;
025: import java.lang.*;
026: import java.util.*;
027:
028: public class FileLog extends Object {
029: private static final String RCS_ID = "$Id: FileLog.java,v 1.2 2004/03/05 12:20:31 deniger Exp $";
030: private static final String RCS_REV = "$Revision: 1.2 $";
031: private static final String RCS_NAME = "$Name: $";
032:
033: private static final String DEFAULT_FILENAME = "log.txt";
034:
035: private static FileLog defaultLogger = null;
036:
037: private String filename;
038: private FileWriter file;
039: private PrintWriter stream;
040: private boolean open;
041: private boolean echo;
042: private boolean autoFlush;
043:
044: static public FileLog getDefaultLogger() {
045: return FileLog.defaultLogger;
046: }
047:
048: static public FileLog setDefaultLogger(FileLog logger) {
049: FileLog old = FileLog.defaultLogger;
050: FileLog.defaultLogger = logger;
051: return old;
052: }
053:
054: static public void checkDefaultLogger() {
055: if (FileLog.defaultLogger == null) {
056: FileLog.defaultLogger = new FileLog(
057: FileLog.DEFAULT_FILENAME);
058: }
059: }
060:
061: public FileLog(String filename) {
062: this .open = false;
063: this .echo = false;
064: this .autoFlush = true;
065: this .filename = filename;
066: this .file = null;
067: this .stream = null;
068: }
069:
070: public void setLogFilename(String filename) {
071: this .filename = filename;
072: }
073:
074: public void setAutoFlush(boolean autoflush) {
075: this .autoFlush = autoflush;
076: }
077:
078: public void checkLogOpen() {
079: if (!this .open) {
080: this .openLogFile();
081: }
082: }
083:
084: public void openLogFile() {
085: boolean isok = true;
086:
087: try {
088: this .file = new FileWriter(this .filename);
089: } catch (Exception ex) {
090: System.err.println("error opening log file '"
091: + this .filename + "' - " + ex.getMessage());
092: this .file = null;
093: isok = false;
094: }
095:
096: if (isok) {
097: this .stream = new PrintWriter(this .file);
098: this .open = true;
099: }
100:
101: this .echo = false;
102: }
103:
104: public void closeLog() {
105: if (this .open) {
106: this .open = false;
107: if (this .stream != null) {
108: this .stream.flush();
109: this .stream.close();
110: this .stream = null;
111: }
112: }
113: }
114:
115: public void setEcho(boolean setting) {
116: this .echo = setting;
117: }
118:
119: public void traceMsg(Throwable thrown, String msg) {
120: this .logMsg(msg);
121: this .logMsg(thrown.getMessage());
122:
123: if (!this .open)
124: thrown.printStackTrace(System.err);
125: else
126: thrown.printStackTrace(this .stream);
127:
128: if (this .autoFlush && this .open)
129: this .stream.flush();
130: }
131:
132: static public void defLogMsg(String msg) {
133: FileLog.checkDefaultLogger();
134: if (FileLog.defaultLogger != null)
135: FileLog.defaultLogger.logMsg(msg);
136: }
137:
138: public void logMsg(String msg) {
139: this .checkLogOpen();
140:
141: if (this .open) {
142: this .stream.println(msg);
143: if (this .autoFlush && this .open)
144: this .stream.flush();
145: }
146:
147: if (this .echo) {
148: System.out.println(msg);
149: }
150: }
151:
152: static public void defLogMsgStdout(String msg) {
153: FileLog.checkDefaultLogger();
154: if (FileLog.defaultLogger != null)
155: FileLog.defaultLogger.logMsgStdout(msg);
156: }
157:
158: public void logMsgStdout(String msg) {
159: this .checkLogOpen();
160:
161: if (this .open) {
162: this .stream.println(msg);
163: if (this .autoFlush && this .open)
164: this .stream.flush();
165: }
166:
167: System.out.println(msg);
168: }
169:
170: static public void defLogMsgStderr(String msg) {
171: FileLog.checkDefaultLogger();
172: if (FileLog.defaultLogger != null)
173: FileLog.defaultLogger.logMsgStderr(msg);
174: }
175:
176: public void logMsgStderr(String msg) {
177: this.checkLogOpen();
178:
179: if (this.open) {
180: this.stream.println(msg);
181: if (this.autoFlush && this.open)
182: this.stream.flush();
183: }
184:
185: System.err.println(msg);
186: }
187: }
|