Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / debugger / JDE.java
1 package jde.debugger;
2
3 import java.io.OutputStream;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.io.Writer;
7 import java.text.DateFormat;
8 import java.util.Date;
9
10 /**
11  * JDE.java
12  *
13  *
14  * Created: Thu Feb 15 12:58:59 2001
15  *
16  * @author Paul Kinnucan
17  * @version $Revision: 1.8 $
18  */
19
20 public class JDE implements Protocol {
21
22   /**
23    * The single instance of this class.
24    *
25    */
26   private static JDE s_jde = new JDE(System.out);
27
28   /**
29    * Writes command responses, messages, and event notifications to
30    * JDE.
31    */
32   private PrintWriter m_out;
33
34   /** logical OR of the debug flags that should be used in this particular session */
35   private int m_debugFlags
36     = APP_IO
37 //     | JDE_PIPE
38 //     | EVENTS
39     | EXCEPTION
40 //     | FRAMEWORK
41 //     | COMMANDS
42     | GUI
43     ;
44
45   private JDE(OutputStream out) {
46     m_out = new PrintWriter(out, true);
47
48   }
49
50
51   /*
52    * Private methods ----------------------------------------------------
53    */
54
55
56   private synchronized void p_transmit(String type, String message) {
57     m_out.println("(" + JDE_BUG + type + " " + message + ")");
58   }
59
60   private void p_setDebugFlags(int flags) {
61     m_debugFlags = flags;
62   }
63
64   private void p_debug(int flag, String message) {
65     if ((m_debugFlags & flag) != 0) {
66       p_transmit(DEBUG, " \"" +
67                  DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date()) + ": " +
68                  message + "\"");
69     }
70   }
71
72   /*
73    * Public, static interface  --------------------------------------------------
74    */
75
76   /**
77    * To indicate which of the debugging flags should be set. If a
78    * flag is set, it means that calls to <code>JDE.debug()</code>
79    * with that flag will actually be printed.
80    *
81    * @param flags an <code>int</code> value with a bitwise OR of the
82    * types of information that are desired.
83    * @see Protocol#NONE
84    */
85   public static void setDebugFlags(int flags) {
86     s_jde.p_setDebugFlags(flags);
87   }
88
89   /**
90    * Send a debugging message to Emacs, for display in the
91    * <code>*JDEbug*</code> buffer. The message will only be printed
92    * if the flag that is passed in the first argument is set. The
93    * current time is prepended to the message string.
94    *
95    * @param flag an <code>int</code> value
96    * @param message a <code>String</code> value
97    * @see Protocol
98    * @see #setDebugFlags
99    */
100   public static void debug(int flag, String message) {
101     s_jde.p_debug(flag, message);
102   }
103
104   /**
105    * Sends the result of executing a debugger command to
106    * Emacs. Depending on wether the command was successful or not,
107    * different lisp code will be generated. If the
108    * <code>quote</code> argument is set ({@link Protocol#QUOTE}),
109    * double quotes will be added around the message string. The idea
110    * is that if the command result should be a Lisp string, you
111    * should set the <code>quote</code> argument to get the double
112    * quotes, and otherwise not.
113    *
114    * @param cmdID   the ID of the command
115    * @param message a <code>String</code> value
116    * @param success indicates what to generate, {@link
117    * Protocol#COMMAND_RESULT} or {@link Protocol#COMMAND_ERROR}
118    * @param quote a <code>boolean</code> value
119    */
120   public static void commandResult(Integer cmdID, String message, boolean success, boolean quote) {
121     String type = success ? COMMAND_RESULT : COMMAND_ERROR;
122
123     StringBuffer messageBuffer = new StringBuffer(cmdID.toString());
124
125     if (message != null && message.length() > 0) {
126       messageBuffer.append(" ");
127       if (quote) {
128         messageBuffer.append("\"");
129       }
130       messageBuffer.append(message);
131       if (quote) {
132         messageBuffer.append("\"");
133       }
134     }
135
136     s_jde.p_transmit(type, messageBuffer.toString());
137   }
138
139
140   /**
141    * Equivalent to calling {@link #commandResult(Integer,
142    * String, boolean, boolean)} with the <code>quote</code> argument
143    * set to {@link Protocol#NOQUOTE}.
144    *
145    * @param cmdID an <code>Integer</code> value
146    * @param message a <code>String</code> value
147    * @param success a <code>boolean</code> value
148    */
149   public static void commandResult(Integer cmdID, String message, boolean success) {
150     commandResult(cmdID, message, success, NOQUOTE);
151   }
152
153   /**
154    * A method for sending more arbitrary information to Emacs. If
155    * the <code>quote</code> argument is set, double quotes will
156    * added around the message string.
157    *
158    * @param procID an <code>Integer</code> value
159    * @param type a <code>String</code> value indicating what Lisp
160    * command should be sent
161    * @param message a <code>String</code> value
162    * @param quote a <code>boolean</code> value
163    */
164   public static void signal(Integer procID, String type, String message, boolean quote) {
165     StringBuffer messageBuffer = new StringBuffer(procID.toString());
166
167     if (message != null && message.length() > 0) {
168       messageBuffer.append(" ");
169       if (quote) {
170         messageBuffer.append("\"");
171       }
172       messageBuffer.append(message);
173       if (quote) {
174         messageBuffer.append("\"");
175       }
176     }
177
178     s_jde.p_transmit(type, messageBuffer.toString());
179   }
180
181   /** A method for signaling JDEE that an exception occured.  The
182       message displayed in Emacs will include the stack trace. */
183   public static void signalException(Throwable exc) {
184     s_jde.p_signalException(exc);
185   }
186
187   private void p_signalException(Throwable exc) {
188     if ((m_debugFlags & EXCEPTION) != 0) {
189       Writer writer = new StringWriter();
190       exc.printStackTrace(new PrintWriter(writer));
191       signal(new Integer(-1), ERROR,
192              "Exception during command execution: " +
193              writer.toString(),
194              QUOTE);
195     }
196   }
197
198
199   /**
200    * Equivalent to calling {@link #signal(Integer,
201    * String, String, boolean)} with the <code>quote</code> argument
202    * set to false.
203    *
204    * @param procID an <code>Integer</code> value
205    * @param type a <code>String</code> value
206    * @param message a <code>String</code> value
207    */
208   public static void signal(Integer procID, String type, String message) {
209     signal(procID, type, message, NOQUOTE);
210   }
211 }// JDE
212
213 /*
214  * $Log: JDE.java,v $
215  * Revision 1.8  2004/12/24 16:05:13  troy
216  * Add window to display threads and stacks
217  *
218  * Revision 1.7  2003/09/21 05:23:11  paulk
219  * Fix bug in setDebugFlags (it was calling itself).
220  *
221  * Revision 1.6  2003/04/29 16:51:56  troy
222  * Initial version of GUI.  Includes display of local variables.
223  *
224  * Revision 1.5  2003/01/15 05:50:51  paulk
225  * Remove CRs.
226  *
227  * Revision 1.4  2003/01/08 06:53:38  paulk
228  * Integrate Petter Mahlen's updates.
229  *
230  */
231
232 // End of JDE.java