View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  import java.util.Stack;
20  
21  /**
22   * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3>
23   */
24  public final class StackLocatorUtil {
25      private static StackLocator stackLocator = null;
26  
27      static {
28          stackLocator = StackLocator.getInstance();
29      }
30  
31      private StackLocatorUtil() {
32      }
33  
34      // TODO: return Object.class instead of null (though it will have a null ClassLoader)
35      // (MS) I believe this would work without any modifications elsewhere, but I could be wrong
36  
37      // migrated from ReflectiveCallerClassUtility
38      @PerformanceSensitive
39      public static Class<?> getCallerClass(final int depth) {
40          return stackLocator.getCallerClass(depth + 1);
41      }
42  
43      public static StackTraceElement getStackTraceElement(final int depth) {
44          return stackLocator.getStackTraceElement(depth + 1);
45      }
46      // migrated from ClassLoaderContextSelector
47      @PerformanceSensitive
48      public static Class<?> getCallerClass(final String fqcn) {
49          return getCallerClass(fqcn, Strings.EMPTY);
50      }
51  
52      // migrated from Log4jLoggerFactory
53      @PerformanceSensitive
54      public static Class<?> getCallerClass(final String fqcn, final String pkg) {
55          return stackLocator.getCallerClass(fqcn, pkg);
56      }
57  
58      // added for use in LoggerAdapter implementations mainly
59      @PerformanceSensitive
60      public static Class<?> getCallerClass(final Class<?> anchor) {
61          return stackLocator.getCallerClass(anchor);
62      }
63  
64      // migrated from ThrowableProxy
65      @PerformanceSensitive
66      public static Stack<Class<?>> getCurrentStackTrace() {
67          return stackLocator.getCurrentStackTrace();
68      }
69  
70      public static StackTraceElement calcLocation(final String fqcnOfLogger) {
71          return stackLocator.calcLocation(fqcnOfLogger);
72      }
73  }