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