View Javadoc

1   package org.apache.maven.plugin.logging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  
25  /**
26   * Logger with "standard" output and error output stream.
27   *
28   * @author jdcasey
29   */
30  public class SystemStreamLog
31      implements Log
32  {
33      /**
34       * @see org.apache.maven.plugin.logging.Log#debug(java.lang.CharSequence)
35       */
36      public void debug( CharSequence content )
37      {
38          print( "debug", content );
39      }
40  
41      /**
42       * @see org.apache.maven.plugin.logging.Log#debug(java.lang.CharSequence, java.lang.Throwable)
43       */
44      public void debug( CharSequence content, Throwable error )
45      {
46          print( "debug", content, error );
47      }
48  
49      /**
50       * @see org.apache.maven.plugin.logging.Log#debug(java.lang.Throwable)
51       */
52      public void debug( Throwable error )
53      {
54          print( "debug", error );
55      }
56  
57      /**
58       * @see org.apache.maven.plugin.logging.Log#info(java.lang.CharSequence)
59       */
60      public void info( CharSequence content )
61      {
62          print( "info", content );
63      }
64  
65      /**
66       * @see org.apache.maven.plugin.logging.Log#info(java.lang.CharSequence, java.lang.Throwable)
67       */
68      public void info( CharSequence content, Throwable error )
69      {
70          print( "info", content, error );
71      }
72  
73      /**
74       * @see org.apache.maven.plugin.logging.Log#info(java.lang.Throwable)
75       */
76      public void info( Throwable error )
77      {
78          print( "info", error );
79      }
80  
81      /**
82       * @see org.apache.maven.plugin.logging.Log#warn(java.lang.CharSequence)
83       */
84      public void warn( CharSequence content )
85      {
86          print( "warn", content );
87      }
88  
89      /**
90       * @see org.apache.maven.plugin.logging.Log#warn(java.lang.CharSequence, java.lang.Throwable)
91       */
92      public void warn( CharSequence content, Throwable error )
93      {
94          print( "warn", content, error );
95      }
96  
97      /**
98       * @see org.apache.maven.plugin.logging.Log#warn(java.lang.Throwable)
99       */
100     public void warn( Throwable error )
101     {
102         print( "warn", error );
103     }
104 
105     /**
106      * @see org.apache.maven.plugin.logging.Log#error(java.lang.CharSequence)
107      */
108     public void error( CharSequence content )
109     {
110         System.err.println( "[error] " + content.toString() );
111     }
112 
113     /**
114      * @see org.apache.maven.plugin.logging.Log#error(java.lang.CharSequence, java.lang.Throwable)
115      */
116     public void error( CharSequence content, Throwable error )
117     {
118         StringWriter sWriter = new StringWriter();
119         PrintWriter pWriter = new PrintWriter( sWriter );
120 
121         error.printStackTrace( pWriter );
122 
123         System.err.println( "[error] " + content.toString() + "\n\n" + sWriter.toString() );
124     }
125 
126     /**
127      * @see org.apache.maven.plugin.logging.Log#error(java.lang.Throwable)
128      */
129     public void error( Throwable error )
130     {
131         StringWriter sWriter = new StringWriter();
132         PrintWriter pWriter = new PrintWriter( sWriter );
133 
134         error.printStackTrace( pWriter );
135 
136         System.err.println( "[error] " + sWriter.toString() );
137     }
138 
139     /**
140      * @see org.apache.maven.plugin.logging.Log#isDebugEnabled()
141      */
142     public boolean isDebugEnabled()
143     {
144         // TODO: Not sure how best to set these for this implementation...
145         return false;
146     }
147 
148     /**
149      * @see org.apache.maven.plugin.logging.Log#isInfoEnabled()
150      */
151     public boolean isInfoEnabled()
152     {
153         return true;
154     }
155 
156     /**
157      * @see org.apache.maven.plugin.logging.Log#isWarnEnabled()
158      */
159     public boolean isWarnEnabled()
160     {
161         return true;
162     }
163 
164     /**
165      * @see org.apache.maven.plugin.logging.Log#isErrorEnabled()
166      */
167     public boolean isErrorEnabled()
168     {
169         return true;
170     }
171 
172     private void print( String prefix, CharSequence content )
173     {
174         System.out.println( "[" + prefix + "] " + content.toString() );
175     }
176 
177     private void print( String prefix, Throwable error )
178     {
179         StringWriter sWriter = new StringWriter();
180         PrintWriter pWriter = new PrintWriter( sWriter );
181 
182         error.printStackTrace( pWriter );
183 
184         System.out.println( "[" + prefix + "] " + sWriter.toString() );
185     }
186 
187     private void print( String prefix, CharSequence content, Throwable error )
188     {
189         StringWriter sWriter = new StringWriter();
190         PrintWriter pWriter = new PrintWriter( sWriter );
191 
192         error.printStackTrace( pWriter );
193 
194         System.out.println( "[" + prefix + "] " + content.toString() + "\n\n" + sWriter.toString() );
195     }
196 }