source: trunk/yao/share/antlr-2.7.7/examples/java/unicode.IDENTs/Debug.java @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 5.3 KB
Line 
1import java.io.*;
2import java.util.*;
3import javax.swing.*;
4import javax.swing.text.*;
5
6import java.awt.*;              //for layout managers
7import java.awt.event.*;        //for action and window events
8
9import java.net.URL;
10import java.io.IOException;
11import java.io.OutputStreamWriter;
12
13/**
14 * Creates the debug log file (and window)
15 *
16 * @version     0.2 11th Sep 2000
17 * @author      Dr. M.P. Ford (c)2000 Forward Computing and Control Pty. Ltd.
18 *   NSW Australia  email fcc@forward.com.au
19 * You may use this class without restriction.
20 */
21
22/**
23 * This class displays all System.out and System.err text in an JTextPanel
24 * and saves it to a log file using the specified encodeing
25 */
26 
27public class Debug extends PrintStream {
28
29        private static String logFile = "debug.log";
30        private static String encoding = "UTF8";
31  private static boolean DEBUG_WINDOW = true;
32       
33        // find new line chars for this OS
34        private static final String newLine = System.getProperty("line.separator","\n");
35
36  public static Debug log;
37  private static JFrame frame;
38  private static JTextPane textPane;
39  private static Document doc;
40  private static OutputStreamWriter writer;
41   
42  static {
43                try {
44                                System.out.println("Redirecting all output to file " +logFile +" in "+ encoding +" format");
45                    log = new Debug(new FileOutputStream(logFile));
46                } catch(IOException ex) {
47                    System.err.println("Could not open Debug file " + logFile);
48                    System.exit(1);
49                }
50    }
51           
52
53    private Debug(FileOutputStream logFile) {
54            super(logFile,true);  // autoflush
55            try {
56                        writer = new OutputStreamWriter(logFile,encoding);
57                } catch (UnsupportedEncodingException ex) {
58                        System.out.println("Invalid encoding");
59                        System.exit(1);
60                }
61            System.setErr(this);
62            System.setOut(this);
63           
64            if (DEBUG_WINDOW) {
65                     frame = new JFrame("Debug Output");
66                                        textPane = createTextPane();
67                                        frame.getContentPane().add(new JScrollPane(textPane));
68                     frame.addWindowListener(new WindowAdapter() {
69                            public void windowClosing(WindowEvent e) {
70                                System.exit(0);
71                            }
72                     });
73                       
74                     frame.pack();
75                     frame.setVisible(true);
76            }
77    System.err.println("Debug Log file: " + new Date());
78                System.err.println("Debug class by Matthew Ford  email:matthew.ford@forward.com.au");
79                System.err.println("(c)2000 Forward Computing and Control Pty. Ltd.");
80                System.err.println("   NSW, Australia,  www.forward.com.au");
81                System.err.println(" You may use this class without restriction");
82                System.err.println("---------------------------------------------------------------");
83    }
84   
85
86    private static JTextPane createTextPane() {
87        JTextPane textPane = new JTextPane();
88        textPane.setEditable(false);
89                                 initStylesForTextPane(textPane);
90        doc = textPane.getDocument();
91        textPane.setPreferredSize(new Dimension(500,500));
92        return textPane;
93    }
94
95   
96    public void print(boolean b) {
97        this.print(new Boolean(b).toString());
98    }
99    public void print(double d) {
100        this.print(new Double(d).toString());
101    }
102    public void print(long l) {
103        this.print(new Long(l).toString());
104    }
105
106    public void println(boolean b) {
107        this.print(b);
108        this.println();
109    }
110    public void println(double d) {
111        this.print(d);
112        this.println();
113    }
114    public void println(long l) {
115        this.print(l);
116        this.println();
117    }
118   
119   
120    public void print(char c) {
121        char c_array[] = new char[1];
122        c_array[0] = c;
123        this.print(new String(c_array));
124    }
125   
126    public void print(char c[]) {
127                this.print(new String(c));
128    }
129
130    public void println(char c) {
131        this.print(c);
132        this.println();
133    }
134   
135    public void println(char c[]) {
136        this.print(c);
137        this.println();
138    }
139
140    public void println(String x) {
141        this.print(x);
142                        this.println();
143    }
144
145    public void print(String x) {
146        // add it the debug frame and also the log file
147        try {
148                writer.write(x,0,x.length());
149        } catch (IOException ex) {
150                x += ex.getMessage();
151        }
152       
153            if (DEBUG_WINDOW) {
154                                try {
155                                doc.insertString(doc.getLength(), x,
156                                 textPane.getStyle("regular"));
157                                } catch (BadLocationException ble) {
158                        System.exit(2);  // cannot write to System.err here
159                                }
160            }
161    }
162   
163
164   
165    public void println() {
166        // start newline in debug frame and also the log file
167        String x = "\n";
168        try {
169                writer.write(newLine,0,newLine.length());
170                writer.flush();
171        } catch (IOException ex) {
172                x += ex.getMessage();
173        }
174            if (DEBUG_WINDOW) {
175                                try {
176                                doc.insertString(doc.getLength(), "\n",
177                                 textPane.getStyle("regular"));
178                                } catch (BadLocationException ble) {
179                        System.exit(2); // cannot write to System.err here
180                                }
181                        }       
182    }   
183   
184    private static void initStylesForTextPane(JTextPane textPane) {
185        //Initialize some styles.
186        Style def = StyleContext.getDefaultStyleContext().
187                                        getStyle(StyleContext.DEFAULT_STYLE);
188
189        Style regular = textPane.addStyle("regular", def);
190        StyleConstants.setFontFamily(def, "SansSerif");
191    }   
192   
193}
Note: See TracBrowser for help on using the repository browser.