001package org.apache.maven.doxia.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 022import java.io.PrintWriter; 023import java.io.StringWriter; 024 025/** 026 * Logger with "standard" output and error output stream. The log prefix is voluntarily in lower case. 027 * <br/> 028 * Based on <code>org.apache.maven.plugin.logging.SystemStreamLog</code>. 029 * 030 * @author jdcasey 031 * @author ltheussl 032 * @version $Id$ 033 * @since 1.1 034 */ 035public class SystemStreamLog 036 implements Log 037{ 038 private static final String EOL = System.getProperty( "line.separator" ); 039 040 private int currentLevel = LEVEL_INFO; 041 042 /** {@inheritDoc} */ 043 public void setLogLevel( int level ) 044 { 045 if ( level <= LEVEL_DEBUG ) 046 { 047 currentLevel = LEVEL_DEBUG; 048 } 049 else if ( level <= LEVEL_INFO ) 050 { 051 currentLevel = LEVEL_INFO; 052 } 053 else if ( level <= LEVEL_WARN ) 054 { 055 currentLevel = LEVEL_WARN; 056 } 057 else if ( level <= LEVEL_ERROR ) 058 { 059 currentLevel = LEVEL_ERROR; 060 } 061 else 062 { 063 currentLevel = LEVEL_DISABLED; 064 } 065 } 066 067 /** {@inheritDoc} */ 068 public void debug( CharSequence content ) 069 { 070 if ( isDebugEnabled() ) 071 { 072 print( "debug", content ); 073 } 074 } 075 076 /** {@inheritDoc} */ 077 public void debug( CharSequence content, Throwable error ) 078 { 079 if ( isDebugEnabled() ) 080 { 081 print( "debug", content, error ); 082 } 083 } 084 085 /** {@inheritDoc} */ 086 public void debug( Throwable error ) 087 { 088 if ( isDebugEnabled() ) 089 { 090 print( "debug", error ); 091 } 092 } 093 094 /** {@inheritDoc} */ 095 public void info( CharSequence content ) 096 { 097 if ( isInfoEnabled() ) 098 { 099 print( "info", content ); 100 } 101 } 102 103 /** {@inheritDoc} */ 104 public void info( CharSequence content, Throwable error ) 105 { 106 if ( isInfoEnabled() ) 107 { 108 print( "info", content, error ); 109 } 110 } 111 112 /** {@inheritDoc} */ 113 public void info( Throwable error ) 114 { 115 if ( isInfoEnabled() ) 116 { 117 print( "info", error ); 118 } 119 } 120 121 /** {@inheritDoc} */ 122 public void warn( CharSequence content ) 123 { 124 if ( isWarnEnabled() ) 125 { 126 print( "warn", content ); 127 } 128 } 129 130 /** {@inheritDoc} */ 131 public void warn( CharSequence content, Throwable error ) 132 { 133 if ( isWarnEnabled() ) 134 { 135 print( "warn", content, error ); 136 } 137 } 138 139 /** {@inheritDoc} */ 140 public void warn( Throwable error ) 141 { 142 if ( isWarnEnabled() ) 143 { 144 print( "warn", error ); 145 } 146 } 147 148 /** {@inheritDoc} */ 149 public void error( CharSequence content ) 150 { 151 if ( isErrorEnabled() ) 152 { 153 System.err.println( "[error] " + content.toString() ); 154 } 155 } 156 157 /** {@inheritDoc} */ 158 public void error( CharSequence content, Throwable error ) 159 { 160 if ( isErrorEnabled() ) 161 { 162 StringWriter sWriter = new StringWriter(); 163 PrintWriter pWriter = new PrintWriter( sWriter ); 164 165 error.printStackTrace( pWriter ); 166 167 System.err.println( "[error] " + content.toString() 168 + EOL + EOL + sWriter.toString() ); 169 } 170 } 171 172 /** {@inheritDoc} */ 173 public void error( Throwable error ) 174 { 175 if ( isErrorEnabled() ) 176 { 177 StringWriter sWriter = new StringWriter(); 178 PrintWriter pWriter = new PrintWriter( sWriter ); 179 180 error.printStackTrace( pWriter ); 181 182 System.err.println( "[error] " + sWriter.toString() ); 183 } 184 } 185 186 /** {@inheritDoc} */ 187 public boolean isDebugEnabled() 188 { 189 return ( currentLevel <= LEVEL_DEBUG ); 190 } 191 192 /** {@inheritDoc} */ 193 public boolean isInfoEnabled() 194 { 195 return ( currentLevel <= LEVEL_INFO ); 196 } 197 198 /** {@inheritDoc} */ 199 public boolean isWarnEnabled() 200 { 201 return ( currentLevel <= LEVEL_WARN ); 202 } 203 204 /** {@inheritDoc} */ 205 public boolean isErrorEnabled() 206 { 207 return ( currentLevel <= LEVEL_ERROR ); 208 } 209 210 // 211 // private 212 // 213 214 private void print( String prefix, CharSequence content ) 215 { 216 System.out.println( "[" + prefix + "] " + content.toString() ); 217 } 218 219 private void print( String prefix, Throwable error ) 220 { 221 StringWriter sWriter = new StringWriter(); 222 PrintWriter pWriter = new PrintWriter( sWriter ); 223 224 error.printStackTrace( pWriter ); 225 226 System.out.println( "[" + prefix + "] " + sWriter.toString() ); 227 } 228 229 private void print( String prefix, CharSequence content, Throwable error ) 230 { 231 StringWriter sWriter = new StringWriter(); 232 PrintWriter pWriter = new PrintWriter( sWriter ); 233 234 error.printStackTrace( pWriter ); 235 236 System.out.println( "[" + prefix + "] " + content.toString() 237 + EOL + EOL + sWriter.toString() ); 238 } 239}