1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.util.console;
|
---|
16 |
|
---|
17 | import java.io.FileOutputStream;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.io.OutputStreamWriter;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p>
|
---|
25 | * Implements an {@link IOutputListener} for the {@link Console} that logs all
|
---|
26 | * outputs in a file. This can be used to "pipe" the output-stream of the
|
---|
27 | * console into a file. The advantage of using this mechanism for piping is that
|
---|
28 | * the file will only contain the output stream. No errors, no commands, etc.
|
---|
29 | * </p>
|
---|
30 | *
|
---|
31 | * @author Steffen Herbold
|
---|
32 | * @version 1.0
|
---|
33 | */
|
---|
34 | public class FileOutputListener implements IOutputListener {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * <p>
|
---|
38 | * Flag that ensures that only one log message is produced if the listener
|
---|
39 | * breaks, e.g., because of a full hard disk/quota.
|
---|
40 | * </p>
|
---|
41 | */
|
---|
42 | boolean failureLogged = false;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * <p>
|
---|
46 | * Name of the output file.
|
---|
47 | * </p>
|
---|
48 | */
|
---|
49 | String filename;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * <p>
|
---|
53 | * Writer for the output.
|
---|
54 | * </p>
|
---|
55 | */
|
---|
56 | OutputStreamWriter writer = null;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * <p>
|
---|
60 | * Constructor. Creates a new FileOutputListener.
|
---|
61 | * </p>
|
---|
62 | *
|
---|
63 | * @param filename
|
---|
64 | * name and path of the file the listener writes to.
|
---|
65 | */
|
---|
66 | public FileOutputListener(String filename) {
|
---|
67 | this.filename = filename;
|
---|
68 |
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p>
|
---|
73 | * Starts the listener by opening the file and registering it with the
|
---|
74 | * {@link Console}.
|
---|
75 | * </p>
|
---|
76 | */
|
---|
77 | public void start() {
|
---|
78 | try {
|
---|
79 | FileOutputStream fos = new FileOutputStream(filename);
|
---|
80 | writer = new OutputStreamWriter(fos, "UTF-8");
|
---|
81 | Console.getInstance().registerOutputListener(this);
|
---|
82 | } catch (IOException e) {
|
---|
83 | Console.printerrln("Failed to start FileOutputListener for file "
|
---|
84 | + filename + ": " + e.getMessage());
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * <p>
|
---|
90 | * Stops the listener by closing the file and removing itself from the
|
---|
91 | * {@link Console}.
|
---|
92 | * </p>
|
---|
93 | */
|
---|
94 | public void stop() {
|
---|
95 | Console.getInstance().removeOutputListener(this);
|
---|
96 | if( writer!=null ) {
|
---|
97 | try {
|
---|
98 | writer.close();
|
---|
99 | writer = null;
|
---|
100 | } catch (IOException e) {
|
---|
101 | Console.printerrln("Failed to close file " + filename + ": "
|
---|
102 | + e.getMessage());
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * (non-Javadoc)
|
---|
109 | *
|
---|
110 | * @see
|
---|
111 | * de.ugoe.cs.util.console.listener.IOutputListener#outputMsg(java.lang.
|
---|
112 | * String)
|
---|
113 | */
|
---|
114 | @Override
|
---|
115 | public void outputMsg(String newMessage) {
|
---|
116 | if( writer!=null ) {
|
---|
117 | try {
|
---|
118 | writer.write(newMessage);
|
---|
119 | } catch (IOException e) {
|
---|
120 | if (!failureLogged) {
|
---|
121 | Console.printerrln("FileOutpustListener for file " + filename
|
---|
122 | + " broken: " + e.getMessage());
|
---|
123 | failureLogged = true;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * <p>
|
---|
131 | * Returns the name of the log file used by this listener.
|
---|
132 | * </p>
|
---|
133 | *
|
---|
134 | * @return name of the log file
|
---|
135 | */
|
---|
136 | public String getFilename() {
|
---|
137 | return filename;
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|