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 the delegate Ant task.
28   *
29   * @author colezlaw
30   */
31  public class AntLoggerAdapter extends MarkerIgnoringBase {
32  
33      /**
34       * A reference to the Ant task used for logging.
35       */
36      private Task task;
37  
38      /**
39       * Constructs an Ant Logger Adapter.
40       *
41       * @param task the Ant Task to use for logging
42       */
43      public AntLoggerAdapter(Task task) {
44          super();
45          this.task = task;
46      }
47  
48      /**
49       * Sets the current Ant task to use for logging.
50       *
51       * @param task the Ant task to use for logging
52       */
53      public void setTask(Task task) {
54          this.task = task;
55      }
56  
57      @Override
58      public boolean isTraceEnabled() {
59          // Might be a more efficient way to do this, but Ant doesn't enable or disable
60          // various levels globally - it just fires things at registered Listeners.
61          return true;
62      }
63  
64      @Override
65      public void trace(String msg) {
66          if (task != null) {
67              task.log(msg, Project.MSG_VERBOSE);
68          }
69      }
70  
71      @Override
72      public void trace(String format, Object arg) {
73          if (task != null) {
74              final FormattingTuple tp = MessageFormatter.format(format, arg);
75              task.log(tp.getMessage(), Project.MSG_VERBOSE);
76          }
77      }
78  
79      @Override
80      public void trace(String format, Object arg1, Object arg2) {
81          if (task != null) {
82              final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
83              task.log(tp.getMessage(), Project.MSG_VERBOSE);
84          }
85      }
86  
87      @Override
88      public void trace(String format, Object... arguments) {
89          if (task != null) {
90              final FormattingTuple tp = MessageFormatter.format(format, arguments);
91              task.log(tp.getMessage(), Project.MSG_VERBOSE);
92          }
93      }
94  
95      @Override
96      public void trace(String msg, Throwable t) {
97          if (task != null) {
98              task.log(msg, t, Project.MSG_VERBOSE);
99          }
100     }
101 
102     @Override
103     public boolean isDebugEnabled() {
104         return true;
105     }
106 
107     @Override
108     public void debug(String msg) {
109         if (task != null) {
110             task.log(msg, Project.MSG_DEBUG);
111         }
112     }
113 
114     @Override
115     public void debug(String format, Object arg) {
116         if (task != null) {
117             final FormattingTuple tp = MessageFormatter.format(format, arg);
118             task.log(tp.getMessage(), Project.MSG_DEBUG);
119         }
120     }
121 
122     @Override
123     public void debug(String format, Object arg1, Object arg2) {
124         if (task != null) {
125             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
126             task.log(tp.getMessage(), Project.MSG_DEBUG);
127         }
128     }
129 
130     @Override
131     public void debug(String format, Object... arguments) {
132         if (task != null) {
133             final FormattingTuple tp = MessageFormatter.format(format, arguments);
134             task.log(tp.getMessage(), Project.MSG_DEBUG);
135         }
136     }
137 
138     @Override
139     public void debug(String msg, Throwable t) {
140         if (task != null) {
141             task.log(msg, t, Project.MSG_DEBUG);
142         }
143     }
144 
145     @Override
146     public boolean isInfoEnabled() {
147         return true;
148     }
149 
150     @Override
151     public void info(String msg) {
152         if (task != null) {
153             task.log(msg, Project.MSG_INFO);
154         }
155     }
156 
157     @Override
158     public void info(String format, Object arg) {
159         if (task != null) {
160             final FormattingTuple tp = MessageFormatter.format(format, arg);
161             task.log(tp.getMessage(), Project.MSG_INFO);
162         }
163     }
164 
165     @Override
166     public void info(String format, Object arg1, Object arg2) {
167         if (task != null) {
168             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
169             task.log(tp.getMessage(), Project.MSG_INFO);
170         }
171     }
172 
173     @Override
174     public void info(String format, Object... arguments) {
175         if (task != null) {
176             final FormattingTuple tp = MessageFormatter.format(format, arguments);
177             task.log(tp.getMessage(), Project.MSG_INFO);
178         }
179     }
180 
181     @Override
182     public void info(String msg, Throwable t) {
183         if (task != null) {
184             task.log(msg, t, Project.MSG_INFO);
185         }
186     }
187 
188     @Override
189     public boolean isWarnEnabled() {
190         return true;
191     }
192 
193     @Override
194     public void warn(String msg) {
195         if (task != null) {
196             task.log(msg, Project.MSG_WARN);
197         }
198     }
199 
200     @Override
201     public void warn(String format, Object arg) {
202         if (task != null) {
203             final FormattingTuple tp = MessageFormatter.format(format, arg);
204             task.log(tp.getMessage(), Project.MSG_WARN);
205         }
206     }
207 
208     @Override
209     public void warn(String format, Object... arguments) {
210         if (task != null) {
211             final FormattingTuple tp = MessageFormatter.format(format, arguments);
212             task.log(tp.getMessage(), Project.MSG_WARN);
213         }
214     }
215 
216     @Override
217     public void warn(String format, Object arg1, Object arg2) {
218         if (task != null) {
219             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
220             task.log(tp.getMessage(), Project.MSG_WARN);
221         }
222     }
223 
224     @Override
225     public void warn(String msg, Throwable t) {
226         if (task != null) {
227             task.log(msg, t, Project.MSG_WARN);
228         }
229     }
230 
231     @Override
232     public boolean isErrorEnabled() {
233         return true;
234     }
235 
236     @Override
237     public void error(String msg) {
238         if (task != null) {
239             task.log(msg, Project.MSG_ERR);
240         }
241     }
242 
243     @Override
244     public void error(String format, Object arg) {
245         if (task != null) {
246             final FormattingTuple tp = MessageFormatter.format(format, arg);
247             task.log(tp.getMessage(), Project.MSG_ERR);
248         }
249     }
250 
251     @Override
252     public void error(String format, Object arg1, Object arg2) {
253         if (task != null) {
254             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
255             task.log(tp.getMessage(), Project.MSG_ERR);
256         }
257     }
258 
259     @Override
260     public void error(String format, Object... arguments) {
261         if (task != null) {
262             final FormattingTuple tp = MessageFormatter.format(format, arguments);
263             task.log(tp.getMessage(), Project.MSG_ERR);
264         }
265     }
266 
267     @Override
268     public void error(String msg, Throwable t) {
269         if (task != null) {
270             task.log(msg, t, Project.MSG_ERR);
271         }
272     }
273 }