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