001    package org.apache.maven.cli;
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.PrintStream;
023    
024    import org.apache.maven.Maven;
025    import org.codehaus.plexus.logging.AbstractLogger;
026    import org.codehaus.plexus.logging.Logger;
027    
028    /**
029     * Logs to a user-supplied {@link PrintStream}.
030     * 
031     * @author Benjamin Bentmann
032     */
033    public class PrintStreamLogger
034        extends AbstractLogger
035    {
036    
037        static interface Provider
038        {
039            PrintStream getStream();
040        }
041    
042        private Provider provider;
043    
044        private static final String FATAL_ERROR = "[FATAL] ";
045    
046        private static final String ERROR = "[ERROR] ";
047    
048        private static final String WARNING = "[WARNING] ";
049    
050        private static final String INFO = "[INFO] ";
051    
052        private static final String DEBUG = "[DEBUG] ";
053    
054        public PrintStreamLogger( Provider provider )
055        {
056            super( Logger.LEVEL_INFO, Maven.class.getName() );
057    
058            if ( provider == null )
059            {
060                throw new IllegalArgumentException( "output stream provider missing" );
061            }
062            this.provider = provider;
063        }
064    
065        public PrintStreamLogger( PrintStream out )
066        {
067            super( Logger.LEVEL_INFO, Maven.class.getName() );
068    
069            setStream( out );
070        }
071    
072        public void setStream( final PrintStream out )
073        {
074            if ( out == null )
075            {
076                throw new IllegalArgumentException( "output stream missing" );
077            }
078    
079            this.provider = new Provider()
080            {
081                public PrintStream getStream()
082                {
083                    return out;
084                }
085            };
086        }
087    
088        public void debug( String message, Throwable throwable )
089        {
090            if ( isDebugEnabled() )
091            {
092                PrintStream out = provider.getStream();
093    
094                out.print( DEBUG );
095                out.println( message );
096    
097                if ( null != throwable )
098                {
099                    throwable.printStackTrace( out );
100                }
101            }
102        }
103    
104        public void info( String message, Throwable throwable )
105        {
106            if ( isInfoEnabled() )
107            {
108                PrintStream out = provider.getStream();
109    
110                out.print( INFO );
111                out.println( message );
112    
113                if ( null != throwable )
114                {
115                    throwable.printStackTrace( out );
116                }
117            }
118        }
119    
120        public void warn( String message, Throwable throwable )
121        {
122            if ( isWarnEnabled() )
123            {
124                PrintStream out = provider.getStream();
125    
126                out.print( WARNING );
127                out.println( message );
128    
129                if ( null != throwable )
130                {
131                    throwable.printStackTrace( out );
132                }
133            }
134        }
135    
136        public void error( String message, Throwable throwable )
137        {
138            if ( isErrorEnabled() )
139            {
140                PrintStream out = provider.getStream();
141    
142                out.print( ERROR );
143                out.println( message );
144    
145                if ( null != throwable )
146                {
147                    throwable.printStackTrace( out );
148                }
149            }
150        }
151    
152        public void fatalError( String message, Throwable throwable )
153        {
154            if ( isFatalErrorEnabled() )
155            {
156                PrintStream out = provider.getStream();
157    
158                out.print( FATAL_ERROR );
159                out.println( message );
160    
161                if ( null != throwable )
162                {
163                    throwable.printStackTrace( out );
164                }
165            }
166        }
167    
168        public void close()
169        {
170            PrintStream out = provider.getStream();
171    
172            if ( out == System.out || out == System.err )
173            {
174                out.flush();
175            }
176            else
177            {
178                out.close();
179            }
180        }
181    
182        public Logger getChildLogger( String arg0 )
183        {
184            return this;
185        }
186    
187    }