All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.log.Trace Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/*
 * $Id: Trace.java,v 1.5 2011/07/09 21:43:22 oboehm Exp $
 *
 * Copyright (c) 2010 by Oliver Boehm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 30.09.2010 by oliver ([email protected])
 */

package patterntesting.runtime.log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.SourceLocation;
import org.slf4j.*;

import patterntesting.runtime.util.*;

/**
 * This class works together with the (Abstract)TraceAspect to provide some
 * kind of tracing information.
 *
 * @author oliver
 * @since 1.0.3 (30.09.2010)
 */
public final class Trace {

    private static final Logger log = LoggerFactory.getLogger(Trace.class);
    private static final int indentStart = calibrateStacktraceDepth();

    static {
        if (log.isTraceEnabled()) {
            log.trace(Trace.class + " loaded");
        } else if (log.isDebugEnabled()) {
            log.info("to see more trace information set log level to TRACE for "
                    + Trace.class.getName());
        } else {
            log.info("to see @TraceMe information you must set log level to DEBUG for "
                    + Trace.class.getName());
        }
    }

    /** Utility class - no need to instantiate it. */
    private Trace() {}

    private static int calibrateStacktraceDepth() {
        StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
        return stacktrace.length;
    }

    /**
     * Logs the trace with a start sign (">").
     *
     * @param joinpoint the joinpoint
     */
    public static void start(final JoinPoint joinpoint) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, "> ", joinpoint, "");
        }
    }

    /**
     * Logs the trace with a start sign ("<").
     *
     * @param joinpoint the joinpoint
     */
    public static void end(final JoinPoint joinpoint) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, "< ", joinpoint, "");
        }
    }

    /**
     * Logs the trace with a start sign ("<").
     *
     * @param joinpoint the joinpoint
     * @param result the result
     */
    public static void end(final JoinPoint joinpoint, final Object result) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, "< ", joinpoint, " = " + Converter.toShortString(result));
        }
    }

    /**
     * Logs the thrown exception.
     *
     * @param joinpoint the joinpoint
     * @param t the t
     */
    public static void throwing(final JoinPoint joinpoint, final Throwable t) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, "<*", joinpoint, "");
            trace(level, " *** ", t);
        }
    }

    /**
     * Logs the trace with a start sign ("<").
     *
     * @param joinpoint the joinpoint
     * @param suffix the suffix
     */
    public static void end(final JoinPoint joinpoint, final String suffix) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, "< ", joinpoint, suffix);
        }
    }

    /**
     * Logs the trace with a "|" sign.
     *
     * @param joinpoint the joinpoint
     */
    public static void trace(final JoinPoint joinpoint) {
        if (log.isDebugEnabled()) {
            int level = getIndentLevel();
            trace(level, " | ", joinpoint, "");
        }
    }

    private static void trace(final int level, final String prefix, final JoinPoint joinpoint,
            final String suffix) {
        if (log.isTraceEnabled()) {
            String loc = getLocation(joinpoint);
            log.trace(indent(level) + prefix + JoinPointHelper.getAsLongString(joinpoint) + suffix
                    + loc);
        } else if (log.isDebugEnabled()) {
            log.debug(indent(level) + prefix + JoinPointHelper.getAsShortString(joinpoint) + suffix);
        }
    }

    private static void trace(final int level, final String prefix, final Throwable t) {
        if (log.isTraceEnabled()) {
            log.trace(indent(level) + prefix, t);
        } else if (log.isDebugEnabled()) {
            log.debug(indent(level) + prefix + t);
        }
    }

    private static String indent(final int level) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < level; i++) {
            buffer.append("  ");
        }
        return buffer.toString();
    }

    private static int getIndentLevel() {
        StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
        return stacktrace.length - indentStart;
    }

    private static String getLocation(final JoinPoint joinpoint) {
        SourceLocation loc = joinpoint.getSourceLocation();
        return " (" + loc.getFileName() + ":" + loc.getLine() + ")";
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy