001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.util;
018
019import java.util.Stack;
020
021/**
022 * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3>
023 */
024public final class StackLocatorUtil {
025    private static StackLocator stackLocator = null;
026
027    static {
028        stackLocator = StackLocator.getInstance();
029    }
030
031    private StackLocatorUtil() {
032    }
033
034    // TODO: return Object.class instead of null (though it will have a null ClassLoader)
035    // (MS) I believe this would work without any modifications elsewhere, but I could be wrong
036
037    // migrated from ReflectiveCallerClassUtility
038    @PerformanceSensitive
039    public static Class<?> getCallerClass(final int depth) {
040        return stackLocator.getCallerClass(depth + 1);
041    }
042
043    public static StackTraceElement getStackTraceElement(final int depth) {
044        return stackLocator.getStackTraceElement(depth + 1);
045    }
046    // migrated from ClassLoaderContextSelector
047    @PerformanceSensitive
048    public static Class<?> getCallerClass(final String fqcn) {
049        return getCallerClass(fqcn, Strings.EMPTY);
050    }
051
052    // migrated from Log4jLoggerFactory
053    @PerformanceSensitive
054    public static Class<?> getCallerClass(final String fqcn, final String pkg) {
055        return stackLocator.getCallerClass(fqcn, pkg);
056    }
057
058    // added for use in LoggerAdapter implementations mainly
059    @PerformanceSensitive
060    public static Class<?> getCallerClass(final Class<?> anchor) {
061        return stackLocator.getCallerClass(anchor);
062    }
063
064    // migrated from ThrowableProxy
065    @PerformanceSensitive
066    public static Stack<Class<?>> getCurrentStackTrace() {
067        return stackLocator.getCurrentStackTrace();
068    }
069
070    public static StackTraceElement calcLocation(final String fqcnOfLogger) {
071        return stackLocator.calcLocation(fqcnOfLogger);
072    }
073}