source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/FileOutputListener.java @ 243

Last change on this file since 243 was 243, checked in by sherbold, 13 years ago
  • added de.ugoe.cs.util.console.FileOutputListener? that can listen to the output stream of the Console and pipe it into a file.
  • Property svn:mime-type set to text/plain
File size: 2.4 KB
Line 
1package de.ugoe.cs.util.console;
2
3import java.io.FileWriter;
4import java.io.IOException;
5
6import de.ugoe.cs.util.console.listener.IOutputListener;
7
8/**
9 * <p>
10 * Implements an {@link IOutputListener} for the {@link Console} that logs all
11 * outputs in a file. This can be used to "pipe" the output-stream of the
12 * console into a file. The advantage of using this mechanism for piping is that
13 * the file will only contain the output stream. No errors, no commands, etc.
14 * </p>
15 *
16 * @author Steffen Herbold
17 * @version 1.0
18 */
19public class FileOutputListener implements IOutputListener {
20
21        /**
22         * <p>
23         * Flag that ensures that only one log message is produced if the listener
24         * breaks, e.g., because of a full hard disk/quota.
25         * </p>
26         */
27        boolean failureLogged = false;
28
29        /**
30         * <p>
31         * Name of the output file.
32         * </p>
33         */
34        String filename;
35
36        /**
37         * <p>
38         * Writer for the output.
39         * </p>
40         */
41        FileWriter writer;
42
43        /**
44         * <p>
45         * Constructor. Creates a new FileOutputListener.
46         * </p>
47         *
48         * @param filename
49         *            name and path of the file the listener writes to.
50         */
51        public FileOutputListener(String filename) {
52                this.filename = filename;
53
54        }
55
56        /**
57         * <p>
58         * Starts the listener by opening the file and registering it with the
59         * {@link Console}.
60         * </p>
61         */
62        public void start() {
63                try {
64                        writer = new FileWriter(filename);
65                        Console.getInstance().registerOutputListener(this);
66                } catch (IOException e) {
67                        Console.printerrln("Failed to start FileOutputListener for file "
68                                        + filename + ": " + e.getMessage());
69                }
70        }
71
72        /**
73         * <p>
74         * Stops the listener by closing the file and removing itself from the
75         * {@link Console}.
76         * </p>
77         */
78        public void stop() {
79                Console.getInstance().removeOutputListener(this);
80                try {
81                        writer.close();
82                } catch (IOException e) {
83                        Console.printerrln("Failed to close file " + filename + ": "
84                                        + e.getMessage());
85                }
86        }
87
88        /*
89         * (non-Javadoc)
90         *
91         * @see
92         * de.ugoe.cs.util.console.listener.IOutputListener#outputMsg(java.lang.
93         * String)
94         */
95        @Override
96        public void outputMsg(String newMessage) {
97                try {
98                        writer.write(newMessage);
99                } catch (IOException e) {
100                        if (!failureLogged) {
101                                Console.printerrln("FileOutpustListener for file " + filename
102                                                + " broken: " + e.getMessage());
103                                failureLogged = true;
104                        }
105                }
106        }
107
108}
Note: See TracBrowser for help on using the repository browser.