001: package snaq.util;
002:
003: import java.io.*;
004: import java.text.*;
005: import java.util.*;
006:
007: /**
008: * Base class providing simple logging and debug functionality, which can
009: * either be instantiated and used as a logging object, or can be sub-classed
010: * to be used as an integral logging facility.
011: * <p>When specifying an OutputStream for use by the Logger, make sure that none
012: * of the "standard" streams are used (System.out, System.err) as
013: * they will be closed either when the close() method is called or the Logger
014: * terminates. To send log to the standard output stream simply call
015: * setLogging(true) and leave the actual log unspecified.
016: * @author Giles Winstanley
017: */
018: public class LogUtil {
019: protected DateFormat dateFormat, ddf;
020: protected PrintStream log;
021: protected boolean logging = false;
022: protected boolean debug = false;
023:
024: /**
025: * Creates a new Logger with logging disabled.
026: */
027: public LogUtil() {
028: }
029:
030: /**
031: * Creates a new Logger which writes to the specified file.
032: * @throws FileNotFoundException if specified file is a directory, or cannot
033: * be opened for some reason.
034: */
035: public LogUtil(File f) throws FileNotFoundException {
036: setLog(new FileOutputStream(f, true));
037: }
038:
039: /**
040: * Sets the date formatter for the logging.
041: */
042: public synchronized void setDateFormat(DateFormat df) {
043: dateFormat = df;
044: }
045:
046: /**
047: * Sets the log stream and enables logging.
048: */
049: public synchronized void setLog(OutputStream out) {
050: if (log != null)
051: close();
052: if (out != null)
053: log = new PrintStream(out);
054: logging = true;
055: }
056:
057: /**
058: * Sets the log writer and enables logging.
059: */
060: public synchronized void setLog(PrintStream ps) {
061: if (log != null)
062: close();
063: log = ps;
064: logging = true;
065: }
066:
067: /**
068: * Returns the current PrintStream used to write to the log.
069: */
070: public PrintStream getLogStream() {
071: return log;
072: }
073:
074: /**
075: * Writes a message to the log, with an optional prefix.
076: */
077: public synchronized void log(String prefix, String logEntry) {
078: if (!logging)
079: return;
080: StringBuffer sb = new StringBuffer();
081: if (dateFormat != null)
082: sb.append(dateFormat.format(new Date()));
083: else {
084: if (ddf == null)
085: ddf = DateFormat.getDateTimeInstance(DateFormat.LONG,
086: DateFormat.LONG);
087: sb.append(ddf.format(new Date()));
088: }
089:
090: sb.append(": ");
091: if (prefix != null)
092: sb.append(prefix);
093: sb.append(logEntry);
094: if (log != null) {
095: synchronized (log) {
096: log.println(sb.toString());
097: // log.flush();
098: }
099: }
100: }
101:
102: /**
103: * Writes a message to the log.
104: */
105: public void log(String logEntry) {
106: log("", logEntry);
107: }
108:
109: /**
110: * Writes a message with an Exception to the log file.
111: */
112: public void log(Throwable e, String prefix, String logEntry) {
113: if (!logging)
114: return;
115: log(prefix, logEntry);
116: e.printStackTrace(log);
117: log.flush();
118: }
119:
120: /**
121: * Writes a message with an Exception to the log file.
122: */
123: public void log(Throwable e, String logEntry) {
124: log(e, "", logEntry);
125: }
126:
127: /**
128: * Writes an Exception to the log file.
129: */
130: public void log(Throwable e) {
131: log(e, e.getMessage());
132: }
133:
134: /**
135: * Closes the log.
136: */
137: public synchronized void close() {
138: logging = false;
139: if (log != null) {
140: log.flush();
141: if (!isSystemLog())
142: log.close();
143: }
144: log = null;
145: }
146:
147: // Returns whether the log is writing to a system log stream.
148: private boolean isSystemLog() {
149: return (!log.equals(System.out) && !log.equals(System.err));
150: }
151:
152: /**
153: * Determines whether to write to the log.
154: */
155: public void setLogging(boolean b) {
156: logging = b;
157: }
158:
159: /**
160: * Returns whether logging is enabled.
161: */
162: public boolean isLogging() {
163: return logging;
164: }
165:
166: /**
167: * Determines whether to perform debug logging.
168: */
169: public void setDebug(boolean b) {
170: debug = b;
171: }
172:
173: /**
174: * Returns whether debug logging is enabled.
175: */
176: public boolean isDebug() {
177: return debug;
178: }
179: }
|