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          task.log(msg, Project.MSG_VERBOSE);
67      }
68  
69      @Override
70      public void trace(String format, Object arg) {
71          if (task != null) {
72              final FormattingTuple tp = MessageFormatter.format(format, arg);
73              task.log(tp.getMessage(), Project.MSG_VERBOSE);
74          }
75      }
76  
77      @Override
78      public void trace(String format, Object arg1, Object arg2) {
79          if (task != null) {
80              final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
81              task.log(tp.getMessage(), Project.MSG_VERBOSE);
82          }
83      }
84  
85      @Override
86      public void trace(String format, Object... arguments) {
87          if (task != null) {
88              final FormattingTuple tp = MessageFormatter.format(format, arguments);
89              task.log(tp.getMessage(), Project.MSG_VERBOSE);
90          }
91      }
92  
93      @Override
94      public void trace(String msg, Throwable t) {
95          if (task != null) {
96              task.log(msg, t, Project.MSG_VERBOSE);
97          }
98      }
99  
100     @Override
101     public boolean isDebugEnabled() {
102         return true;
103     }
104 
105     @Override
106     public void debug(String msg) {
107         if (task != null) {
108             task.log(msg, Project.MSG_DEBUG);
109         }
110     }
111 
112     @Override
113     public void debug(String format, Object arg) {
114         if (task != null) {
115             final FormattingTuple tp = MessageFormatter.format(format, arg);
116             task.log(tp.getMessage(), Project.MSG_DEBUG);
117         }
118     }
119 
120     @Override
121     public void debug(String format, Object arg1, Object arg2) {
122         if (task != null) {
123             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
124             task.log(tp.getMessage(), Project.MSG_DEBUG);
125         }
126     }
127 
128     @Override
129     public void debug(String format, Object... arguments) {
130         if (task != null) {
131             final FormattingTuple tp = MessageFormatter.format(format, arguments);
132             task.log(tp.getMessage(), Project.MSG_DEBUG);
133         }
134     }
135 
136     @Override
137     public void debug(String msg, Throwable t) {
138         if (task != null) {
139             task.log(msg, t, Project.MSG_DEBUG);
140         }
141     }
142 
143     @Override
144     public boolean isInfoEnabled() {
145         return true;
146     }
147 
148     @Override
149     public void info(String msg) {
150         if (task != null) {
151             task.log(msg, Project.MSG_INFO);
152         }
153     }
154 
155     @Override
156     public void info(String format, Object arg) {
157         if (task != null) {
158             final FormattingTuple tp = MessageFormatter.format(format, arg);
159             task.log(tp.getMessage(), Project.MSG_INFO);
160         }
161     }
162 
163     @Override
164     public void info(String format, Object arg1, Object arg2) {
165         if (task != null) {
166             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
167             task.log(tp.getMessage(), Project.MSG_INFO);
168         }
169     }
170 
171     @Override
172     public void info(String format, Object... arguments) {
173         if (task != null) {
174             final FormattingTuple tp = MessageFormatter.format(format, arguments);
175             task.log(tp.getMessage(), Project.MSG_INFO);
176         }
177     }
178 
179     @Override
180     public void info(String msg, Throwable t) {
181         if (task != null) {
182             task.log(msg, t, Project.MSG_INFO);
183         }
184     }
185 
186     @Override
187     public boolean isWarnEnabled() {
188         return true;
189     }
190 
191     @Override
192     public void warn(String msg) {
193         if (task != null) {
194             task.log(msg, Project.MSG_WARN);
195         }
196     }
197 
198     @Override
199     public void warn(String format, Object arg) {
200         if (task != null) {
201             final FormattingTuple tp = MessageFormatter.format(format, arg);
202             task.log(tp.getMessage(), Project.MSG_WARN);
203         }
204     }
205 
206     @Override
207     public void warn(String format, Object... arguments) {
208         if (task != null) {
209             final FormattingTuple tp = MessageFormatter.format(format, arguments);
210             task.log(tp.getMessage(), Project.MSG_WARN);
211         }
212     }
213 
214     @Override
215     public void warn(String format, Object arg1, Object arg2) {
216         if (task != null) {
217             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
218             task.log(tp.getMessage(), Project.MSG_WARN);
219         }
220     }
221 
222     @Override
223     public void warn(String msg, Throwable t) {
224         if (task != null) {
225             task.log(msg, t, Project.MSG_WARN);
226         }
227     }
228 
229     @Override
230     public boolean isErrorEnabled() {
231         return true;
232     }
233 
234     @Override
235     public void error(String msg) {
236         if (task != null) {
237             task.log(msg, Project.MSG_ERR);
238         }
239     }
240 
241     @Override
242     public void error(String format, Object arg) {
243         if (task != null) {
244             final FormattingTuple tp = MessageFormatter.format(format, arg);
245             task.log(tp.getMessage(), Project.MSG_ERR);
246         }
247     }
248 
249     @Override
250     public void error(String format, Object arg1, Object arg2) {
251         if (task != null) {
252             final FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
253             task.log(tp.getMessage(), Project.MSG_ERR);
254         }
255     }
256 
257     @Override
258     public void error(String format, Object... arguments) {
259         if (task != null) {
260             final FormattingTuple tp = MessageFormatter.format(format, arguments);
261             task.log(tp.getMessage(), Project.MSG_ERR);
262         }
263     }
264 
265     @Override
266     public void error(String msg, Throwable t) {
267         if (task != null) {
268             task.log(msg, t, Project.MSG_ERR);
269         }
270     }
271 }