View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.repack;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static java.lang.String.format;
19  
20  import java.util.concurrent.locks.Lock;
21  import java.util.concurrent.locks.ReentrantLock;
22  
23  import org.apache.maven.shared.utils.logging.MessageBuilder;
24  import org.apache.maven.shared.utils.logging.MessageUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  final class PluginLog {
29  
30      private final Logger logger;
31      private final Lock instanceLock = new ReentrantLock();
32  
33      PluginLog(final Class<?> clazz) {
34          checkNotNull(clazz, "clazz is null");
35          this.logger = LoggerFactory.getLogger(clazz);
36      }
37  
38      public void debug(final String fmt, final Object... args) {
39          checkNotNull(fmt, "fmt is null");
40          try {
41              instanceLock.lock();
42              logger.debug(format(fmt, args));
43          } finally {
44              instanceLock.unlock();
45          }
46      }
47  
48      public void debug(final Throwable t, final String fmt, final Object... args) {
49          checkNotNull(fmt, "fmt is null");
50          checkNotNull(t, "t is null");
51          try {
52              instanceLock.lock();
53              logger.debug(format(fmt, args), t);
54          } finally {
55              instanceLock.unlock();
56          }
57      }
58  
59      public void info(final String fmt, final Object... args) {
60          checkNotNull(fmt, "fmt is null");
61          try {
62              instanceLock.lock();
63              logger.info(format(fmt, args));
64          } finally {
65              instanceLock.unlock();
66          }
67      }
68  
69      public void info(final Throwable t, final String fmt, final Object... args) {
70          checkNotNull(fmt, "fmt is null");
71          checkNotNull(t, "t is null");
72          try {
73              instanceLock.lock();
74              logger.info(format(fmt, args), t);
75          } finally {
76              instanceLock.unlock();
77          }
78      }
79  
80      public void warn(final String fmt, final Object... args) {
81          checkNotNull(fmt, "fmt is null");
82          MessageBuilder mb = MessageUtils.buffer();
83          try {
84              instanceLock.lock();
85              logger.warn(mb.warning(format(fmt, args)).build());
86          } finally {
87              instanceLock.unlock();
88          }
89      }
90  
91      public void warn(final Throwable t, final String fmt, final Object... args) {
92          checkNotNull(fmt, "fmt is null");
93          checkNotNull(t, "t is null");
94          MessageBuilder mb = MessageUtils.buffer();
95          try {
96              instanceLock.lock();
97              logger.warn(mb.warning(format(fmt, args)).build(), t);
98          } finally {
99              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)).build());
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)).build(), 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 }