001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.dvc;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.lang.String.format;
019
020import org.apache.maven.shared.utils.logging.MessageBuilder;
021import org.apache.maven.shared.utils.logging.MessageUtils;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025public final class PluginLog {
026
027    private final Logger logger;
028
029    public PluginLog(final Class<?> clazz) {
030        checkNotNull(clazz, "clazz is null");
031        this.logger = LoggerFactory.getLogger(clazz);
032    }
033
034    public void debug(final String fmt, final Object... args) {
035        checkNotNull(fmt, "fmt is null");
036        synchronized (logger) {
037            logger.debug(format(fmt, args));
038        }
039    }
040
041    public void debug(final Throwable t, final String fmt, final Object... args) {
042        checkNotNull(fmt, "fmt is null");
043        checkNotNull(t, "t is null");
044        synchronized (logger) {
045            logger.debug(format(fmt, args), t);
046        }
047    }
048
049    public void info(final String fmt, final Object... args) {
050        checkNotNull(fmt, "fmt is null");
051        synchronized (logger) {
052            logger.info(format(fmt, args));
053        }
054    }
055
056    public void info(final Throwable t, final String fmt, final Object... args) {
057        checkNotNull(fmt, "fmt is null");
058        checkNotNull(t, "t is null");
059        synchronized (logger) {
060            logger.info(format(fmt, args), t);
061        }
062    }
063
064    public void warn(final String fmt, final Object... args) {
065        checkNotNull(fmt, "fmt is null");
066        MessageBuilder mb = MessageUtils.buffer();
067        synchronized (logger) {
068            logger.warn(mb.warning(format(fmt, args)).toString());
069        }
070    }
071
072    public void warn(final Throwable t, final String fmt, final Object... args) {
073        checkNotNull(fmt, "fmt is null");
074        checkNotNull(t, "t is null");
075        MessageBuilder mb = MessageUtils.buffer();
076        synchronized (logger) {
077            logger.warn(mb.warning(format(fmt, args)).toString(), t);
078        }
079    }
080
081    public void error(final String fmt, final Object... args) {
082        checkNotNull(fmt, "fmt is null");
083        MessageBuilder mb = MessageUtils.buffer();
084        synchronized (logger) {
085            logger.error(mb.failure(format(fmt, args)).toString());
086        }
087    }
088
089    public void error(final Throwable t, final String fmt, final Object... args) {
090        checkNotNull(fmt, "fmt is null");
091        checkNotNull(t, "t is null");
092        MessageBuilder mb = MessageUtils.buffer();
093        synchronized (logger) {
094            logger.error(mb.failure(format(fmt, args)).toString(), t);
095        }
096    }
097
098    public void report(final boolean quiet, final String fmt, final Object... args) {
099        if (quiet) {
100            debug(fmt, args);
101        } else {
102            info(fmt, args);
103        }
104    }
105}