Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/util/event/StdOutCaptureListener.java


1   
2   package com.eireneh.util.event;
3   
4   import java.io.*;
5   import com.eireneh.util.*;
6   
7   /**
8    * This class listens to Reporter captures and copies them to a
9    * stream.
10   *
11   * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
12   * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
13   * Distribution Licence:<br />
14   * Project B is free software; you can redistribute it
15   * and/or modify it under the terms of the GNU General Public License,
16   * version 2 as published by the Free Software Foundation.<br />
17   * This program is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   * General Public License for more details.<br />
21   * The License is available on the internet
22   * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
23   * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24   * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
25   * The copyright to this program is held by it's authors.
26   * </font></td></tr></table>
27   * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
28   * @see docs.Licence
29   * @author Joe Walker
30   */
31  public class StdOutCaptureListener implements CaptureListener, ReporterListener
32  {
33      /**
34       * Called whenever Reporter.informUser() is passed an Exception
35       * @param ev The event describing the Exception
36       */
37      public void captureException(CaptureEvent ev)
38      {
39          println(ev.getException());
40      }
41  
42      /**
43       * Called whenever Reporter.informUser() is passed a message
44       * @param ev The event describing the message
45       */
46      public void captureMessage(CaptureEvent ev)
47      {
48          out.println(ev.getMessage());
49      }
50  
51      /**
52       * Called whenever Reporter.informUser() is passed an Exception
53       * @param ev The event describing the Exception
54       */
55      public void reportException(ReporterEvent ev)
56      {
57          println(ev.getException());
58      }
59  
60      /**
61       * Called whenever Reporter.informUser() is passed a message
62       * @param ev The event describing the message
63       */
64      public void reportMessage(ReporterEvent ev)
65      {
66          out.println(ev.getMessage());
67      }
68  
69      /**
70       * Actually println the Throwable and recurse if needed
71       * @param ex The exception to be displayed
72       */
73      private void println(Throwable ex)
74      {
75          ex.printStackTrace(out);
76  
77          if (ex instanceof LucidException)
78          {
79              LucidException lex = (LucidException) ex;
80              Throwable nex = lex.getException();
81              if (nex != null)
82              {
83                  out.println();
84                  out.println("This was caused by:");
85                  println(nex);
86              }
87          }
88      }
89  
90      /** The stream to log to */
91      private PrintStream out = System.out;
92  
93      /**
94       * You must call setHelpDeskListener() in order to start displaying
95       * Exceptions sent to the Log, and in order to properly
96       * close this class you must call it again (with false).
97       * @param joined Are we listening to the Log
98       */
99      public static void setHelpDeskInformListener(boolean joined)
100     {
101         if (joined && inform == null)
102         {
103             inform = new StdOutCaptureListener();
104             Reporter.addReporterListener(inform);
105         }
106 
107         if (!joined && inform != null)
108         {
109             Reporter.removeReporterListener(inform);
110             inform = null;
111         }
112     }
113 
114     /**
115      * Get the listening status
116      */
117     public static boolean getHelpDeskInformListener()
118     {
119         return (inform != null);
120     }
121 
122     /**
123      * You must call setHelpDeskListener() in order to start logging
124      * messages sent to the Log, and in order to properly
125      * close this class you must call it again (with false).
126      * @param joined Are we listening to the Log
127      */
128     public static void setHelpDeskLogListener(boolean joined)
129     {
130         if (joined && logger == null)
131         {
132             logger = new StdOutCaptureListener();
133             Logger.addLogCaptureListener(logger);
134         }
135 
136         if (!joined && logger != null)
137         {
138             Logger.removeLogCaptureListener(logger);
139             logger = null;
140         }
141     }
142 
143     /**
144      * Get the listening status
145      */
146     public static boolean getHelpDeskLogListener()
147     {
148         return (logger != null);
149     }
150 
151     /** The listener for the logging service */
152     private static StdOutCaptureListener logger = null;
153 
154     /** The listener for the inform service */
155     private static StdOutCaptureListener inform = null;
156 
157     /**
158      * Make the default to log exceptions to std out
159      */
160     static
161     {
162         // StdOutCaptureListener.setHelpDeskListener(true);
163     }
164 }