Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / Debug.java @ 15280

History | View | Annotate | Download (2.95 KB)

1
/*
2
 *  $Id: Debug.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2002 Jan Blok (jblok@profdata.nl - PDM - www.profdata.nl)
5
 *
6
 *  File :               Debug.java
7
 *  Description :        a ButtonFactory.
8
 *  Author's email :     jblok@profdata.nl
9
 *  Author's Website :   http://www.profdata.nl
10
 *
11
 *  This program is free software; you can redistribute it and/or
12
 *  modify it under the terms of the GNU General Public License
13
 *  as published by the Free Software Foundation; either version 2
14
 *  of the License, or any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25
package com.izforge.izpack.util;
26

    
27
import java.io.File;
28
import java.io.FileWriter;
29
import java.io.PrintWriter;
30

    
31
public class Debug
32
{
33
        private static final boolean TRACE;
34
        private static final boolean STACKTRACE;
35
        public static boolean LOG;
36
        
37
        static 
38
        {
39
                boolean st = false;
40
                try
41
                {
42
                         st = Boolean.getBoolean ("STACKTRACE");
43
                }
44
                catch(Exception ex)
45
                {
46
                        //ignore
47
                }
48
                STACKTRACE = st;
49
                
50
                boolean t = false;
51
                try
52
                {
53
                        if (STACKTRACE)
54
                        {
55
                                t = true;
56
                        }
57
                        else
58
                        {
59
                                t = Boolean.getBoolean ("TRACE");
60
                        }
61
                }
62
                catch(Exception ex)
63
                {
64
                        //ignore
65
                }
66
                TRACE = t;
67
        }
68
        
69
        public static void trace(Object s)
70
        {
71
                if (TRACE)
72
                {
73
//                        console.println(s.toString());
74
                        System.out.println(s);
75
                        if(STACKTRACE && (s instanceof Throwable ))
76
                        {
77
//                                StringWriter sw = new StringWriter();
78
//                                PrintWriter pw = new PrintWriter(sw);
79
//                                ((Throwable)s).printStackTrace(pw);
80
//                                console.println(sw.toString());
81
                                ((Throwable)s).printStackTrace();
82
                        }
83
                        System.out.flush();
84
                }
85
        }
86

    
87
        public static void error(Object s)
88
        {
89
                trace(s);
90
                System.err.println(s);
91
                System.err.flush();
92
                
93
                if (LOG && logFile == null) createLogFile();
94
                if (LOG && logFile != null && s != null)
95
                {
96
                        logFile.print(s);
97
                        if(s instanceof Throwable )
98
                        {
99
                                ((Throwable)s).printStackTrace(logFile);
100
                        }
101
                        logFile.flush();
102
                }
103
        }
104
        
105
        private static PrintWriter logFile;
106
        private static void createLogFile()
107
        {
108
                try
109
                {
110
                        File out = new File(System.getProperty("user.dir"),".log.txt");
111
                        if (out.canWrite())
112
                        {
113
                                if (out.exists()) out.delete();
114
                                FileWriter fw = new FileWriter(out);
115
                                logFile = new PrintWriter(fw);
116
                        }
117
                }
118
                catch (Throwable e)
119
                {
120
                        System.err.println(e);
121
                        System.err.flush();
122
                }
123
        }
124
        
125
        public static boolean tracing()
126
        {
127
                return TRACE;
128
        }
129
  
130
  public static boolean stackTracing()
131
  {
132
    return STACKTRACE;
133
  }
134
}