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.NoSuchElementException;
20  import java.util.Stack;
21  
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  /**
25   * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3>
26   */
27  public final class StackLocatorUtil {
28      private static StackLocator stackLocator = null;
29      private static volatile boolean errorLogged = false;
30  
31      static {
32          stackLocator = StackLocator.getInstance();
33      }
34  
35      private StackLocatorUtil() {
36      }
37  
38      // TODO: return Object.class instead of null (though it will have a null ClassLoader)
39      // (MS) I believe this would work without any modifications elsewhere, but I could be wrong
40  
41      // migrated from ReflectiveCallerClassUtility
42      @PerformanceSensitive
43      public static Class<?> getCallerClass(final int depth) {
44          return stackLocator.getCallerClass(depth + 1);
45      }
46  
47      public static StackTraceElement getStackTraceElement(final int depth) {
48          return stackLocator.getStackTraceElement(depth + 1);
49      }
50      // migrated from ClassLoaderContextSelector
51      @PerformanceSensitive
52      public static Class<?> getCallerClass(final String fqcn) {
53          return getCallerClass(fqcn, Strings.EMPTY);
54      }
55  
56      // migrated from Log4jLoggerFactory
57      @PerformanceSensitive
58      public static Class<?> getCallerClass(final String fqcn, final String pkg) {
59          return stackLocator.getCallerClass(fqcn, pkg);
60      }
61  
62      // added for use in LoggerAdapter implementations mainly
63      @PerformanceSensitive
64      public static Class<?> getCallerClass(final Class<?> anchor) {
65          return stackLocator.getCallerClass(anchor);
66      }
67  
68      // migrated from ThrowableProxy
69      @PerformanceSensitive
70      public static Stack<Class<?>> getCurrentStackTrace() {
71          return stackLocator.getCurrentStackTrace();
72      }
73  
74      public static StackTraceElement calcLocation(final String fqcnOfLogger) {
75          try {
76              return stackLocator.calcLocation(fqcnOfLogger);
77          } catch (NoSuchElementException ex) {
78              if (!errorLogged) {
79                  errorLogged = true;
80                  StatusLogger.getLogger().warn("Unable to locate stack trace element for {}", fqcnOfLogger, ex);
81              }
82              return null;
83          }
84      }
85  }