Resolved issue: NETTY-23 (Simplify an exception stack trace.)

* Added DebugUtil and StackTraceSimplifier
* DefaultExceptionEvent and InternalLoggerFactory simplifies the stack trace unless Netty debug mode is enabled.
This commit is contained in:
Trustin Lee 2008-08-21 05:38:43 +00:00
parent 84ea016931
commit 5ac5611e51
4 changed files with 195 additions and 1 deletions

View File

@ -22,6 +22,8 @@
*/
package org.jboss.netty.channel;
import org.jboss.netty.util.StackTraceSimplifier;
public class DefaultExceptionEvent extends DefaultChannelEvent implements
ExceptionEvent {
@ -34,6 +36,7 @@ public class DefaultExceptionEvent extends DefaultChannelEvent implements
throw new NullPointerException("cause");
}
this.cause = cause;
StackTraceSimplifier.simplify(cause);
}
public Throwable getCause() {

View File

@ -22,6 +22,8 @@
*/
package org.jboss.netty.logging;
import org.jboss.netty.util.StackTraceSimplifier;
/**
* Creates an {@link InternalLogger} or changes the default factory
* implementation. This factory allows you to choose what logging framework
@ -76,7 +78,61 @@ public abstract class InternalLoggerFactory {
* Creates a new logger instance with the specified name.
*/
public static InternalLogger getInstance(String name) {
return getDefaultFactory().newInstance(name);
final InternalLogger logger = getDefaultFactory().newInstance(name);
return new InternalLogger() {
public void debug(String msg) {
logger.debug(msg);
}
public void debug(String msg, Throwable cause) {
StackTraceSimplifier.simplify(cause);
logger.debug(msg, cause);
}
public void error(String msg) {
logger.error(msg);
}
public void error(String msg, Throwable cause) {
StackTraceSimplifier.simplify(cause);
logger.error(msg, cause);
}
public void info(String msg) {
logger.info(msg);
}
public void info(String msg, Throwable cause) {
StackTraceSimplifier.simplify(cause);
logger.info(msg, cause);
}
public boolean isDebugEnabled() {
return logger.isDebugEnabled();
}
public boolean isErrorEnabled() {
return logger.isErrorEnabled();
}
public boolean isInfoEnabled() {
return logger.isInfoEnabled();
}
public boolean isWarnEnabled() {
return logger.isWarnEnabled();
}
public void warn(String msg) {
logger.warn(msg);
}
public void warn(String msg, Throwable cause) {
StackTraceSimplifier.simplify(cause);
logger.warn(msg, cause);
}
};
}
/**

View File

@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.util;
/**
* Determines if Netty is running in a debug mode or not. Please note that
* this is not a Java debug mode. You can enable Netty debug mode by
* specifying the {@code "org.jboss.netty.debug"} system property (e.g.
* {@code java -Dorg.jboss.netty.debug ...})
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*/
public class DebugUtil {
/**
* Returns {@code true} if and only if Netty debug mode is enabled.
*/
public static boolean isDebugEnabled() {
String value;
try {
value = System.getProperty("org.jboss.netty.debug");
} catch (Exception e) {
value = null;
}
return value != null;
}
private DebugUtil() {
// Unused
}
}

View File

@ -0,0 +1,80 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.util;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
public class StackTraceSimplifier {
private static final boolean SIMPLIFY_STACK_TRACE = !DebugUtil.isDebugEnabled();
private static final Pattern EXCLUDED_STACK_TRACE =
Pattern.compile(
"^org\\.jboss\\.netty\\." +
"(util\\.(ThreadRenamingRunnable)" +
"|channel\\.(SimpleChannelHandler|DefaultChannelPipeline.*))$");
public static void simplify(Throwable e) {
if (!SIMPLIFY_STACK_TRACE) {
return;
}
if (e.getCause() != null) {
simplify(e.getCause());
}
StackTraceElement[] trace = e.getStackTrace();
if (trace == null || trace.length == 0) {
return;
}
// Perhaps Netty bug. Let's not strip things out.
if (EXCLUDED_STACK_TRACE.matcher(trace[0].getClassName()).matches()) {
return;
}
List<StackTraceElement> simpleTrace =
new ArrayList<StackTraceElement>(trace.length);
simpleTrace.add(trace[0]);
// Remove unnecessary stack trace elements.
for (int i = 1; i < trace.length; i ++) {
if (EXCLUDED_STACK_TRACE.matcher(trace[i].getClassName()).matches()) {
continue;
}
simpleTrace.add(trace[i]);
}
e.setStackTrace(
simpleTrace.toArray(new StackTraceElement[simpleTrace.size()]));
}
}