001/*
002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de)
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package de.mnl.osgi.lf4osgi.core;
018
019import org.osgi.framework.Bundle;
020import org.osgi.service.log.LogLevel;
021import org.osgi.service.log.Logger;
022import org.osgi.service.log.LoggerConsumer;
023
024/**
025 * A logger that logs to a buffer.
026 */
027@SuppressWarnings("PMD.TooManyMethods")
028public class BufferingLogger implements Logger {
029
030    private final BufferingLoggerFactory factory;
031    private final Bundle bundle;
032    private final String name;
033
034    /**
035     * Instantiates a new buffering logger with the given factory, bundle
036     * and provided factory.
037     *
038     * @param factory the factory
039     * @param bundle the bundle
040     * @param name the name
041     */
042    public BufferingLogger(BufferingLoggerFactory factory, Bundle bundle,
043            String name) {
044        this.factory = factory;
045        this.bundle = bundle;
046        this.name = name;
047    }
048
049    /**
050     * @return the name
051     */
052    public final String getName() {
053        return name;
054    }
055
056    @Override
057    public boolean isTraceEnabled() {
058        return factory.threshold().implies(LogLevel.TRACE);
059    }
060
061    @Override
062    public void trace(String message) {
063        if (isTraceEnabled()) {
064            factory.addEvent(new BufferedEvent(bundle, getName(),
065                LogLevel.TRACE, message));
066        }
067    }
068
069    @Override
070    public void trace(String format, Object arg) {
071        if (isTraceEnabled()) {
072            factory.addEvent(new BufferedEvent(bundle, getName(),
073                LogLevel.TRACE, format, arg));
074        }
075    }
076
077    @Override
078    public void trace(String format, Object arg1, Object arg2) {
079        if (isTraceEnabled()) {
080            factory.addEvent(new BufferedEvent(bundle, getName(),
081                LogLevel.TRACE, format, arg1, arg2));
082        }
083    }
084
085    @Override
086    public void trace(String format, Object... arguments) {
087        if (isTraceEnabled()) {
088            factory.addEvent(new BufferedEvent(bundle, getName(),
089                LogLevel.TRACE, format, arguments));
090        }
091    }
092
093    @Override
094    public <E extends Exception> void trace(LoggerConsumer<E> consumer)
095            throws E {
096        consumer.accept(this);
097    }
098
099    @Override
100    public boolean isDebugEnabled() {
101        return factory.threshold().implies(LogLevel.DEBUG);
102    }
103
104    @Override
105    public void debug(String message) {
106        if (isDebugEnabled()) {
107            factory.addEvent(new BufferedEvent(bundle, getName(),
108                LogLevel.DEBUG, message));
109        }
110    }
111
112    @Override
113    public void debug(String format, Object arg) {
114        if (isDebugEnabled()) {
115            factory.addEvent(new BufferedEvent(bundle, getName(),
116                LogLevel.DEBUG, format, arg));
117        }
118    }
119
120    @Override
121    public void debug(String format, Object arg1, Object arg2) {
122        if (isDebugEnabled()) {
123            factory.addEvent(new BufferedEvent(bundle, getName(),
124                LogLevel.DEBUG, format, arg1, arg2));
125        }
126    }
127
128    @Override
129    public void debug(String format, Object... arguments) {
130        if (isDebugEnabled()) {
131            factory.addEvent(new BufferedEvent(bundle, getName(),
132                LogLevel.DEBUG, format, arguments));
133        }
134    }
135
136    @Override
137    public <E extends Exception> void debug(LoggerConsumer<E> consumer)
138            throws E {
139        consumer.accept(this);
140    }
141
142    @Override
143    public boolean isInfoEnabled() {
144        return factory.threshold().implies(LogLevel.INFO);
145    }
146
147    @Override
148    public void info(String message) {
149        if (isInfoEnabled()) {
150            factory.addEvent(new BufferedEvent(bundle, getName(),
151                LogLevel.INFO, message));
152        }
153    }
154
155    @Override
156    public void info(String format, Object arg) {
157        if (isInfoEnabled()) {
158            factory.addEvent(new BufferedEvent(bundle, getName(),
159                LogLevel.INFO, format, arg));
160        }
161    }
162
163    @Override
164    public void info(String format, Object arg1, Object arg2) {
165        if (isInfoEnabled()) {
166            factory.addEvent(new BufferedEvent(bundle, getName(),
167                LogLevel.INFO, format, arg1, arg2));
168        }
169    }
170
171    @Override
172    public void info(String format, Object... arguments) {
173        if (isInfoEnabled()) {
174            factory.addEvent(new BufferedEvent(bundle, getName(),
175                LogLevel.INFO, format, arguments));
176        }
177    }
178
179    @Override
180    public <E extends Exception> void info(LoggerConsumer<E> consumer)
181            throws E {
182        consumer.accept(this);
183    }
184
185    @Override
186    public boolean isWarnEnabled() {
187        return factory.threshold().implies(LogLevel.WARN);
188    }
189
190    @Override
191    public void warn(String message) {
192        if (isWarnEnabled()) {
193            factory.addEvent(new BufferedEvent(bundle, getName(),
194                LogLevel.WARN, message));
195        }
196    }
197
198    @Override
199    public void warn(String format, Object arg) {
200        if (isWarnEnabled()) {
201            factory.addEvent(new BufferedEvent(bundle, getName(),
202                LogLevel.WARN, format, arg));
203        }
204    }
205
206    @Override
207    public void warn(String format, Object arg1, Object arg2) {
208        if (isWarnEnabled()) {
209            factory.addEvent(new BufferedEvent(bundle, getName(),
210                LogLevel.WARN, format, arg1, arg2));
211        }
212    }
213
214    @Override
215    public void warn(String format, Object... arguments) {
216        if (isWarnEnabled()) {
217            factory.addEvent(new BufferedEvent(bundle, getName(),
218                LogLevel.WARN, format, arguments));
219        }
220    }
221
222    @Override
223    public <E extends Exception> void warn(LoggerConsumer<E> consumer)
224            throws E {
225        consumer.accept(this);
226    }
227
228    @Override
229    public boolean isErrorEnabled() {
230        return factory.threshold().implies(LogLevel.ERROR);
231    }
232
233    @Override
234    public void error(String message) {
235        if (isErrorEnabled()) {
236            factory.addEvent(new BufferedEvent(bundle, getName(),
237                LogLevel.ERROR, message));
238        }
239    }
240
241    @Override
242    public void error(String format, Object arg) {
243        if (isErrorEnabled()) {
244            factory.addEvent(new BufferedEvent(bundle, getName(),
245                LogLevel.ERROR, format, arg));
246        }
247    }
248
249    @Override
250    public void error(String format, Object arg1, Object arg2) {
251        if (isErrorEnabled()) {
252            factory.addEvent(new BufferedEvent(bundle, getName(),
253                LogLevel.ERROR, format, arg1, arg2));
254        }
255    }
256
257    @Override
258    public void error(String format, Object... arguments) {
259        if (isErrorEnabled()) {
260            factory.addEvent(new BufferedEvent(bundle, getName(),
261                LogLevel.ERROR, format, arguments));
262        }
263    }
264
265    @Override
266    public <E extends Exception> void error(LoggerConsumer<E> consumer)
267            throws E {
268        consumer.accept(this);
269    }
270
271    @Override
272    public void audit(String message) {
273        factory.addEvent(new BufferedEvent(bundle, getName(),
274            LogLevel.AUDIT, message));
275    }
276
277    @Override
278    public void audit(String format, Object arg) {
279        factory.addEvent(new BufferedEvent(bundle, getName(),
280            LogLevel.AUDIT, format, arg));
281    }
282
283    @Override
284    public void audit(String format, Object arg1, Object arg2) {
285        factory.addEvent(new BufferedEvent(bundle, getName(),
286            LogLevel.AUDIT, format, arg1, arg2));
287    }
288
289    @Override
290    public void audit(String format, Object... arguments) {
291        factory.addEvent(new BufferedEvent(bundle, getName(),
292            LogLevel.AUDIT, format, arguments));
293    }
294
295}