1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * OutputLogger.java |
---|
19 | * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.core.logging; |
---|
23 | |
---|
24 | import weka.core.RevisionUtils; |
---|
25 | import weka.core.Tee; |
---|
26 | |
---|
27 | import java.io.PrintStream; |
---|
28 | import java.util.Date; |
---|
29 | |
---|
30 | /** |
---|
31 | * A logger that logs all output on stdout and stderr to a file. |
---|
32 | * |
---|
33 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
34 | * @version $Revision: 4716 $ |
---|
35 | */ |
---|
36 | public class OutputLogger |
---|
37 | extends FileLogger { |
---|
38 | |
---|
39 | /** |
---|
40 | * A print stream class to capture all data from stdout and stderr. |
---|
41 | * |
---|
42 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
43 | * @version $Revision: 4716 $ |
---|
44 | */ |
---|
45 | public static class OutputPrintStream |
---|
46 | extends PrintStream { |
---|
47 | |
---|
48 | /** the owning logger. */ |
---|
49 | protected OutputLogger m_Owner; |
---|
50 | |
---|
51 | /** the line feed. */ |
---|
52 | protected String m_LineFeed; |
---|
53 | |
---|
54 | /** |
---|
55 | * Default constructor. |
---|
56 | * |
---|
57 | * @param owner the owning logger |
---|
58 | * @param stream the stream |
---|
59 | * @throws Exception if something goes wrong |
---|
60 | */ |
---|
61 | public OutputPrintStream(OutputLogger owner, PrintStream stream) throws Exception { |
---|
62 | super(stream); |
---|
63 | |
---|
64 | m_Owner = owner; |
---|
65 | m_LineFeed = System.getProperty("line.separator"); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * ignored. |
---|
70 | */ |
---|
71 | public void flush() { |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * prints the given int to the streams. |
---|
76 | * |
---|
77 | * @param x the object to print |
---|
78 | */ |
---|
79 | public void print(int x) { |
---|
80 | m_Owner.append("" + x); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * prints the given boolean to the streams. |
---|
85 | * |
---|
86 | * @param x the object to print |
---|
87 | */ |
---|
88 | public void print(boolean x) { |
---|
89 | m_Owner.append("" + x); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * prints the given string to the streams. |
---|
94 | * |
---|
95 | * @param x the object to print |
---|
96 | */ |
---|
97 | public void print(String x) { |
---|
98 | m_Owner.append("" + x); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * prints the given object to the streams. |
---|
103 | * |
---|
104 | * @param x the object to print |
---|
105 | */ |
---|
106 | public void print(Object x) { |
---|
107 | m_Owner.append("" + x); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * prints a new line to the streams. |
---|
112 | */ |
---|
113 | public void println() { |
---|
114 | m_Owner.append(m_LineFeed); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * prints the given int to the streams. |
---|
119 | * |
---|
120 | * @param x the object to print |
---|
121 | */ |
---|
122 | public void println(int x) { |
---|
123 | m_Owner.append(x + m_LineFeed); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * prints the given boolean to the streams. |
---|
128 | * |
---|
129 | * @param x the object to print |
---|
130 | */ |
---|
131 | public void println(boolean x) { |
---|
132 | m_Owner.append(x + m_LineFeed); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * prints the given string to the streams. |
---|
137 | * |
---|
138 | * @param x the object to print |
---|
139 | */ |
---|
140 | public void println(String x) { |
---|
141 | m_Owner.append(x + m_LineFeed); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * prints the given object to the streams (for Throwables we print the stack |
---|
146 | * trace). |
---|
147 | * |
---|
148 | * @param x the object to print |
---|
149 | */ |
---|
150 | public void println(Object x) { |
---|
151 | m_Owner.append(x + m_LineFeed); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /** the stream object used for logging stdout. */ |
---|
156 | protected OutputPrintStream m_StreamOut; |
---|
157 | |
---|
158 | /** the stream object used for logging stderr. */ |
---|
159 | protected OutputPrintStream m_StreamErr; |
---|
160 | |
---|
161 | /** the Tee instance to redirect stdout. */ |
---|
162 | protected Tee m_StdOut; |
---|
163 | |
---|
164 | /** the Tee instance to redirect stderr. */ |
---|
165 | protected Tee m_StdErr; |
---|
166 | |
---|
167 | /** |
---|
168 | * Initializes the logger. |
---|
169 | */ |
---|
170 | protected void initialize() { |
---|
171 | super.initialize(); |
---|
172 | |
---|
173 | try { |
---|
174 | m_StdOut = new Tee(System.out); |
---|
175 | System.setOut(m_StdOut); |
---|
176 | m_StreamOut = new OutputPrintStream(this, m_StdOut.getDefault()); |
---|
177 | m_StdOut.add(m_StreamOut); |
---|
178 | |
---|
179 | m_StdErr = new Tee(System.err); |
---|
180 | System.setErr(m_StdErr); |
---|
181 | m_StreamErr = new OutputPrintStream(this, m_StdErr.getDefault()); |
---|
182 | m_StdErr.add(m_StreamErr); |
---|
183 | } |
---|
184 | catch (Exception e) { |
---|
185 | // ignored |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Performs the actual logging. |
---|
191 | * |
---|
192 | * @param level the level of the message |
---|
193 | * @param msg the message to log |
---|
194 | * @param cls the classname originating the log event |
---|
195 | * @param method the method originating the log event |
---|
196 | * @param lineno the line number originating the log event |
---|
197 | */ |
---|
198 | protected void doLog(Level level, String msg, String cls, String method, int lineno) { |
---|
199 | // append output to file |
---|
200 | append( |
---|
201 | m_DateFormat.format(new Date()) + " " + cls + " " + method + m_LineFeed |
---|
202 | + level + ": " + msg + m_LineFeed); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Returns the revision string. |
---|
207 | * |
---|
208 | * @return the revision |
---|
209 | */ |
---|
210 | public String getRevision() { |
---|
211 | return RevisionUtils.extract("$Revision: 4716 $"); |
---|
212 | } |
---|
213 | } |
---|