View Javadoc

1   /*
2   The MIT License
3   
4   Copyright (c) 2004, The Codehaus
5   
6   Permission is hereby granted, free of charge, to any person obtaining a copy of
7   this software and associated documentation files (the "Software"), to deal in
8   the Software without restriction, including without limitation the rights to
9   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  of the Software, and to permit persons to whom the Software is furnished to do
11  so, subject to the following conditions:
12  
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15  
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23   */
24  package org.codehaus.mojo.scmchangelog.scm.util;
25  
26  import java.io.PrintWriter;
27  import java.io.StringWriter;
28  import org.apache.maven.plugin.logging.Log;
29  
30  /**
31   * Logger Wrapper that uses the ANSI Console Commands to add some colors.
32   * @author ehsavoie
33   * @version $Id$
34   */
35  public class ColorConsoleLogger implements Log
36  {
37    private static final int ATTR_DIM = 2;
38    private static final int FG_RED = 31;
39    private static final int FG_GREEN = 32;
40    //private static final int FG_BLUE = 34;
41    private static final int FG_MAGENTA = 35;
42    private static final int FG_CYAN = 36;
43    private static final String PREFIX = "\u001b[";
44    private static final String SUFFIX = "m";
45    private static final char SEPARATOR = ';';
46    private static final String END_COLOR = PREFIX + SUFFIX;
47    private String errColor = PREFIX + ATTR_DIM + SEPARATOR + FG_RED + SUFFIX;
48    private String warnColor = PREFIX + ATTR_DIM + SEPARATOR + FG_MAGENTA + SUFFIX;
49    private String infoColor = PREFIX + ATTR_DIM + SEPARATOR + FG_CYAN + SUFFIX;
50    private String debugColor = PREFIX + ATTR_DIM + SEPARATOR + FG_GREEN + SUFFIX;
51    /**
52     * The 'real' logger implementation.s
53     */
54    private Log realLogger;
55  
56   /**
57    * Create a new ColorConsoleLogger that wraps a maven Log.
58    * @param logger the 'real' logger to be wrapped.
59    **/
60    public ColorConsoleLogger( Log realLogger )
61    {
62      this.realLogger = realLogger;
63    }
64  
65    /** {@inheritDoc} */
66    public boolean isDebugEnabled()
67    {
68      return this.realLogger.isDebugEnabled();
69    }
70  
71    /** {@inheritDoc} */
72    public void debug( CharSequence content )
73    {
74      this.realLogger.debug( debugColor + content + END_COLOR );
75    }
76  
77    /** {@inheritDoc} */
78    public void debug( CharSequence content, Throwable error )
79    {
80      this.debug( content + "\n\n" + formatError( error ) );
81    }
82  
83    /** {@inheritDoc} */
84    public void debug( Throwable error )
85    {
86      this.debug( formatError( error ) );
87    }
88  
89    /** {@inheritDoc} */
90    public boolean isInfoEnabled()
91    {
92      return this.realLogger.isInfoEnabled();
93    }
94  
95    /** {@inheritDoc} */
96    public void info( CharSequence content )
97    {
98      this.realLogger.info( infoColor + content + END_COLOR );
99    }
100 
101   /** {@inheritDoc} */
102   public void info( CharSequence content, Throwable error )
103   {
104     this.info( content + "\n\n" + formatError( error ) );
105   }
106 
107   /** {@inheritDoc} */
108   public void info( Throwable error )
109   {
110     this.info( formatError( error ) );
111   }
112 
113   /** {@inheritDoc} */
114   public boolean isWarnEnabled()
115   {
116     return this.realLogger.isWarnEnabled();
117   }
118 
119   /** {@inheritDoc} */
120   public void warn( CharSequence content )
121   {
122     this.realLogger.warn( warnColor + content + END_COLOR );
123   }
124 
125   /** {@inheritDoc} */
126   public void warn( CharSequence content, Throwable error )
127   {
128     this.warn( content + "\n\n" + formatError( error ) );
129   }
130 
131   /** {@inheritDoc} */
132   public void warn( Throwable error )
133   {
134     this.warn( formatError( error ) );
135   }
136 
137   /** {@inheritDoc} */
138   public boolean isErrorEnabled()
139   {
140     return this.realLogger.isErrorEnabled();
141   }
142 
143   /** {@inheritDoc} */
144   public void error( CharSequence content )
145   {
146     this.realLogger.error( errColor + content + END_COLOR );
147   }
148 
149   /** {@inheritDoc} */
150   public void error( CharSequence content, Throwable error )
151   {
152     this.error( content + "\n\n" + formatError( error ) );
153   }
154 
155   /** {@inheritDoc} */
156   public void error( Throwable error )
157   {
158     this.error( formatError( error ) );
159   }
160 
161   /**
162    * Format the exception to a readable message.
163    * @param error the exception to be formatted.
164    * @return a readable message for the specified exception.
165    */
166   private String formatError( Throwable error )
167   {
168     StringWriter sWriter = new StringWriter();
169     PrintWriter pWriter = new PrintWriter( sWriter );
170     error.printStackTrace( pWriter );
171     return sWriter.toString();
172   }
173 }