001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.spi;
015
016import ch.qos.logback.core.CoreConstants;
017
018import java.util.List;
019
020/**
021 * This class computes caller data returning the result in the form
022 * of a StackTraceElement array.
023 *
024 * @author Ceki Gülcü
025 */
026public class CallerData {
027
028    /**
029     * When caller information is not available this constant is used for file
030     * name, method name, etc.
031     */
032    public static final String NA = "?";
033
034    // All logger call's in log4j-over-slf4j use the Category class
035    private static final String LOG4J_CATEGORY = "org.apache.log4j.Category";
036    private static final String SLF4J_BOUNDARY = "org.slf4j.Logger";
037
038    /**
039     * When caller information is not available this constant is used for the line
040     * number.
041     */
042    public static final int LINE_NA = -1;
043
044    public static final String CALLER_DATA_NA = "?#?:?" + CoreConstants.LINE_SEPARATOR;
045
046    /**
047     * This value is returned in case no caller data could be extracted.
048     */
049    public static final StackTraceElement[] EMPTY_CALLER_DATA_ARRAY = new StackTraceElement[0];
050
051    /**
052     * Extract caller data information as an array based on a Throwable passed as
053     * parameter
054     */
055    public static StackTraceElement[] extract(Throwable t, String fqnOfInvokingClass, final int maxDepth, List<String> frameworkPackageList) {
056        if (t == null) {
057            return null;
058        }
059
060        StackTraceElement[] steArray = t.getStackTrace();
061        StackTraceElement[] callerDataArray;
062
063        int found = LINE_NA;
064        for (int i = 0; i < steArray.length; i++) {
065            if (isInFrameworkSpace(steArray[i].getClassName(), fqnOfInvokingClass, frameworkPackageList)) {
066                // the caller is assumed to be the next stack frame, hence the +1.
067                found = i + 1;
068            } else {
069                if (found != LINE_NA) {
070                    break;
071                }
072            }
073        }
074
075        // we failed to extract caller data
076        if (found == LINE_NA) {
077            return EMPTY_CALLER_DATA_ARRAY;
078        }
079
080        int availableDepth = steArray.length - found;
081        int desiredDepth = maxDepth < (availableDepth) ? maxDepth : availableDepth;
082
083        callerDataArray = new StackTraceElement[desiredDepth];
084        for (int i = 0; i < desiredDepth; i++) {
085            callerDataArray[i] = steArray[found + i];
086        }
087        return callerDataArray;
088    }
089
090    static boolean isInFrameworkSpace(String currentClass, String fqnOfInvokingClass, List<String> frameworkPackageList) {
091        // the check for org.apache.log4j.Category class is intended to support
092        // log4j-over-slf4j. it solves http://bugzilla.slf4j.org/show_bug.cgi?id=66
093        if (currentClass.equals(fqnOfInvokingClass) || currentClass.equals(LOG4J_CATEGORY) || currentClass.startsWith(SLF4J_BOUNDARY)
094                        || isInFrameworkSpaceList(currentClass, frameworkPackageList)) {
095            return true;
096        } else {
097            return false;
098        }
099    }
100
101    /**
102     * Is currentClass present in the list of packages considered part of the logging framework?
103     */
104    private static boolean isInFrameworkSpaceList(String currentClass, List<String> frameworkPackageList) {
105        if (frameworkPackageList == null)
106            return false;
107
108        for (String s : frameworkPackageList) {
109            if (currentClass.startsWith(s))
110                return true;
111        }
112        return false;
113    }
114
115    /**
116     * Returns a StackTraceElement where all string fields are set to {@link #NA} and line number is set to {@link #LINE_NA}.
117     *
118     * @return StackTraceElement with values set to NA constants.
119     * @since 1.0.10
120     */
121    public static StackTraceElement naInstance() {
122        return new StackTraceElement(NA, NA, NA, LINE_NA);
123    }
124
125}