1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.shared.invoker;
20
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 /**
26 * Offers a logger that writes to a print stream like {@link java.lang.System#out}.
27 *
28 * @since 2.0.9
29 */
30 public class PrintStreamLogger implements InvokerLogger {
31
32 /**
33 * The print stream to write to, never <code>null</code>.
34 */
35 private PrintStream out;
36
37 /**
38 * The threshold used to filter messages.
39 */
40 private int threshold;
41
42 /**
43 * Creates a new logger that writes to {@link java.lang.System#out} and has a threshold of {@link #INFO}.
44 */
45 public PrintStreamLogger() {
46 this(System.out, INFO);
47 }
48
49 /**
50 * Creates a new logger that writes to the specified print stream.
51 *
52 * @param out The print stream to write to, must not be <code>null</code>.
53 * @param threshold The threshold for the logger.
54 */
55 public PrintStreamLogger(PrintStream out, int threshold) {
56 if (out == null) {
57 throw new NullPointerException("missing output stream");
58 }
59 this.out = out;
60 setThreshold(threshold);
61 }
62
63 /**
64 * Writes the specified message and exception to the print stream.
65 *
66 * @param level The priority level of the message.
67 * @param message The message to log, may be <code>null</code>.
68 * @param error The exception to log, may be <code>null</code>.
69 */
70 private void log(int level, String message, Throwable error) {
71 if (level > threshold) {
72 // don't log when it doesn't match your threshold.
73 return;
74 }
75
76 if (message == null && error == null) {
77 // don't log when there's nothing to log.
78 return;
79 }
80
81 StringBuilder buffer = new StringBuilder();
82
83 switch (level) {
84 case (DEBUG):
85 buffer.append("[DEBUG]");
86 break;
87
88 case (INFO):
89 buffer.append("[INFO]");
90 break;
91
92 case (WARN):
93 buffer.append("[WARN]");
94 break;
95
96 case (ERROR):
97 buffer.append("[ERROR]");
98 break;
99
100 case (FATAL):
101 buffer.append("[FATAL]");
102 break;
103
104 default:
105 }
106
107 buffer.append(' ');
108
109 if (message != null) {
110 buffer.append(message);
111 }
112
113 if (error != null) {
114 StringWriter writer = new StringWriter();
115 PrintWriter pWriter = new PrintWriter(writer);
116
117 error.printStackTrace(pWriter);
118
119 if (message != null) {
120 buffer.append('\n');
121 }
122
123 buffer.append("Error:\n");
124 buffer.append(writer.toString());
125 }
126
127 out.println(buffer.toString());
128 }
129
130 /** {@inheritDoc} */
131 public void debug(String message) {
132 log(DEBUG, message, null);
133 }
134
135 /** {@inheritDoc} */
136 public void debug(String message, Throwable throwable) {
137 log(DEBUG, message, throwable);
138 }
139
140 /** {@inheritDoc} */
141 public void info(String message) {
142 log(INFO, message, null);
143 }
144
145 /** {@inheritDoc} */
146 public void info(String message, Throwable throwable) {
147 log(INFO, message, throwable);
148 }
149
150 /** {@inheritDoc} */
151 public void warn(String message) {
152 log(WARN, message, null);
153 }
154
155 /** {@inheritDoc} */
156 public void warn(String message, Throwable throwable) {
157 log(WARN, message, throwable);
158 }
159
160 /** {@inheritDoc} */
161 public void error(String message) {
162 log(ERROR, message, null);
163 }
164
165 /** {@inheritDoc} */
166 public void error(String message, Throwable throwable) {
167 log(ERROR, message, throwable);
168 }
169
170 /** {@inheritDoc} */
171 public void fatalError(String message) {
172 log(FATAL, message, null);
173 }
174
175 /** {@inheritDoc} */
176 public void fatalError(String message, Throwable throwable) {
177 log(FATAL, message, throwable);
178 }
179
180 /**
181 * <p>isDebugEnabled.</p>
182 *
183 * @return a boolean.
184 */
185 public boolean isDebugEnabled() {
186 return threshold >= DEBUG;
187 }
188
189 /**
190 * <p>isErrorEnabled.</p>
191 *
192 * @return a boolean.
193 */
194 public boolean isErrorEnabled() {
195 return threshold >= ERROR;
196 }
197
198 /**
199 * <p>isFatalErrorEnabled.</p>
200 *
201 * @return a boolean.
202 */
203 public boolean isFatalErrorEnabled() {
204 return threshold >= FATAL;
205 }
206
207 /**
208 * <p>isInfoEnabled.</p>
209 *
210 * @return a boolean.
211 */
212 public boolean isInfoEnabled() {
213 return threshold >= INFO;
214 }
215
216 /**
217 * <p>isWarnEnabled.</p>
218 *
219 * @return a boolean.
220 */
221 public boolean isWarnEnabled() {
222 return threshold >= WARN;
223 }
224
225 /**
226 * <p>Getter for the field <code>threshold</code>.</p>
227 *
228 * @return a int.
229 */
230 public int getThreshold() {
231 return threshold;
232 }
233
234 /** {@inheritDoc} */
235 public void setThreshold(int threshold) {
236 this.threshold = threshold;
237 }
238 }