001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2002 Jan Blok
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.util;
022:
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.OutputStreamWriter;
027: import java.io.PrintWriter;
028: import java.util.Date;
029: import java.util.Enumeration;
030: import java.util.Properties;
031:
032: import com.izforge.izpack.installer.Installer;
033:
034: /**
035: * This class is for debug purposes. It is highly recommended to use it on critical or experimental
036: * code places. To enable the debug mode of IzPack, just start the installer with the java parameter
037: * -DTRACE=true or -DSTACKTRACE=true to enable extendend output of the internal status of critical
038: * objects. <br>
039: * How to use it as IzPack Setup Developer: <br>
040: * Just import this class and use one of the methods:
041: *
042: * <dl>
043: * <dt> Debug.trace( aCriticalObject ) </dt>
044: * <dd> - to print the status on console </dd>
045: * <dt> Debug.error( aCriticalObject ) </dt>
046: * <dd> - to print the status on console and<br>
047: * print the stacktrace of a supressed Exception. </dd>
048: * <dt> Additionally: </dt>
049: * <dd> if -DLOG is given the output will be written in the File see #LOGFILENAME in the users Home
050: * directory. </dd>
051: * </dl>
052: *
053: *
054: * @author Julien Ponge, Klaus Bartz, Marc Eppelmann
055: * @version $Revision: 2036 $ ($Id: Debug.java 2036 2008-02-09 11:14:05Z jponge $)
056: */
057: public class Debug {
058:
059: // ~ Static fields/initializers *********************************************************
060:
061: /**
062: * Parameter for public javacall "java -jar izpack.jar -DLOG" (Class.internal.variable: (DLOG =
063: * "LOG"))
064: */
065: public static final String DLOG = "LOG";
066:
067: /**
068: * Parameter for public javacall "java -jar izpack.jar -DSTACKTRACE" (Class.internal.variable:
069: * (DSTACKTRACE = "STACKTRACE"))
070: */
071: public static final String DSTACKTRACE = "STACKTRACE";
072:
073: /**
074: * Parameter for public javacall "java -jar izpack.jar -DTRACE" (Class.internal.variable:
075: * (DTRACE = "TRACE"))
076: */
077: public static final String DTRACE = "TRACE";
078:
079: /** System.Property Key: IZPACK_LOGFILE = "izpack.logfile" */
080: public static final String IZPACK_LOGFILE = "izpack.logfile";
081:
082: /** LOG_WITHOUT_DATE = 0 */
083: public static final int LOG_WITHOUT_DATE = 0;
084:
085: /** LOG_WITH_DATE = 1 */
086: public static final int LOG_WITH_DATE = 1;
087:
088: /** LOG_WITH_TIME_STAMP = 2 */
089: public static final int LOG_WITH_TIME_STAMP = 2;
090:
091: /** LOG_WITH_TIME_AND_DATE= LOG_WITH_DATE | LOG_WITH_TIME_STAMP = 3 */
092: public static final int LOG_WITH_TIME_AND_DATE = LOG_WITH_DATE
093: | LOG_WITH_TIME_STAMP;
094:
095: /** internally initial unintialized TRACE-flag */
096: private static boolean TRACE;
097:
098: /** internal initial unintialized STACKTRACE-flag */
099: 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 LOGFILE_PREFIX = "IzPack_Logfile_at_";
106:
107: /** LOGFILE_EXTENSION = ".txt" */
108: public static String LOGFILE_EXTENSION = ".txt";
109:
110: /** LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis() + LOGFILE_EXTENSION */
111: public static String LOGFILENAME = LOGFILE_PREFIX
112: + System.currentTimeMillis() + LOGFILE_EXTENSION;
113:
114: /**
115: * The log initializion bloc.
116: */
117: static {
118: boolean st = false;
119:
120: try {
121: st = Boolean.getBoolean(DSTACKTRACE);
122: } catch (Exception ex) {
123: // ignore
124: }
125:
126: STACKTRACE = st;
127:
128: boolean log = false;
129:
130: try {
131: log = Boolean.getBoolean(DLOG);
132: } catch (Exception ex) {
133: // ignore
134: }
135:
136: LOG = log;
137:
138: boolean t = false;
139:
140: try {
141: if (STACKTRACE) {
142: t = true;
143: } else {
144: t = Boolean.getBoolean(DTRACE);
145: }
146: } catch (Exception ex) {
147: // ignore
148: }
149:
150: TRACE = t;
151:
152: if (LOG) {
153: System.out.println(DLOG + " enabled.");
154: PrintWriter logfile = createLogFile();
155:
156: Debug.log(Installer.class.getName()
157: + " LogFile created at ");
158:
159: // ** write some runtime system properties into the logfile **
160: Debug.log("System.Properties:", LOG_WITH_TIME_STAMP);
161:
162: Properties sysProps = System.getProperties();
163:
164: Enumeration spe = sysProps.keys();
165:
166: while (spe.hasMoreElements()) {
167: String aKey = (String) spe.nextElement();
168: Debug.log(aKey + " = " + sysProps.getProperty(aKey),
169: LOG_WITHOUT_DATE);
170: }
171: Debug.log("\n==========================================\n",
172: LOG_WITHOUT_DATE);
173: Debug.log("\n " + Installer.class.getName()
174: + " installs on: \n", LOG_WITHOUT_DATE);
175: Debug.log(OsVersion.getOsDetails(), LOG_WITHOUT_DATE);
176: Debug.log("\n==========================================\n",
177: LOG_WITHOUT_DATE);
178: }
179:
180: if (TRACE) {
181: System.out.println(DTRACE + " enabled.");
182: }
183:
184: if (STACKTRACE) {
185: System.out.println(DSTACKTRACE + " enabled.");
186: }
187: }
188:
189: // ~ Methods ****************************************************************************
190:
191: /**
192: * Traces the internal status of the given Object
193: *
194: * @param s
195: */
196: public static void trace(Object s) {
197: if (TRACE) {
198: // console.println(s.toString());
199: System.out.println(s);
200:
201: if (STACKTRACE && (s instanceof Throwable)) {
202: // StringWriter sw = new StringWriter();
203: // PrintWriter pw = new PrintWriter(sw);
204: // ((Throwable)s).printStackTrace(pw);
205: // console.println(sw.toString());
206: ((Throwable) s).printStackTrace();
207: }
208:
209: System.out.flush();
210: }
211: }
212:
213: /**
214: * Traces the given object and additional write their status in the LOGFILE.
215: *
216: * @param s
217: */
218: public static void error(Object s) {
219: trace(s);
220: System.err.println(s);
221: System.err.flush();
222: log(s);
223: }
224:
225: /**
226: * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
227: * -DLOG=true -jar izpack-installer.jar
228: *
229: * @param o The Object to log, can be also an exception.
230: */
231: public static void log(Object o) {
232: log(o, LOG_WITH_TIME_AND_DATE);
233: }
234:
235: /**
236: * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
237: * -DLOG=true -jar izpack-installer.jar
238: *
239: * @param o The Object to log
240: * @param withWhatFormat if the given MASK is greater than 0, Log with Date/Timestamp
241: */
242: public static void log(Object o, int withWhatFormat) {
243: // if LOG was given
244: if (LOG) {
245: PrintWriter logfile;
246: if ((logfile = getLogFile()) == null) {
247: logfile = createLogFile();
248: }
249:
250: if (logfile != null) {
251: if (o == null) {
252: o = "null";
253: }
254:
255: StringBuffer entry = new StringBuffer();
256: if (logWithTimeStamp(withWhatFormat)) {
257: entry.append(System.currentTimeMillis());
258: entry.append(';');
259: entry.append(' ');
260: }
261: if (logWithDate(withWhatFormat)) {
262: entry.append(new Date());
263: entry.append(';');
264: entry.append(' ');
265: }
266:
267: entry.append(o);
268:
269: logfile.println(entry.toString());
270:
271: if (o instanceof Throwable) {
272: ((Throwable) o).printStackTrace(logfile);
273: }
274:
275: logfile.flush();
276:
277: // logfile.close();
278: // logFile = null;
279: } else {
280: System.err.println("Cannot write into logfile: ("
281: + logfile + ") <- '" + o + "'");
282: }
283: }
284: }
285:
286: /**
287: * Indicates that to log with Date.
288: *
289: * @param withWhatFormat The whished Format
290: * @return true if to log with Date
291: */
292: private static boolean logWithDate(int withWhatFormat) {
293:
294: return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
295: }
296:
297: /**
298: * Indicates that to log with Timestamp.
299: *
300: * @param withWhatFormat The whished Format
301: * @return true if to log with Timestamp
302: */
303: private static boolean logWithTimeStamp(int withWhatFormat) {
304:
305: return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
306: }
307:
308: /**
309: * Creates the logfile to write log-infos into.
310: *
311: * @return The writer object instance
312: */
313: private static PrintWriter createLogFile() {
314: String tempDir = System.getProperty("java.io.tmpdir");
315:
316: File tempDirFile = new File(tempDir);
317:
318: try {
319: tempDirFile.mkdirs();
320: } catch (RuntimeException e1) {
321: e1.printStackTrace();
322: }
323:
324: String logfilename = LOGFILENAME;
325: System.out.println("creating Logfile: '" + logfilename
326: + "' in: '" + tempDir + "'");
327:
328: File out = new File(tempDir, logfilename);
329:
330: PrintWriter logfile;
331: if (tempDirFile.canWrite()) {
332: try {
333: BufferedWriter fw = new BufferedWriter(
334: new OutputStreamWriter(
335: new FileOutputStream(out), "UTF-8"));
336: logfile = setLogFile(new PrintWriter(fw));
337: } catch (Exception e) {
338: logfile = null;
339: e.printStackTrace();
340: }
341: } else {
342: logfile = null;
343: System.err.println("Fatal: cannot write File: '"
344: + logfilename + "' into: " + tempDirFile);
345: }
346:
347: return logfile;
348: }
349:
350: /**
351: * Indicates if debug is tracing
352: *
353: * @return true if tracing otherwise false
354: */
355: public static boolean tracing() {
356: return TRACE;
357: }
358:
359: /**
360: * Indicates if debug is stacktracing
361: *
362: * @return true if stacktracing otherwise false
363: */
364: public static boolean stackTracing() {
365: return STACKTRACE;
366: }
367:
368: /**
369: * Returns the LOG flag.
370: *
371: * @return Returns the LOG flag.
372: */
373: public static boolean isLOG() {
374: return LOG;
375: }
376:
377: /**
378: * Sets The LOG like the given value
379: *
380: * @param aFlag The LOG status to set to or not.
381: */
382: public static void setLOG(boolean aFlag) {
383: System.out.println(DLOG + " = " + aFlag);
384: LOG = aFlag;
385: }
386:
387: /**
388: * Returns the current STACKTRACE flag
389: *
390: * @return Returns the STACKTRACE.
391: */
392: public static boolean isSTACKTRACE() {
393: return STACKTRACE;
394: }
395:
396: /**
397: * Sets the STACKTRACE like the given value
398: *
399: * @param aFlag The STACKTRACE to set / unset.
400: */
401: public static void setSTACKTRACE(boolean aFlag) {
402: System.out.println(DSTACKTRACE + " = " + aFlag);
403: STACKTRACE = aFlag;
404: }
405:
406: /**
407: * Gets the current TRACE flag
408: *
409: * @return Returns the TRACE.
410: */
411: public static boolean isTRACE() {
412: return TRACE;
413: }
414:
415: /**
416: * Sets the TRACE flag like the given value
417: *
418: * @param aFlag The TRACE to set / unset.
419: */
420: public static void setTRACE(boolean aFlag) {
421: System.out.println(DTRACE + " = " + aFlag);
422: TRACE = aFlag;
423: }
424:
425: /**
426: * Get the Logfile
427: *
428: * @return Returns the logFile.
429: */
430: public static PrintWriter getLogFile() {
431: PrintWriter logfile = (PrintWriter) System.getProperties().get(
432: IZPACK_LOGFILE);
433:
434: return logfile;
435: }
436:
437: /**
438: * Sets the Logfile
439: *
440: * @param aLogFile The logFile to set. *
441: * @return The logfile to write into
442: */
443: public static synchronized PrintWriter setLogFile(
444: PrintWriter aLogFile) {
445: System.getProperties().put(IZPACK_LOGFILE, aLogFile);
446:
447: PrintWriter logfile = (PrintWriter) System.getProperties().get(
448: IZPACK_LOGFILE);
449:
450: if (logfile == null) {
451: System.err.println("Set::logfile == null");
452: }
453:
454: return logfile;
455: }
456: }
|