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

Quick Search    Search Deep

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


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