KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > debug > DebugFile


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.debug;
34
35 import java.util.Date JavaDoc;
36
37 import java.io.FileWriter JavaDoc;
38 import java.io.IOException JavaDoc;
39
40
41 /**
42  * <p>Write execution traces to a flat text file</p>
43  * Traces are written to javatrc.txt file on the especified directory.<br>
44  * By default /tmp/ on Unix and C:\WINNT\TEMP or C:\WINDOWS\TEMP on Windows.
45  * @author Sergio Montoro Ten
46  * @version 2.2
47  */

48 public final class DebugFile {
49
50   private static final String JavaDoc DEFAULT_TMP_UNIX = "/tmp/";
51   private static final String JavaDoc DEFAULT_TMP_WIN32 = "C:\\";
52
53   private static String JavaDoc sFilePath = null;
54
55   private static String JavaDoc chomp(String JavaDoc sSource, char cEndsWith) {
56     return sSource.charAt(sSource.length()-1)==cEndsWith ? sSource : sSource + cEndsWith;
57   } // chomp
58

59   /**
60    * Set debug file path
61    * @param sDebugFilePath Full path to file where debug traces will be written including directory and file name
62    */

63   public static void setFile(String JavaDoc sDebugFilePath) {
64     sFilePath = sDebugFilePath;
65   }
66
67   /**
68    * Get debug file path
69    * @return If setFile() has not been called this method returns C:\\javatrc.txt
70    * on Windows environments or /tmp/javatrc.txt on UNIX environments
71    * @since v2.2 the behaviour of this method has changed and now reads Java
72    * environment variable hipergate.debugdir for getting the directory where
73    * javatrc.txt file is generated
74    */

75   public static String JavaDoc getFile() {
76     if (null==sFilePath) {
77       if (System.getProperty("os.name").startsWith("Windows"))
78         sFilePath = chomp(System.getProperty("hipergate.debugdir", DEFAULT_TMP_WIN32),'\\') + "javatrc.txt";
79       else
80         sFilePath = chomp(System.getProperty("hipergate.debugdir", DEFAULT_TMP_UNIX),'/') + "javatrc.txt";
81     }
82     return sFilePath;
83   }
84
85   /**
86    * Increment identation level
87    */

88   public static void incIdent() {
89     sIdent += " ";
90   }
91
92   /**
93    * Decrement identation level
94    */

95
96   public static void decIdent() {
97     if (sIdent.length()>2)
98       sIdent = sIdent.substring(0,sIdent.length()-2);
99     else
100       sIdent = "";
101   }
102
103   /**
104    * <p>Write trace</p>
105    * Traces are written to /tmp/javatrc.txt on UNIX systems or C:\WINNT\javatrc.txt on Windows
106    */

107   public static void write(char[] str) {
108     FileWriter JavaDoc oDebugWriter;
109
110     try {
111
112       // Volcar las trazas de salida a una ubicación por defecto
113
// según el sistema operativo sea Windows o UNIX
114

115       switch (dumpTo) {
116         case DUMP_TO_FILE:
117           oDebugWriter = new FileWriter JavaDoc(getFile(), true);
118           oDebugWriter.write(str);
119           oDebugWriter.close();
120           break;
121         case DUMP_TO_STDOUT:
122           System.out.print(str);
123           break;
124       }
125     }
126     catch (IOException JavaDoc e) {
127       System.out.print(str);
128     }
129   }
130
131   /**
132    * <p>Write trace</p>
133    * Traces are written to /tmp/javatrc.txt on UNIX systems or C:\WINNT\javatrc.txt on Windows
134    */

135   public static void write(String JavaDoc str) {
136     FileWriter JavaDoc oDebugWriter;
137
138     try {
139
140       // Volcar las trazas de salida a una ubicación por defecto
141
// según el sistema operativo sea Windows o UNIX
142

143       switch (dumpTo) {
144         case DUMP_TO_FILE:
145           oDebugWriter = new FileWriter JavaDoc(getFile(), true);
146           oDebugWriter.write(str);
147           oDebugWriter.close();
148           break;
149         case DUMP_TO_STDOUT:
150           System.out.print(str);
151           break;
152       }
153     }
154     catch (IOException JavaDoc e) {
155       System.out.print(str);
156     }
157   }
158
159   /**
160    * Write trace and append line feed
161    * @param str
162    */

163   public static void writeln(String JavaDoc str) {
164     FileWriter JavaDoc oDebugWriter;
165     Date JavaDoc dt = new Date JavaDoc(System.currentTimeMillis());
166
167     try {
168
169       // Volcar las trazas de salida a una ubicación por defecto
170
// según el sistema operativo sea Windows o UNIX
171

172       switch (dumpTo) {
173         case DUMP_TO_FILE:
174           oDebugWriter = new FileWriter JavaDoc(getFile(), true);
175           oDebugWriter.write(dt.toString()+" "+sIdent+str+"\n");
176           oDebugWriter.close();
177           break;
178         case DUMP_TO_STDOUT:
179           System.out.print(dt.toString()+sIdent+str+"\n");
180           break;
181       }
182     }
183     catch (IOException JavaDoc e) {
184       System.out.print(dt.toString()+sIdent+str+"\n");
185     }
186   }
187
188   /**
189    * Write trace and append line feed
190    * @param str
191    */

192   public static void writeln(char[] str) {
193     FileWriter JavaDoc oDebugWriter;
194     Date JavaDoc dt = new Date JavaDoc(System.currentTimeMillis());
195
196     try {
197
198       // Volcar las trazas de salida a una ubicación por defecto
199
// según el sistema operativo sea Windows o UNIX
200

201       switch (dumpTo) {
202         case DUMP_TO_FILE:
203           oDebugWriter = new FileWriter JavaDoc(getFile(), true);
204           oDebugWriter.write(dt.toString()+" "+sIdent+str+"\n");
205           oDebugWriter.close();
206           break;
207         case DUMP_TO_STDOUT:
208           System.out.print(dt.toString()+sIdent+str+"\n");
209           break;
210       }
211     }
212     catch (IOException JavaDoc e) {
213       System.out.print(dt.toString()+sIdent+str+"\n");
214     }
215   } // write
216

217   /**
218    * Write stack trace for an exception to debug file
219    * @param t Throwable
220    */

221   public static void writeStackTrace(Throwable JavaDoc t) {
222     try {
223       DebugFile.write(StackTraceUtil.getStackTrace(t));
224     } catch (Exception JavaDoc ignore) {}
225   }
226
227   /**
228    * This method is just an alias for DebugFile.writeln
229    * @param str
230    */

231   public void debug(String JavaDoc str) {
232     DebugFile.writeln(str);
233   }
234
235   public void debug(String JavaDoc str, Exception JavaDoc xcpt) {
236     DebugFile.writeln(str);
237     new ErrorHandler(xcpt);
238   }
239
240
241   public static void envinfo() throws java.security.AccessControlException JavaDoc {
242     DebugFile.writeln(System.getProperty("java.vendor") + " Runtime Environment " + System.getProperty("java.version"));
243     DebugFile.writeln(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
244     DebugFile.writeln(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch"));
245     DebugFile.writeln("JVM encoding " + System.getProperty( "file.encoding "));
246
247     /*
248     WINDOWS MILLENIUM
249     -----------------
250     java.vendor=Sun Microsystems Inc.
251     java.version=1.3.1
252     java.vm.vendor=Sun Microsystems Inc.
253     java.vm.name=Java HotSpot(TM) Client VM
254     java.vm.version=1.3.1-b24
255     os.name=Windows Me
256     os.version=4.90
257     os.arch=x86
258
259     ALPHA
260     -----
261     java.vendor=Compaq Computer Corp.
262     java.version=1.2.2-8
263     java.vm.vendor=Compaq Computer Corp.
264     java.vm.name=Classic VM
265     java.vm.version=1.2.2-8
266     os.name=OSF1
267     os.version=V4.0
268     os.arch=alpha
269
270     JSERVER
271     -------
272     java.vendor=Oracle Corporation
273     java.version=1.2.1
274     java.vm.vendor=Oracle Corporation
275     java.vm.name=JServer VM
276     java.vm.version=1.2.1
277     os.name=Solaris
278     os.version=V4.0
279     os.arch=alpha
280     */

281   }
282
283   public static void main(String JavaDoc[] argv) {
284     System.out.println("Debug mode " + (trace ? "enabled" : "disabled"));
285     System.out.println("Debug file " + getFile());
286     System.out.println(System.getProperty("java.vendor") + " Runtime Environment " + System.getProperty("java.version"));
287     System.out.println(System.getProperty("java.vm.vendor") + " " + System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
288     System.out.println(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch"));
289     System.out.println("JVM encoding " + System.getProperty( "file.encoding "));
290   }
291
292   // Espacios de identación en cada línea de traza
293
private static String JavaDoc sIdent = "";
294
295   // **********************************************************
296
// Esta variable controla si se volcarán trazas o no
297

298   public static final short DUMP_TO_FILE = (short) 1;
299   public static final short DUMP_TO_STDOUT = (short) 2;
300
301   public static short dumpTo = DUMP_TO_FILE;
302
303   /**
304    * Activate/Deactive trace output
305    */

306   public static final boolean trace = false;
307   }
308
Popular Tags