001    package org.apache.maven.plugin.logging;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.PrintWriter;
023    import java.io.StringWriter;
024    
025    /**
026     * Logger with "standard" output and error output stream.
027     *
028     * @author jdcasey
029     */
030    public class SystemStreamLog
031        implements Log
032    {
033        /**
034         * @see org.apache.maven.plugin.logging.Log#debug(java.lang.CharSequence)
035         */
036        public void debug( CharSequence content )
037        {
038            print( "debug", content );
039        }
040    
041        /**
042         * @see org.apache.maven.plugin.logging.Log#debug(java.lang.CharSequence, java.lang.Throwable)
043         */
044        public void debug( CharSequence content, Throwable error )
045        {
046            print( "debug", content, error );
047        }
048    
049        /**
050         * @see org.apache.maven.plugin.logging.Log#debug(java.lang.Throwable)
051         */
052        public void debug( Throwable error )
053        {
054            print( "debug", error );
055        }
056    
057        /**
058         * @see org.apache.maven.plugin.logging.Log#info(java.lang.CharSequence)
059         */
060        public void info( CharSequence content )
061        {
062            print( "info", content );
063        }
064    
065        /**
066         * @see org.apache.maven.plugin.logging.Log#info(java.lang.CharSequence, java.lang.Throwable)
067         */
068        public void info( CharSequence content, Throwable error )
069        {
070            print( "info", content, error );
071        }
072    
073        /**
074         * @see org.apache.maven.plugin.logging.Log#info(java.lang.Throwable)
075         */
076        public void info( Throwable error )
077        {
078            print( "info", error );
079        }
080    
081        /**
082         * @see org.apache.maven.plugin.logging.Log#warn(java.lang.CharSequence)
083         */
084        public void warn( CharSequence content )
085        {
086            print( "warn", content );
087        }
088    
089        /**
090         * @see org.apache.maven.plugin.logging.Log#warn(java.lang.CharSequence, java.lang.Throwable)
091         */
092        public void warn( CharSequence content, Throwable error )
093        {
094            print( "warn", content, error );
095        }
096    
097        /**
098         * @see org.apache.maven.plugin.logging.Log#warn(java.lang.Throwable)
099         */
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    }