View Javadoc
1   package org.apache.maven.plugins.surefire.report;
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 org.apache.maven.plugin.logging.Log;
23  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
24  import org.apache.maven.shared.utils.logging.MessageBuilder;
25  
26  import static java.lang.Integer.numberOfLeadingZeros;
27  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
28  
29  /**
30   * Wrapper logger of miscellaneous (Maven 2.2.1 or 3.1) implementations of {@link Log}.
31   * Calling {@link Log#isInfoEnabled()} before {@link Log#info(CharSequence)} due to Maven 2.2.1.
32   *
33   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
34   * @since 2.20
35   * @see ConsoleLogger
36   */
37  final class PluginConsoleLogger
38      implements ConsoleLogger
39  {
40      private final Log mojoLogger;
41  
42      PluginConsoleLogger( Log mojoLogger )
43      {
44          this.mojoLogger = mojoLogger;
45      }
46  
47      @Override
48      public boolean isDebugEnabled()
49      {
50          return mojoLogger.isDebugEnabled();
51      }
52  
53      @Override
54      public void debug( String message )
55      {
56          if ( mojoLogger.isDebugEnabled() )
57          {
58              mojoLogger.debug( createAnsiBuilder( message ).debug( message ).toString() );
59          }
60      }
61  
62      public void debug( CharSequence content, Throwable error )
63      {
64          if ( mojoLogger.isDebugEnabled() )
65          {
66              mojoLogger.debug( content, error );
67          }
68      }
69  
70      @Override
71      public boolean isInfoEnabled()
72      {
73          return mojoLogger.isInfoEnabled();
74      }
75  
76      @Override
77      public void info( String message )
78      {
79          if ( mojoLogger.isInfoEnabled() )
80          {
81              mojoLogger.info( createAnsiBuilder( message ).info( message ).toString() );
82          }
83      }
84  
85      @Override
86      public boolean isWarnEnabled()
87      {
88          return mojoLogger.isWarnEnabled();
89      }
90  
91      @Override
92      public void warning( String message )
93      {
94          if ( mojoLogger.isWarnEnabled() )
95          {
96              mojoLogger.warn( createAnsiBuilder( message ).warning( message ).toString() );
97          }
98      }
99  
100     public void warn( CharSequence content, Throwable error )
101     {
102         if ( mojoLogger.isWarnEnabled() )
103         {
104             mojoLogger.warn( content, error );
105         }
106     }
107 
108     @Override
109     public boolean isErrorEnabled()
110     {
111         return mojoLogger.isErrorEnabled();
112     }
113 
114     @Override
115     public void error( String message )
116     {
117         if ( mojoLogger.isErrorEnabled() )
118         {
119             mojoLogger.error( createAnsiBuilder( message ).error( message ).toString() );
120         }
121     }
122 
123     @Override
124     public void error( String message, Throwable t )
125     {
126         if ( mojoLogger.isErrorEnabled() )
127         {
128             mojoLogger.error( message, t );
129         }
130     }
131 
132     @Override
133     public void error( Throwable t )
134     {
135         if ( mojoLogger.isErrorEnabled() )
136         {
137             mojoLogger.error( t );
138         }
139     }
140 
141     private static MessageBuilder createAnsiBuilder( CharSequence message )
142     {
143         return buffer( bufferSize( message ) );
144     }
145 
146     private static int bufferSize( CharSequence message )
147     {
148         return 32 - numberOfLeadingZeros( message.length() );
149     }
150 }