View Javadoc
1   /*
2    * This file is part of dependency-check-ant.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2015 The OWASP Foundation. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.ant.logging;
19  
20  import org.apache.tools.ant.Project;
21  import org.apache.tools.ant.Task;
22  import org.slf4j.helpers.FormattingTuple;
23  import org.slf4j.helpers.MarkerIgnoringBase;
24  import org.slf4j.helpers.MessageFormatter;
25  
26  /**
27   * An instance of {@link org.slf4j.Logger} which simply calls the log method on
28   * the delegate Ant task.
29   *
30   * @author colezlaw
31   */
32  public class AntLoggerAdapter extends MarkerIgnoringBase {
33  
34      /**
35       * The serial version UID for serialization.
36       */
37      private static final long serialVersionUID = -8546294566287970709L;
38      /**
39       * A reference to the Ant task used for logging.
40       */
41      private transient Task task;
42  
43      /**
44       * Constructs an Ant Logger Adapter.
45       *
46       * @param task the Ant Task to use for logging
47       */
48      public AntLoggerAdapter(Task task) {
49          super();
50          this.task = task;
51      }
52  
53      /**
54       * Sets the current Ant task to use for logging.
55       *
56       * @param task the Ant task to use for logging
57       */
58      public void setTask(Task task) {
59          this.task = task;
60      }
61  
62      @Override
63      public boolean isTraceEnabled() {
64          // Might be a more efficient way to do this, but Ant doesn't enable or disable
65          // various levels globally - it just fires things at registered Listeners.
66          return true;
67      }
68  
69      @Override
70      public void trace(String msg) {
71          if (task != null) {
72              task.log(msg, Project.MSG_VERBOSE);
73          }
74      }
75  
76      @Override
77      public void trace(String format, Object arg) {
78          if (task != null) {
79              final FormattingTuple tp = MessageFormatter.format(format, arg);
80              task.log(tp.getMessage(), Project.MSG_VERBOSE);
81          }
82      }
83  
84      @Override
85      public void trace(String format, Object arg1, Object arg2) {
86          if (task != null) {
87              final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
88              task.log(tp.getMessage(), Project.MSG_VERBOSE);
89          }
90      }
91  
92      @Override
93      public void trace(String format, Object... arguments) {
94          if (task != null) {
95              final FormattingTuple tp = MessageFormatter.format(format, arguments);
96              task.log(tp.getMessage(), Project.MSG_VERBOSE);
97          }
98      }
99  
100     @Override
101     public void trace(String msg, Throwable t) {
102         if (task != null) {
103             task.log(msg, t, Project.MSG_VERBOSE);
104         }
105     }
106 
107     @Override
108     public boolean isDebugEnabled() {
109         return true;
110     }
111 
112     @Override
113     public void debug(String msg) {
114         if (task != null) {
115             task.log(msg, Project.MSG_DEBUG);
116         }
117     }
118 
119     @Override
120     public void debug(String format, Object arg) {
121         if (task != null) {
122             final FormattingTuple tp = MessageFormatter.format(format, arg);
123             task.log(tp.getMessage(), Project.MSG_DEBUG);
124         }
125     }
126 
127     @Override
128     public void debug(String format, Object arg1, Object arg2) {
129         if (task != null) {
130             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
131             task.log(tp.getMessage(), Project.MSG_DEBUG);
132         }
133     }
134 
135     @Override
136     public void debug(String format, Object... arguments) {
137         if (task != null) {
138             final FormattingTuple tp = MessageFormatter.format(format, arguments);
139             task.log(tp.getMessage(), Project.MSG_DEBUG);
140         }
141     }
142 
143     @Override
144     public void debug(String msg, Throwable t) {
145         if (task != null) {
146             task.log(msg, t, Project.MSG_DEBUG);
147         }
148     }
149 
150     @Override
151     public boolean isInfoEnabled() {
152         return true;
153     }
154 
155     @Override
156     public void info(String msg) {
157         if (task != null) {
158             task.log(msg, Project.MSG_INFO);
159         }
160     }
161 
162     @Override
163     public void info(String format, Object arg) {
164         if (task != null) {
165             final FormattingTuple tp = MessageFormatter.format(format, arg);
166             task.log(tp.getMessage(), Project.MSG_INFO);
167         }
168     }
169 
170     @Override
171     public void info(String format, Object arg1, Object arg2) {
172         if (task != null) {
173             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
174             task.log(tp.getMessage(), Project.MSG_INFO);
175         }
176     }
177 
178     @Override
179     public void info(String format, Object... arguments) {
180         if (task != null) {
181             final FormattingTuple tp = MessageFormatter.format(format, arguments);
182             task.log(tp.getMessage(), Project.MSG_INFO);
183         }
184     }
185 
186     @Override
187     public void info(String msg, Throwable t) {
188         if (task != null) {
189             task.log(msg, t, Project.MSG_INFO);
190         }
191     }
192 
193     @Override
194     public boolean isWarnEnabled() {
195         return true;
196     }
197 
198     @Override
199     public void warn(String msg) {
200         if (task != null) {
201             task.log(msg, Project.MSG_WARN);
202         }
203     }
204 
205     @Override
206     public void warn(String format, Object arg) {
207         if (task != null) {
208             final FormattingTuple tp = MessageFormatter.format(format, arg);
209             task.log(tp.getMessage(), Project.MSG_WARN);
210         }
211     }
212 
213     @Override
214     public void warn(String format, Object... arguments) {
215         if (task != null) {
216             final FormattingTuple tp = MessageFormatter.format(format, arguments);
217             task.log(tp.getMessage(), Project.MSG_WARN);
218         }
219     }
220 
221     @Override
222     public void warn(String format, Object arg1, Object arg2) {
223         if (task != null) {
224             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
225             task.log(tp.getMessage(), Project.MSG_WARN);
226         }
227     }
228 
229     @Override
230     public void warn(String msg, Throwable t) {
231         if (task != null) {
232             task.log(msg, t, Project.MSG_WARN);
233         }
234     }
235 
236     @Override
237     public boolean isErrorEnabled() {
238         return true;
239     }
240 
241     @Override
242     public void error(String msg) {
243         if (task != null) {
244             task.log(msg, Project.MSG_ERR);
245         }
246     }
247 
248     @Override
249     public void error(String format, Object arg) {
250         if (task != null) {
251             final FormattingTuple tp = MessageFormatter.format(format, arg);
252             task.log(tp.getMessage(), Project.MSG_ERR);
253         }
254     }
255 
256     @Override
257     public void error(String format, Object arg1, Object arg2) {
258         if (task != null) {
259             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
260             task.log(tp.getMessage(), Project.MSG_ERR);
261         }
262     }
263 
264     @Override
265     public void error(String format, Object... arguments) {
266         if (task != null) {
267             final FormattingTuple tp = MessageFormatter.format(format, arguments);
268             task.log(tp.getMessage(), Project.MSG_ERR);
269         }
270     }
271 
272     @Override
273     public void error(String msg, Throwable t) {
274         if (task != null) {
275             task.log(msg, t, Project.MSG_ERR);
276         }
277     }
278 }