source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/PrintWriterListener.java @ 1899

Last change on this file since 1899 was 1899, checked in by sherbold, 9 years ago
  • Property svn:mime-type set to text/plain
File size: 3.6 KB
Line 
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
15package de.ugoe.cs.util.console;
16
17import java.io.PrintWriter;
18import java.text.SimpleDateFormat;
19import java.util.Date;
20import java.util.logging.Level;
21
22import de.ugoe.cs.util.console.listener.IErrorListener;
23import de.ugoe.cs.util.console.listener.IExceptionListener;
24import de.ugoe.cs.util.console.listener.IOutputListener;
25import de.ugoe.cs.util.console.listener.ITraceListener;
26
27/**
28 * <p>
29 * A listener that gets a PrintWriter and outputs all information to that PrintWriter.
30 * </p>
31 *
32 * @author Steffen Herbold
33 */
34public class PrintWriterListener implements IOutputListener, IErrorListener, ITraceListener,
35    IExceptionListener
36{
37
38    /**
39     * <p>
40     * Defines the trace level used by this console.
41     * </p>
42     */
43    private Level traceLevel;
44
45    private final SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
46
47    private final PrintWriter out;
48
49    /**
50     * <p>
51     * Creates a new PrintWriterListener. The trace level is {@link Level#WARNING}.
52     * </p>
53     */
54    public PrintWriterListener(PrintWriter out) {
55        this(out, Level.WARNING);
56    }
57
58    /**
59     * <p>
60     * Creates a new PrintWriterListener.
61     * </p>
62     *
63     * @param traceLevel
64     *            trace level used by this text console
65     */
66    public PrintWriterListener(PrintWriter out, Level traceLevel) {
67        this.out = out;
68        this.traceLevel = traceLevel;
69    }
70
71    /**
72     * <p>
73     * Prints messages to out.
74     * </p>
75     *
76     * @see ConsoleObserver#outputMsg(java.lang.String)
77     */
78    public void outputMsg(String newMessage) {
79        out.print(newMessage);
80    }
81
82    /**
83     * <p>
84     * Prints messages to out.
85     * </p>
86     *
87     * @see ConsoleObserver#errorMsg(String)
88     */
89    @Override
90    public void errorMsg(String errMessage) {
91        out.print(errMessage);
92    }
93
94    /**
95     * <p>
96     * Prints the stacktrace of an exception to out if the log level is more or equally detailed to
97     * <code>Level.FINE</code>. Otherwise, it just prints a line naming the exception or only the
98     * message, if any.
99     * </p>
100     *
101     * @see ConsoleObserver#logException(Exception)
102     */
103    @Override
104    public void logException(Exception e) {
105        if (traceLevel.intValue() > Level.FINE.intValue()) {
106            if (e.getMessage() != null) {
107                out.println(e.getMessage());
108            }
109            else {
110                out.println(e);
111            }
112        }
113        else {
114            e.printStackTrace(out);
115        }
116    }
117
118    /**
119     * <p>
120     * Prints messages to out. These message are only printed if the trace level is higher than the
121     * defined minimum.
122     * </p>
123     */
124    @Override
125    public void traceMsg(String traceMessage, Level level) {
126        if (level.intValue() >= traceLevel.intValue()) {
127            out.print("[" + level.toString() + "] [" + ft.format(new Date()) + "] " + traceMessage);
128        }
129    }
130}
Note: See TracBrowser for help on using the repository browser.