Skip to content

Commit

Permalink
[SSHD-838] Remove LoggingUtils#logExceptionStackTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Turner authored and Lyor Goldstein committed Aug 1, 2018
1 parent a4bb502 commit f17bf38
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 120 deletions.
Expand Up @@ -31,14 +31,12 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

import org.apache.sshd.common.Closeable;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoHandler;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.logging.LoggingUtils;

/**
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
Expand Down Expand Up @@ -289,7 +287,9 @@ protected boolean okToReaccept(Throwable exc, SocketAddress address) {

log.warn("Caught {} while accepting incoming connection from {}: {}",
exc.getClass().getSimpleName(), address, exc.getMessage());
LoggingUtils.logExceptionStackTrace(log, Level.WARNING, exc);
if (log.isDebugEnabled()) {
log.debug("Incoming connection from " + address + " failure details", exc);
}
return true;
}
}
Expand Down
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.RuntimeSshException;
Expand All @@ -44,7 +43,6 @@
import org.apache.sshd.common.util.Readable;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.closeable.AbstractCloseable;
import org.apache.sshd.common.util.logging.LoggingUtils;

/**
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
Expand Down Expand Up @@ -255,7 +253,9 @@ protected void doCloseImmediately() {
} catch (IOException e) {
log.info("doCloseImmediately({}) {} caught while closing socket={}: {}",
this, e.getClass().getSimpleName(), socket, e.getMessage());
LoggingUtils.logExceptionStackTrace(log, Level.INFO, e);
if (debugEnabled) {
log.debug("doCloseImmediately(" + this + ") socket=" + socket + " close failure details", e);
}
}

service.sessionClosed(this);
Expand Down
Expand Up @@ -19,7 +19,6 @@

package org.apache.sshd.common.util.logging;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand All @@ -30,7 +29,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.logging.Level;
Expand All @@ -44,14 +42,6 @@
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
public final class LoggingUtils {
/**
* Default value used for {@link #logExceptionStackTrace(Logger, Level, Throwable) logExceptionStackTrace}
* unless {@link #setDefaultStackTraceLoggingDepth(int) overridden}
*/
public static final int DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE = Byte.SIZE;

private static final AtomicInteger DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE_HOLDER =
new AtomicInteger(DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE);

private LoggingUtils() {
throw new UnsupportedOperationException("No instance");
Expand Down Expand Up @@ -555,108 +545,4 @@ public String toString() {
}
};
}

/**
* Logs the stack trace of the exception up to {@link #getDefaultStackTraceLoggingDepth() default depth}
* or available stack trace elements.
*
* @param logger The {@link Logger} instance to log the information
* @param level The logging {@link Level} to use
* @param t The {@link Throwable} data to write - ignored if {@code null}
*/
public static void logExceptionStackTrace(Logger logger, Level level, Throwable t) {
logExceptionStackTrace(logger, level, t, getDefaultStackTraceLoggingDepth());
}

/**
* Logs the stack trace of the exception up to specified depth or available stack trace elements.
*
* @param logger The {@link Logger} instance to log the information
* @param level The logging {@link Level} to use
* @param t The {@link Throwable} data to write - ignored if {@code null}
* @param maxDepth Maximum stack trace elements to log - if non-positive then nothing is logged
*/
public static void logExceptionStackTrace(Logger logger, Level level, Throwable t, int maxDepth) {
if ((t == null) || (maxDepth <= 0) || (!isLoggable(logger, level))) {
return;
}

Consumer<String> executor = loggingClosure(logger, level);
logExceptionStackTrace(t, maxDepth, executor);
}

/**
* Logs the stack trace of the exception up to specified depth or available stack trace elements.
*
* @param t The {@link Throwable} data to write - ignored if {@code null}
* @param maxDepth Maximum stack trace elements to log - if non-positive then nothing is logged
* @param executor The {@link Consumer} invoked for each formatted stack trace element
*/
public static void logExceptionStackTrace(Throwable t, int maxDepth, Consumer<? super String> executor) {
if ((t == null) || (maxDepth <= 0) || (executor == null)) {
return;
}
StackTraceElement[] elements = t.getStackTrace();
int numElements = GenericUtils.length(elements);
StringBuilder workBuf = new StringBuilder(Byte.MAX_VALUE);
for (int index = 0, maxElements = Math.min(maxDepth, numElements); index < maxElements; index++) {
StackTraceElement ste = elements[index];
workBuf.setLength(0); // re-use
try {
appendStackTraceElement(workBuf.append(" at "), ste);
} catch (IOException e) {
throw new RuntimeException("Unexpected failure (" + e.getClass().getSimpleName() + ")" + " to append stack-trace-element=" + ste + ": " + e.getMessage(), e);
}
executor.accept(workBuf.toString());
}
}

/**
* @return The default value used by {@link #logExceptionStackTrace(Logger, Level, Throwable)}
*/
public static int getDefaultStackTraceLoggingDepth() {
return Math.max(DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE_HOLDER.get(), DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE);
}

/**
* @param value The value to set - <B>Note:</B> the effective value is the <U>maximum</U>
* between it and {@value #DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE}
*/
public static void setDefaultStackTraceLoggingDepth(int value) {
DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE_HOLDER.set(Math.max(value, DEFAULT_STACK_TRACE_LOGGING_DEPTH_VALUE));
}

/**
* Generates a result very similar to {@link StackTraceElement#toString()}
*
* @param <A> The {@link Appendable} target type
* @param sb The target appender
* @param ste The {@link StackTraceElement} to append - ignored if {@code null}
* @return The updated appender instance
* @throws IOException If failed to append the data
*/
public static <A extends Appendable> A appendStackTraceElement(A sb, StackTraceElement ste) throws IOException {
if (ste == null) {
return sb;
}

sb.append(ste.getClassName()).append('.').append(ste.getMethodName());
if (ste.isNativeMethod()) {
sb.append("(Native Method)");
return sb;
}

sb.append('(');

String fileName = ste.getFileName();
sb.append(GenericUtils.isEmpty(fileName) ? "Unknown Source" : fileName);

int lineNumber = ste.getLineNumber();
if (lineNumber >= 0) {
sb.append(':').append(Integer.toString(lineNumber));
}
sb.append(')');

return sb;
}
}

0 comments on commit f17bf38

Please sign in to comment.