1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
32
33
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
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
53
54 private Log realLogger;
55
56
57
58
59
60 public ColorConsoleLogger( Log realLogger )
61 {
62 this.realLogger = realLogger;
63 }
64
65
66 public boolean isDebugEnabled()
67 {
68 return this.realLogger.isDebugEnabled();
69 }
70
71
72 public void debug( CharSequence content )
73 {
74 this.realLogger.debug( debugColor + content + END_COLOR );
75 }
76
77
78 public void debug( CharSequence content, Throwable error )
79 {
80 this.debug( content + "\n\n" + formatError( error ) );
81 }
82
83
84 public void debug( Throwable error )
85 {
86 this.debug( formatError( error ) );
87 }
88
89
90 public boolean isInfoEnabled()
91 {
92 return this.realLogger.isInfoEnabled();
93 }
94
95
96 public void info( CharSequence content )
97 {
98 this.realLogger.info( infoColor + content + END_COLOR );
99 }
100
101
102 public void info( CharSequence content, Throwable error )
103 {
104 this.info( content + "\n\n" + formatError( error ) );
105 }
106
107
108 public void info( Throwable error )
109 {
110 this.info( formatError( error ) );
111 }
112
113
114 public boolean isWarnEnabled()
115 {
116 return this.realLogger.isWarnEnabled();
117 }
118
119
120 public void warn( CharSequence content )
121 {
122 this.realLogger.warn( warnColor + content + END_COLOR );
123 }
124
125
126 public void warn( CharSequence content, Throwable error )
127 {
128 this.warn( content + "\n\n" + formatError( error ) );
129 }
130
131
132 public void warn( Throwable error )
133 {
134 this.warn( formatError( error ) );
135 }
136
137
138 public boolean isErrorEnabled()
139 {
140 return this.realLogger.isErrorEnabled();
141 }
142
143
144 public void error( CharSequence content )
145 {
146 this.realLogger.error( errColor + content + END_COLOR );
147 }
148
149
150 public void error( CharSequence content, Throwable error )
151 {
152 this.error( content + "\n\n" + formatError( error ) );
153 }
154
155
156 public void error( Throwable error )
157 {
158 this.error( formatError( error ) );
159 }
160
161
162
163
164
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 }