View Javadoc

1   package org.codehaus.mojo.apt;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright 2006-2008 The Codehaus.
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileNotFoundException;
30  import java.io.FileReader;
31  import java.io.IOException;
32  import java.io.Reader;
33  import java.util.Collection;
34  
35  import org.apache.maven.plugin.logging.Log;
36  import org.codehaus.plexus.util.IOUtil;
37  
38  /**
39   * Provides utilities for working with Mojo logs.
40   * 
41   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
42   * @version $Id: LogUtils.java 7031 2008-05-22 09:41:31Z mark $
43   */
44  public final class LogUtils
45  {
46      // constants --------------------------------------------------------------
47  
48      public static final int LEVEL_DEBUG = 0;
49  
50      public static final int LEVEL_INFO = 1;
51  
52      public static final int LEVEL_WARN = 2;
53  
54      public static final int LEVEL_ERROR = 3;
55  
56      // constructors -----------------------------------------------------------
57  
58      private LogUtils()
59      {
60          throw new AssertionError();
61      }
62  
63      // public methods ---------------------------------------------------------
64  
65      public static void log( Log log, int level, File file )
66      {
67          log( log, level, file, null );
68      }
69  
70      public static void log( Log log, int level, File file, CharSequence prefix )
71      {
72          if ( !isEnabled( log, level ) )
73          {
74              return;
75          }
76  
77          FileReader reader = null;
78  
79          try
80          {
81              reader = new FileReader( file );
82  
83              log( log, level, reader, prefix );
84          }
85          catch ( FileNotFoundException exception )
86          {
87              log.warn( "Error logging file", exception );
88          }
89          finally
90          {
91              IOUtil.close( reader );
92          }
93      }
94  
95      public static void log( Log log, int level, Reader reader )
96      {
97          log( log, level, reader, null );
98      }
99  
100     public static void log( Log log, int level, Reader reader, CharSequence prefix )
101     {
102         if ( !isEnabled( log, level ) )
103         {
104             return;
105         }
106 
107         BufferedReader bufferedReader = new BufferedReader( reader );
108 
109         String line;
110 
111         try
112         {
113             while ( ( line = bufferedReader.readLine() ) != null )
114             {
115                 log( log, level, line, prefix );
116             }
117         }
118         catch ( IOException exception )
119         {
120             log.warn( "Error logging reader", exception );
121         }
122     }
123 
124     public static void log( Log log, int level, Collection<?> messages )
125     {
126         log( log, level, messages, null );
127     }
128 
129     public static void log( Log log, int level, Collection<?> messages, CharSequence prefix )
130     {
131         if ( !isEnabled( log, level ) )
132         {
133             return;
134         }
135 
136         for ( Object message : messages )
137         {
138             log( log, level, message, prefix );
139         }
140     }
141 
142     public static void log( Log log, int level, Object message )
143     {
144         log( log, level, message, null );
145     }
146 
147     public static void log( Log log, int level, Object message, CharSequence prefix )
148     {
149         CharSequence chars;
150 
151         if ( message instanceof CharSequence )
152         {
153             chars = (CharSequence) message;
154         }
155         else
156         {
157             chars = String.valueOf( message );
158         }
159 
160         log( log, level, chars, prefix );
161     }
162 
163     public static void log( Log log, int level, CharSequence message )
164     {
165         if ( level == LEVEL_DEBUG )
166         {
167             log.debug( message );
168         }
169         else if ( level == LEVEL_INFO )
170         {
171             log.info( message );
172         }
173         else if ( level == LEVEL_WARN )
174         {
175             log.warn( message );
176         }
177         else if ( level == LEVEL_ERROR )
178         {
179             log.error( message );
180         }
181         else
182         {
183             throw new IllegalArgumentException( "Unknown log level: " + level );
184         }
185     }
186 
187     public static void log( Log log, int level, CharSequence message, CharSequence prefix )
188     {
189         log( log, level, format( message, prefix ) );
190     }
191 
192     public static boolean isEnabled( Log log, int level )
193     {
194         boolean enabled;
195 
196         if ( level == LEVEL_DEBUG )
197         {
198             enabled = log.isDebugEnabled();
199         }
200         else if ( level == LEVEL_INFO )
201         {
202             enabled = log.isInfoEnabled();
203         }
204         else if ( level == LEVEL_WARN )
205         {
206             enabled = log.isWarnEnabled();
207         }
208         else if ( level == LEVEL_ERROR )
209         {
210             enabled = log.isErrorEnabled();
211         }
212         else
213         {
214             throw new IllegalArgumentException( "Unknown log level: " + level );
215         }
216 
217         return enabled;
218     }
219 
220     // private methods --------------------------------------------------------
221 
222     private static CharSequence format( CharSequence message, CharSequence prefix )
223     {
224         if ( prefix == null || prefix.length() == 0 )
225         {
226             return message;
227         }
228 
229         return new StringBuffer( prefix ).append( message ).toString();
230     }
231 }