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 java.util.concurrent.locks.Lock;
021import java.util.concurrent.locks.ReentrantLock;
022
023import org.apache.maven.shared.utils.logging.MessageBuilder;
024import org.apache.maven.shared.utils.logging.MessageUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028public final class PluginLog {
029
030    private final Logger logger;
031    private final Lock instanceLock = new ReentrantLock();
032
033    public PluginLog(final Class<?> clazz) {
034        checkNotNull(clazz, "clazz is null");
035        this.logger = LoggerFactory.getLogger(clazz);
036    }
037
038    public void debug(final String fmt, final Object... args) {
039        checkNotNull(fmt, "fmt is null");
040        try {
041            instanceLock.lock();
042            logger.debug(format(fmt, args));
043        } finally {
044            instanceLock.unlock();
045        }
046    }
047
048    public void debug(final Throwable t, final String fmt, final Object... args) {
049        checkNotNull(fmt, "fmt is null");
050        checkNotNull(t, "t is null");
051        try {
052            instanceLock.lock();
053            logger.debug(format(fmt, args), t);
054        } finally {
055            instanceLock.unlock();
056        }
057    }
058
059    public void info(final String fmt, final Object... args) {
060        checkNotNull(fmt, "fmt is null");
061        try {
062            instanceLock.lock();
063            logger.info(format(fmt, args));
064        } finally {
065            instanceLock.unlock();
066        }
067    }
068
069    public void info(final Throwable t, final String fmt, final Object... args) {
070        checkNotNull(fmt, "fmt is null");
071        checkNotNull(t, "t is null");
072        try {
073            instanceLock.lock();
074            logger.info(format(fmt, args), t);
075        } finally {
076            instanceLock.unlock();
077        }
078    }
079
080    public void warn(final String fmt, final Object... args) {
081        checkNotNull(fmt, "fmt is null");
082        MessageBuilder mb = MessageUtils.buffer();
083        try {
084            instanceLock.lock();
085            logger.warn(mb.warning(format(fmt, args)).toString());
086        } finally {
087            instanceLock.unlock();
088        }
089    }
090
091    public void warn(final Throwable t, final String fmt, final Object... args) {
092        checkNotNull(fmt, "fmt is null");
093        checkNotNull(t, "t is null");
094        MessageBuilder mb = MessageUtils.buffer();
095        try {
096            instanceLock.lock();
097            logger.warn(mb.warning(format(fmt, args)).toString(), t);
098        } finally {
099            instanceLock.unlock();
100        }
101    }
102
103    public void error(final String fmt, final Object... args) {
104        checkNotNull(fmt, "fmt is null");
105        MessageBuilder mb = MessageUtils.buffer();
106        try {
107            instanceLock.lock();
108            logger.error(mb.failure(format(fmt, args)).toString());
109        } finally {
110            instanceLock.unlock();
111        }
112    }
113
114    public void error(final Throwable t, final String fmt, final Object... args) {
115        checkNotNull(fmt, "fmt is null");
116        checkNotNull(t, "t is null");
117        MessageBuilder mb = MessageUtils.buffer();
118        try {
119            instanceLock.lock();
120            logger.error(mb.failure(format(fmt, args)).toString(), t);
121        } finally {
122            instanceLock.unlock();
123        }
124    }
125
126    public void report(final boolean quiet, final String fmt, final Object... args) {
127        if (quiet) {
128            debug(fmt, args);
129        } else {
130            info(fmt, args);
131        }
132    }
133}