KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > LogWriter


1 package gnu.mapping;
2 import java.io.*;
3
4 /** A class that supports an optional log file that output is duplicated to.
5   * This is used to implement the Scheme transcript facility. */

6
7 public class LogWriter extends FilterWriter
8 {
9   private Writer log;
10
11   public LogWriter (Writer out)
12   {
13     super (out);
14   }
15
16   public final Writer getLogFile () { return log; }
17
18   public void setLogFile (Writer log)
19   {
20     this.log = log;
21   }
22
23   public void setLogFile (String JavaDoc name) throws java.io.IOException JavaDoc
24   {
25     // try
26
{
27     log = new PrintWriter(new BufferedWriter(new FileWriter(name)));
28       }
29   // catch (??)
30
{
31       }
32   }
33
34   public void closeLogFile () throws java.io.IOException JavaDoc
35   {
36     if (log != null)
37       log.close();
38     log = null;
39   }
40
41   public void write (int c) throws java.io.IOException JavaDoc
42   {
43     if (log != null)
44       log.write(c);
45     super.write(c);
46   }
47
48   public void echo (char buf[], int off, int len) throws java.io.IOException JavaDoc
49   {
50     if (log != null)
51       log.write(buf, off, len);
52   }
53
54   public void write (char buf[], int off, int len) throws java.io.IOException JavaDoc
55   {
56     if (log != null)
57       log.write(buf, off, len);
58     super.write(buf, off, len);
59   }
60
61   public void write (String JavaDoc str, int off, int len) throws java.io.IOException JavaDoc
62   {
63     if (log != null)
64       log.write(str, off, len);
65     super.write(str, off, len);
66   }
67
68   public void flush () throws java.io.IOException JavaDoc
69   {
70     if (log != null)
71       log.flush();
72     super.flush();
73   }
74
75   public void close () throws java.io.IOException JavaDoc
76   {
77     if (log != null)
78       log.close();
79     super.close();
80   }
81 }
82
Popular Tags