Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LogWriter |
|
| 4.714285714285714;4.714 | ||||
LogWriter$1 |
|
| 4.714285714285714;4.714 | ||||
LogWriter$LogLevel |
|
| 4.714285714285714;4.714 |
1 | /* | |
2 | * Copyright (c) 2008 | |
3 | * Jon Schewe. All rights reserved | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
24 | * SUCH DAMAGE. | |
25 | * | |
26 | * I'd appreciate comments/suggestions on the code jpschewe@mtu.net | |
27 | */ | |
28 | package net.mtu.eggplant.io; | |
29 | ||
30 | import java.io.IOException; | |
31 | import java.io.Writer; | |
32 | ||
33 | import org.slf4j.Logger; | |
34 | ||
35 | /** | |
36 | * Writer that outputs to the current logging facility (SLF4J). | |
37 | */ | |
38 | public class LogWriter extends Writer { | |
39 | ||
40 | private final Logger _logger; | |
41 | ||
42 | private final LogLevel _level; | |
43 | ||
44 | 0 | private final StringBuilder _text = new StringBuilder(); |
45 | ||
46 | /** | |
47 | * Log levels that {@link LogWriter} can output to. | |
48 | */ | |
49 | 0 | public enum LogLevel { |
50 | 0 | TRACE, DEBUG, INFO, WARN, ERROR |
51 | } | |
52 | ||
53 | /** | |
54 | * @param logger the logger to log to | |
55 | * @param level the level to log at | |
56 | * @throws NullPointerException if logger or level is null | |
57 | */ | |
58 | 0 | public LogWriter(final Logger logger, final LogLevel level) { |
59 | 0 | if (null == logger |
60 | || null == level) { | |
61 | 0 | throw new NullPointerException(); |
62 | } | |
63 | ||
64 | 0 | this._logger = logger; |
65 | 0 | this._level = level; |
66 | 0 | } |
67 | ||
68 | /** | |
69 | * @param lock the lock | |
70 | * @param logger the logger to log to | |
71 | * @param level the level to log at | |
72 | * @throws NullPointerException if logger or level is null | |
73 | */ | |
74 | public LogWriter(final Object lock, final Logger logger, final LogLevel level) { | |
75 | 0 | super(lock); |
76 | 0 | if (null == logger |
77 | || null == level) { | |
78 | 0 | throw new NullPointerException(); |
79 | } | |
80 | 0 | this._logger = logger; |
81 | 0 | this._level = level; |
82 | 0 | } |
83 | ||
84 | /** | |
85 | * @see java.io.Writer#close() | |
86 | */ | |
87 | @Override | |
88 | public void close() throws IOException { | |
89 | 0 | flush(); |
90 | 0 | } |
91 | ||
92 | /** | |
93 | * @see java.io.Writer#flush() | |
94 | */ | |
95 | @Override | |
96 | public void flush() throws IOException { | |
97 | 0 | if (isLogLevelEnabled()) { |
98 | 0 | final String s = _text.toString(); |
99 | 0 | if (s.length() > 0) { |
100 | 0 | log(s); |
101 | } | |
102 | } | |
103 | 0 | _text.setLength(0); |
104 | 0 | } |
105 | ||
106 | /** | |
107 | * @see java.io.Writer#write(char[], int, int) | |
108 | */ | |
109 | @Override | |
110 | public void write(final char[] cbuf, final int off, final int len) throws IOException { | |
111 | 0 | if (isLogLevelEnabled()) { |
112 | 0 | _text.append(cbuf, off, len); |
113 | } | |
114 | 0 | } |
115 | ||
116 | /** | |
117 | * Is the logging level that was specified in the constructor enabled? | |
118 | */ | |
119 | private boolean isLogLevelEnabled() { | |
120 | 0 | switch (_level) { |
121 | case TRACE: | |
122 | 0 | return _logger.isTraceEnabled(); |
123 | case DEBUG: | |
124 | 0 | return _logger.isDebugEnabled(); |
125 | case INFO: | |
126 | 0 | return _logger.isInfoEnabled(); |
127 | case WARN: | |
128 | 0 | return _logger.isWarnEnabled(); |
129 | case ERROR: | |
130 | 0 | return _logger.isErrorEnabled(); |
131 | default: | |
132 | 0 | throw new RuntimeException("Unknown log level: " |
133 | + _level); | |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * Do the logging. | |
139 | */ | |
140 | private void log(final String s) { | |
141 | 0 | switch (_level) { |
142 | case TRACE: | |
143 | 0 | _logger.trace(s); |
144 | 0 | break; |
145 | case DEBUG: | |
146 | 0 | _logger.debug(s); |
147 | 0 | break; |
148 | case INFO: | |
149 | 0 | _logger.info(s); |
150 | 0 | break; |
151 | case WARN: | |
152 | 0 | _logger.warn(s); |
153 | 0 | break; |
154 | case ERROR: | |
155 | 0 | _logger.error(s); |
156 | 0 | break; |
157 | default: | |
158 | 0 | throw new RuntimeException("Unknown log level: " |
159 | + _level); | |
160 | } | |
161 | 0 | } |
162 | ||
163 | } |