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.NoSuchElementException; 020import java.util.Stack; 021 022import org.apache.logging.log4j.status.StatusLogger; 023 024/** 025 * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3> 026 */ 027public final class StackLocatorUtil { 028 private static StackLocator stackLocator = null; 029 private static volatile boolean errorLogged = false; 030 031 static { 032 stackLocator = StackLocator.getInstance(); 033 } 034 035 private StackLocatorUtil() { 036 } 037 038 // TODO: return Object.class instead of null (though it will have a null ClassLoader) 039 // (MS) I believe this would work without any modifications elsewhere, but I could be wrong 040 041 // migrated from ReflectiveCallerClassUtility 042 @PerformanceSensitive 043 public static Class<?> getCallerClass(final int depth) { 044 return stackLocator.getCallerClass(depth + 1); 045 } 046 047 public static StackTraceElement getStackTraceElement(final int depth) { 048 return stackLocator.getStackTraceElement(depth + 1); 049 } 050 // migrated from ClassLoaderContextSelector 051 @PerformanceSensitive 052 public static Class<?> getCallerClass(final String fqcn) { 053 return getCallerClass(fqcn, Strings.EMPTY); 054 } 055 056 // migrated from Log4jLoggerFactory 057 @PerformanceSensitive 058 public static Class<?> getCallerClass(final String fqcn, final String pkg) { 059 return stackLocator.getCallerClass(fqcn, pkg); 060 } 061 062 // added for use in LoggerAdapter implementations mainly 063 @PerformanceSensitive 064 public static Class<?> getCallerClass(final Class<?> anchor) { 065 return stackLocator.getCallerClass(anchor); 066 } 067 068 // migrated from ThrowableProxy 069 @PerformanceSensitive 070 public static Stack<Class<?>> getCurrentStackTrace() { 071 return stackLocator.getCurrentStackTrace(); 072 } 073 074 public static StackTraceElement calcLocation(final String fqcnOfLogger) { 075 try { 076 return stackLocator.calcLocation(fqcnOfLogger); 077 } catch (NoSuchElementException ex) { 078 if (!errorLogged) { 079 errorLogged = true; 080 StatusLogger.getLogger().warn("Unable to locate stack trace element for {}", fqcnOfLogger, ex); 081 } 082 return null; 083 } 084 } 085}