001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oña, 107 1º2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.debug;
034:
035: import java.util.Date;
036:
037: import java.io.FileWriter;
038: import java.io.IOException;
039:
040: /**
041: * <p>Write execution traces to a flat text file</p>
042: * Traces are written to javatrc.txt file on the especified directory.<br>
043: * By default /tmp/ on Unix and C:\WINNT\TEMP or C:\WINDOWS\TEMP on Windows.
044: * @author Sergio Montoro Ten
045: * @version 2.2
046: */
047: public final class DebugFile {
048:
049: private static final String DEFAULT_TMP_UNIX = "/tmp/";
050: private static final String DEFAULT_TMP_WIN32 = "C:\\";
051:
052: private static String sFilePath = null;
053:
054: private static String chomp(String sSource, char cEndsWith) {
055: return sSource.charAt(sSource.length() - 1) == cEndsWith ? sSource
056: : sSource + cEndsWith;
057: } // chomp
058:
059: /**
060: * Set debug file path
061: * @param sDebugFilePath Full path to file where debug traces will be written including directory and file name
062: */
063: public static void setFile(String sDebugFilePath) {
064: sFilePath = sDebugFilePath;
065: }
066:
067: /**
068: * Get debug file path
069: * @return If setFile() has not been called this method returns C:\\javatrc.txt
070: * on Windows environments or /tmp/javatrc.txt on UNIX environments
071: * @since v2.2 the behaviour of this method has changed and now reads Java
072: * environment variable hipergate.debugdir for getting the directory where
073: * javatrc.txt file is generated
074: */
075: public static String getFile() {
076: if (null == sFilePath) {
077: if (System.getProperty("os.name").startsWith("Windows"))
078: sFilePath = chomp(System.getProperty(
079: "hipergate.debugdir", DEFAULT_TMP_WIN32), '\\')
080: + "javatrc.txt";
081: else
082: sFilePath = chomp(System.getProperty(
083: "hipergate.debugdir", DEFAULT_TMP_UNIX), '/')
084: + "javatrc.txt";
085: }
086: return sFilePath;
087: }
088:
089: /**
090: * Increment identation level
091: */
092: public static void incIdent() {
093: sIdent += " ";
094: }
095:
096: /**
097: * Decrement identation level
098: */
099:
100: public static void decIdent() {
101: if (sIdent.length() > 2)
102: sIdent = sIdent.substring(0, sIdent.length() - 2);
103: else
104: sIdent = "";
105: }
106:
107: /**
108: * <p>Write trace</p>
109: * Traces are written to /tmp/javatrc.txt on UNIX systems or C:\WINNT\javatrc.txt on Windows
110: */
111: public static void write(char[] str) {
112: FileWriter oDebugWriter;
113:
114: try {
115:
116: // Volcar las trazas de salida a una ubicación por defecto
117: // según el sistema operativo sea Windows o UNIX
118:
119: switch (dumpTo) {
120: case DUMP_TO_FILE:
121: oDebugWriter = new FileWriter(getFile(), true);
122: oDebugWriter.write(str);
123: oDebugWriter.close();
124: break;
125: case DUMP_TO_STDOUT:
126: System.out.print(str);
127: break;
128: }
129: } catch (IOException e) {
130: System.out.print(str);
131: }
132: }
133:
134: /**
135: * <p>Write trace</p>
136: * Traces are written to /tmp/javatrc.txt on UNIX systems or C:\WINNT\javatrc.txt on Windows
137: */
138: public static void write(String str) {
139: FileWriter oDebugWriter;
140:
141: try {
142:
143: // Volcar las trazas de salida a una ubicación por defecto
144: // según el sistema operativo sea Windows o UNIX
145:
146: switch (dumpTo) {
147: case DUMP_TO_FILE:
148: oDebugWriter = new FileWriter(getFile(), true);
149: oDebugWriter.write(str);
150: oDebugWriter.close();
151: break;
152: case DUMP_TO_STDOUT:
153: System.out.print(str);
154: break;
155: }
156: } catch (IOException e) {
157: System.out.print(str);
158: }
159: }
160:
161: /**
162: * Write trace and append line feed
163: * @param str
164: */
165: public static void writeln(String str) {
166: FileWriter oDebugWriter;
167: Date dt = new Date(System.currentTimeMillis());
168:
169: try {
170:
171: // Volcar las trazas de salida a una ubicación por defecto
172: // según el sistema operativo sea Windows o UNIX
173:
174: switch (dumpTo) {
175: case DUMP_TO_FILE:
176: oDebugWriter = new FileWriter(getFile(), true);
177: oDebugWriter.write(dt.toString() + " " + sIdent + str
178: + "\n");
179: oDebugWriter.close();
180: break;
181: case DUMP_TO_STDOUT:
182: System.out.print(dt.toString() + sIdent + str + "\n");
183: break;
184: }
185: } catch (IOException e) {
186: System.out.print(dt.toString() + sIdent + str + "\n");
187: }
188: }
189:
190: /**
191: * Write trace and append line feed
192: * @param str
193: */
194: public static void writeln(char[] str) {
195: FileWriter oDebugWriter;
196: Date dt = new Date(System.currentTimeMillis());
197:
198: try {
199:
200: // Volcar las trazas de salida a una ubicación por defecto
201: // según el sistema operativo sea Windows o UNIX
202:
203: switch (dumpTo) {
204: case DUMP_TO_FILE:
205: oDebugWriter = new FileWriter(getFile(), true);
206: oDebugWriter.write(dt.toString() + " " + sIdent + str
207: + "\n");
208: oDebugWriter.close();
209: break;
210: case DUMP_TO_STDOUT:
211: System.out.print(dt.toString() + sIdent + str + "\n");
212: break;
213: }
214: } catch (IOException e) {
215: System.out.print(dt.toString() + sIdent + str + "\n");
216: }
217: } // write
218:
219: /**
220: * Write stack trace for an exception to debug file
221: * @param t Throwable
222: */
223: public static void writeStackTrace(Throwable t) {
224: try {
225: DebugFile.write(StackTraceUtil.getStackTrace(t));
226: } catch (Exception ignore) {
227: }
228: }
229:
230: /**
231: * This method is just an alias for DebugFile.writeln
232: * @param str
233: */
234: public void debug(String str) {
235: DebugFile.writeln(str);
236: }
237:
238: public void debug(String str, Exception xcpt) {
239: DebugFile.writeln(str);
240: new ErrorHandler(xcpt);
241: }
242:
243: public static void envinfo()
244: throws java.security.AccessControlException {
245: DebugFile.writeln(System.getProperty("java.vendor")
246: + " Runtime Environment "
247: + System.getProperty("java.version"));
248: DebugFile.writeln(System.getProperty("java.vm.vendor") + " "
249: + System.getProperty("java.vm.name") + " "
250: + System.getProperty("java.vm.version"));
251: DebugFile.writeln(System.getProperty("os.name") + " "
252: + System.getProperty("os.version") + " "
253: + System.getProperty("os.arch"));
254: DebugFile.writeln("JVM encoding "
255: + System.getProperty("file.encoding "));
256:
257: /*
258: WINDOWS MILLENIUM
259: -----------------
260: java.vendor=Sun Microsystems Inc.
261: java.version=1.3.1
262: java.vm.vendor=Sun Microsystems Inc.
263: java.vm.name=Java HotSpot(TM) Client VM
264: java.vm.version=1.3.1-b24
265: os.name=Windows Me
266: os.version=4.90
267: os.arch=x86
268:
269: ALPHA
270: -----
271: java.vendor=Compaq Computer Corp.
272: java.version=1.2.2-8
273: java.vm.vendor=Compaq Computer Corp.
274: java.vm.name=Classic VM
275: java.vm.version=1.2.2-8
276: os.name=OSF1
277: os.version=V4.0
278: os.arch=alpha
279:
280: JSERVER
281: -------
282: java.vendor=Oracle Corporation
283: java.version=1.2.1
284: java.vm.vendor=Oracle Corporation
285: java.vm.name=JServer VM
286: java.vm.version=1.2.1
287: os.name=Solaris
288: os.version=V4.0
289: os.arch=alpha
290: */
291: }
292:
293: public static void main(String[] argv) {
294: System.out.println("Debug mode "
295: + (trace ? "enabled" : "disabled"));
296: System.out.println("Debug file " + getFile());
297: System.out.println(System.getProperty("java.vendor")
298: + " Runtime Environment "
299: + System.getProperty("java.version"));
300: System.out.println(System.getProperty("java.vm.vendor") + " "
301: + System.getProperty("java.vm.name") + " "
302: + System.getProperty("java.vm.version"));
303: System.out.println(System.getProperty("os.name") + " "
304: + System.getProperty("os.version") + " "
305: + System.getProperty("os.arch"));
306: System.out.println("JVM encoding "
307: + System.getProperty("file.encoding "));
308: }
309:
310: // Espacios de identación en cada línea de traza
311: private static String sIdent = "";
312:
313: // **********************************************************
314: // Esta variable controla si se volcarán trazas o no
315:
316: public static final short DUMP_TO_FILE = (short) 1;
317: public static final short DUMP_TO_STDOUT = (short) 2;
318:
319: public static short dumpTo = DUMP_TO_FILE;
320:
321: /**
322: * Activate/Deactive trace output
323: */
324: public static final boolean trace = true;
325: }
|