KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > Debug


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Jan Blok
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package com.izforge.izpack.util;
22
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import com.izforge.izpack.installer.Installer;
33
34 /**
35  * This class is for debug purposes. It is highly recommended to use it on critical or experimental
36  * code places. To enable the debug mode of IzPack, just start the installer with the java parameter
37  * -DTRACE=true or -DSTACKTRACE=true to enable extendend output of the internal status of critical
38  * objects. <br>
39  * How to use it as IzPack Setup Developer: <br>
40  * Just import this class and use one of the methods:
41  *
42  * <dl>
43  * <dt> Debug.trace( aCriticalObject ) </dt>
44  * <dd> - to print the status on console </dd>
45  * <dt> Debug.error( aCriticalObject ) </dt>
46  * <dd> - to print the status on console and<br>
47  * print the stacktrace of a supressed Exception. </dd>
48  * <dt> Additionally: </dt>
49  * <dd> if -DLOG is given the output will be written in the File see #LOGFILENAME in the users Home
50  * directory. </dd>
51  * </dl>
52  *
53  *
54  * @author Julien Ponge, Klaus Bartz, Marc Eppelmann
55  * @version $Revision: 1708 $ ($Id: Debug.java 1708 2007-01-13 18:31:26Z jponge $)
56  */

57 public class Debug
58 {
59
60     // ~ Static fields/initializers *********************************************************
61

62     /**
63      * Parameter for public javacall "java -jar izpack.jar -DLOG" (Class.internal.variable: (DLOG =
64      * "LOG"))
65      */

66     public static final String JavaDoc DLOG = "LOG";
67
68     /**
69      * Parameter for public javacall "java -jar izpack.jar -DSTACKTRACE" (Class.internal.variable:
70      * (DSTACKTRACE = "STACKTRACE"))
71      */

72     public static final String JavaDoc DSTACKTRACE = "STACKTRACE";
73
74     /**
75      * Parameter for public javacall "java -jar izpack.jar -DTRACE" (Class.internal.variable:
76      * (DTRACE = "TRACE"))
77      */

78     public static final String JavaDoc DTRACE = "TRACE";
79
80     /** System.Property Key: IZPACK_LOGFILE = "izpack.logfile" */
81     public static final String JavaDoc IZPACK_LOGFILE = "izpack.logfile";
82
83     /** LOG_WITHOUT_DATE = 0 */
84     public static final int LOG_WITHOUT_DATE = 0;
85
86     /** LOG_WITH_DATE = 1 */
87     public static final int LOG_WITH_DATE = 1;
88
89     /** LOG_WITH_TIME_STAMP = 2 */
90     public static final int LOG_WITH_TIME_STAMP = 2;
91
92     /** LOG_WITH_TIME_AND_DATE= LOG_WITH_DATE | LOG_WITH_TIME_STAMP = 3 */
93     public static final int LOG_WITH_TIME_AND_DATE = LOG_WITH_DATE | LOG_WITH_TIME_STAMP;
94
95     /** internally initial unintialized TRACE-flag */
96     private static boolean TRACE;
97
98     /** internal initial unintialized STACKTRACE-flag */
99     private static boolean STACKTRACE;
100
101     /** internal initial unintialized LOG-flag */
102     private static boolean LOG;
103
104     /** LOGFILE_PREFIX = "IzPack_Logfile_at_" */
105     public static String JavaDoc LOGFILE_PREFIX = "IzPack_Logfile_at_";
106
107     /** LOGFILE_EXTENSION = ".txt" */
108     public static String JavaDoc LOGFILE_EXTENSION = ".txt";
109
110     /** LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis() + LOGFILE_EXTENSION */
111     public static String JavaDoc LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis()
112             + LOGFILE_EXTENSION;
113
114     /**
115      * The log initializion bloc.
116      */

117     static
118     {
119         boolean st = false;
120
121         try
122         {
123             st = Boolean.getBoolean(DSTACKTRACE);
124         }
125         catch (Exception JavaDoc ex)
126         {
127             // ignore
128
}
129
130         STACKTRACE = st;
131
132         boolean log = false;
133
134         try
135         {
136             log = Boolean.getBoolean(DLOG);
137         }
138         catch (Exception JavaDoc ex)
139         {
140             // ignore
141
}
142
143         LOG = log;
144
145         boolean t = false;
146
147         try
148         {
149             if (STACKTRACE)
150             {
151                 t = true;
152             }
153             else
154             {
155                 t = Boolean.getBoolean(DTRACE);
156             }
157         }
158         catch (Exception JavaDoc ex)
159         {
160             // ignore
161
}
162
163         TRACE = t;
164
165         if (LOG)
166         {
167             System.out.println(DLOG + " enabled.");
168             PrintWriter JavaDoc logfile = createLogFile();
169
170             Debug.log(Installer.class.getName() + " LogFile created at ");
171
172             // ** write some runtime system properties into the logfile **
173
Debug.log("System.Properties:", LOG_WITH_TIME_STAMP);
174
175             Properties JavaDoc sysProps = System.getProperties();
176
177             Enumeration JavaDoc spe = sysProps.keys();
178
179             while (spe.hasMoreElements())
180             {
181                 String JavaDoc aKey = (String JavaDoc) spe.nextElement();
182                 Debug.log(aKey + " = " + sysProps.getProperty(aKey), LOG_WITHOUT_DATE);
183             }
184             Debug.log("\n==========================================\n", LOG_WITHOUT_DATE);
185             Debug.log("\n " + Installer.class.getName() + " installs on: \n", LOG_WITHOUT_DATE);
186             Debug.log(OsVersion.getOsDetails(), LOG_WITHOUT_DATE);
187             Debug.log("\n==========================================\n", LOG_WITHOUT_DATE);
188         }
189
190         if (TRACE)
191         {
192             System.out.println(DTRACE + " enabled.");
193         }
194
195         if (STACKTRACE)
196         {
197             System.out.println(DSTACKTRACE + " enabled.");
198         }
199     }
200
201     // ~ Methods ****************************************************************************
202

203     /**
204      * Traces the internal status of the given Object
205      *
206      * @param s
207      */

208     public static void trace(Object JavaDoc s)
209     {
210         if (TRACE)
211         {
212             // console.println(s.toString());
213
System.out.println(s);
214
215             if (STACKTRACE && (s instanceof Throwable JavaDoc))
216             {
217                 // StringWriter sw = new StringWriter();
218
// PrintWriter pw = new PrintWriter(sw);
219
// ((Throwable)s).printStackTrace(pw);
220
// console.println(sw.toString());
221
((Throwable JavaDoc) s).printStackTrace();
222             }
223
224             System.out.flush();
225         }
226     }
227
228     /**
229      * Traces the given object and additional write their status in the LOGFILE.
230      *
231      * @param s
232      */

233     public static void error(Object JavaDoc s)
234     {
235         trace(s);
236         System.err.println(s);
237         System.err.flush();
238         log(s);
239     }
240
241     /**
242      * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
243      * -DLOG=true -jar izpack-installer.jar
244      *
245      * @param o The Object to log, can be also an exception.
246      */

247     public static void log(Object JavaDoc o)
248     {
249         log(o, LOG_WITH_TIME_AND_DATE);
250     }
251
252     /**
253      * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
254      * -DLOG=true -jar izpack-installer.jar
255      *
256      * @param o The Object to log
257      * @param withWhatFormat if the given MASK is greater than 0, Log with Date/Timestamp
258      */

259     public static void log(Object JavaDoc o, int withWhatFormat)
260     {
261         // if LOG was given
262
if (LOG)
263         {
264             PrintWriter JavaDoc logfile;
265             if ((logfile = getLogFile()) == null)
266             {
267                 logfile = createLogFile();
268             }
269
270             if (logfile != null)
271             {
272                 if (o == null)
273                 {
274                     o = "null";
275                 }
276
277                 StringBuffer JavaDoc entry = new StringBuffer JavaDoc();
278                 if (logWithTimeStamp(withWhatFormat))
279                 {
280                     entry.append(System.currentTimeMillis());
281                     entry.append(';');
282                     entry.append(' ');
283                 }
284                 if (logWithDate(withWhatFormat))
285                 {
286                     entry.append(new Date JavaDoc());
287                     entry.append(';');
288                     entry.append(' ');
289                 }
290
291                 entry.append(o);
292
293                 logfile.println(entry.toString());
294
295                 if (o instanceof Throwable JavaDoc)
296                 {
297                     ((Throwable JavaDoc) o).printStackTrace(logfile);
298                 }
299
300                 logfile.flush();
301
302                 // logfile.close();
303
// logFile = null;
304
}
305             else
306             {
307                 System.err.println("Cannot write into logfile: (" + logfile + ") <- '" + o + "'");
308             }
309         }
310     }
311
312     /**
313      * Indicates that to log with Date.
314      *
315      * @param withWhatFormat The whished Format
316      * @return true if to log with Date
317      */

318     private static boolean logWithDate(int withWhatFormat)
319     {
320
321         return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
322     }
323
324     /**
325      * Indicates that to log with Timestamp.
326      *
327      * @param withWhatFormat The whished Format
328      * @return true if to log with Timestamp
329      */

330     private static boolean logWithTimeStamp(int withWhatFormat)
331     {
332
333         return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
334     }
335
336     /**
337      * Creates the logfile to write log-infos into.
338      *
339      * @return The writer object instance
340      */

341     private static PrintWriter JavaDoc createLogFile()
342     {
343         String JavaDoc tempDir = System.getProperty("java.io.tmpdir");
344
345         File JavaDoc tempDirFile = new File JavaDoc(tempDir);
346
347         try
348         {
349             tempDirFile.mkdirs();
350         }
351         catch (RuntimeException JavaDoc e1)
352         {
353             e1.printStackTrace();
354         }
355
356         String JavaDoc logfilename = LOGFILENAME;
357         System.out.println("creating Logfile: '" + logfilename + "' in: '" + tempDir + "'");
358
359         File JavaDoc out = new File JavaDoc(tempDir, logfilename);
360
361         PrintWriter JavaDoc logfile;
362         if (tempDirFile.canWrite())
363         {
364             try
365             {
366                 BufferedWriter JavaDoc fw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(
367                         out), "UTF-8"));
368                 logfile = setLogFile(new PrintWriter JavaDoc(fw));
369             }
370             catch (Exception JavaDoc e)
371             {
372                 logfile = null;
373                 e.printStackTrace();
374             }
375         }
376         else
377         {
378             logfile = null;
379             System.err.println("Fatal: cannot write File: '" + logfilename + "' into: "
380                     + tempDirFile);
381         }
382
383         return logfile;
384     }
385
386     /**
387      * Indicates if debug is tracing
388      *
389      * @return true if tracing otherwise false
390      */

391     public static boolean tracing()
392     {
393         return TRACE;
394     }
395
396     /**
397      * Indicates if debug is stacktracing
398      *
399      * @return true if stacktracing otherwise false
400      */

401     public static boolean stackTracing()
402     {
403         return STACKTRACE;
404     }
405
406     /**
407      * Returns the LOG flag.
408      *
409      * @return Returns the LOG flag.
410      */

411     public static boolean isLOG()
412     {
413         return LOG;
414     }
415
416     /**
417      * Sets The LOG like the given value
418      *
419      * @param aFlag The LOG status to set to or not.
420      */

421     public static void setLOG(boolean aFlag)
422     {
423         System.out.println(DLOG + " = " + aFlag);
424         LOG = aFlag;
425     }
426
427     /**
428      * Returns the current STACKTRACE flag
429      *
430      * @return Returns the STACKTRACE.
431      */

432     public static boolean isSTACKTRACE()
433     {
434         return STACKTRACE;
435     }
436
437     /**
438      * Sets the STACKTRACE like the given value
439      *
440      * @param aFlag The STACKTRACE to set / unset.
441      */

442     public static void setSTACKTRACE(boolean aFlag)
443     {
444         System.out.println(DSTACKTRACE + " = " + aFlag);
445         STACKTRACE = aFlag;
446     }
447
448     /**
449      * Gets the current TRACE flag
450      *
451      * @return Returns the TRACE.
452      */

453     public static boolean isTRACE()
454     {
455         return TRACE;
456     }
457
458     /**
459      * Sets the TRACE flag like the given value
460      *
461      * @param aFlag The TRACE to set / unset.
462      */

463     public static void setTRACE(boolean aFlag)
464     {
465         System.out.println(DTRACE + " = " + aFlag);
466         TRACE = aFlag;
467     }
468
469     /**
470      * Get the Logfile
471      *
472      * @return Returns the logFile.
473      */

474     public static PrintWriter JavaDoc getLogFile()
475     {
476         PrintWriter JavaDoc logfile = (PrintWriter JavaDoc) System.getProperties().get(IZPACK_LOGFILE);
477
478         return logfile;
479     }
480
481     /**
482      * Sets the Logfile
483      *
484      * @param aLogFile The logFile to set. *
485      * @return The logfile to write into
486      */

487     public static synchronized PrintWriter JavaDoc setLogFile(PrintWriter JavaDoc aLogFile)
488     {
489         System.getProperties().put(IZPACK_LOGFILE, aLogFile);
490
491         PrintWriter JavaDoc logfile = (PrintWriter JavaDoc) System.getProperties().get(IZPACK_LOGFILE);
492
493         if (logfile == null)
494         {
495             System.err.println("Set::logfile == null");
496         }
497
498         return logfile;
499     }
500 }
501
Popular Tags