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

Last change on this file since 250 was 250, checked in by sherbold, 13 years ago
  • various changed to de.ugoe.cs.util.Console:
    • changed instantiation method of the instance, i.e., how the Singleton property is guaranteed
    • added method reset() to remove all listeners at once
    • added functions to check is a given listener is registered
  • various changes to de.ugoe.cs.util.FileOutputListener?:
    • added null-checks for writer to improve behavior in case of improper usage
    • added method getFilename() to check to which file the listener writes.
  • added method setDebug() to de.ugoe.cs.util.TextConsole? to define whether trace stream is displayed or not.
  • Property svn:mime-type set to text/plain
File size: 2.7 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 = null;
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                if( writer!=null ) {
81                        try {
82                                writer.close();
83                                writer = null;
84                        } catch (IOException e) {
85                                Console.printerrln("Failed to close file " + filename + ": "
86                                                + e.getMessage());
87                        }
88                }
89        }
90
91        /*
92         * (non-Javadoc)
93         *
94         * @see
95         * de.ugoe.cs.util.console.listener.IOutputListener#outputMsg(java.lang.
96         * String)
97         */
98        @Override
99        public void outputMsg(String newMessage) {
100                if( writer!=null ) {
101                        try {
102                                writer.write(newMessage);
103                        } catch (IOException e) {
104                                if (!failureLogged) {
105                                        Console.printerrln("FileOutpustListener for file " + filename
106                                                        + " broken: " + e.getMessage());
107                                        failureLogged = true;
108                                }
109                        }
110                }
111        }
112
113        /**
114         * <p>
115         * Returns the name of the log file used by this listener.
116         * </p>
117         *
118         * @return name of the log file
119         */
120        public String getFilename() {
121                return filename;
122        }
123
124}
Note: See TracBrowser for help on using the repository browser.