1/**
2 * Copyright (c) 2004-2011 QOS.ch
3 * All rights reserved.
4 *
5 * Permission is hereby granted, free  of charge, to any person obtaining
6 * a  copy  of this  software  and  associated  documentation files  (the
7 * "Software"), to  deal in  the Software without  restriction, including
8 * without limitation  the rights to  use, copy, modify,  merge, publish,
9 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10 * permit persons to whom the Software  is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The  above  copyright  notice  and  this permission  notice  shall  be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25package org.slf4j.helpers;
26
27/**
28 * An internal utility class.
29 *
30 * @author Alexander Dorokhine
31 * @author Ceki Gülcü
32 */
33public final class Util {
34
35    private Util() {
36    }
37
38    /**
39     * In order to call {@link SecurityManager#getClassContext()}, which is a
40     * protected method, we add this wrapper which allows the method to be visible
41     * inside this package.
42     */
43    private static final class ClassContextSecurityManager extends SecurityManager {
44        protected Class<?>[] getClassContext() {
45            return super.getClassContext();
46        }
47    }
48
49    private static final ClassContextSecurityManager SECURITY_MANAGER = new ClassContextSecurityManager();
50
51    /**
52     * Returns the name of the class which called the invoking method.
53     *
54     * @return the name of the class which called the invoking method.
55     */
56    public static Class<?> getCallingClass() {
57        Class<?>[] trace = SECURITY_MANAGER.getClassContext();
58        String thisClassName = Util.class.getName();
59
60        // Advance until Util is found
61        int i;
62        for (i = 0; i < trace.length; i++) {
63            if (thisClassName.equals(trace[i].getName()))
64                break;
65        }
66
67        // trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
68        if (i >= trace.length || i + 2 >= trace.length) {
69            throw new IllegalStateException("Failed to find org.slf4j.helpers.Util or its caller in the stack; " + "this should not happen");
70        }
71
72        return trace[i + 2];
73    }
74
75    static final public void report(String msg, Throwable t) {
76        System.err.println(msg);
77        System.err.println("Reported exception:");
78        t.printStackTrace();
79    }
80
81    static final public void report(String msg) {
82        System.err.println("SLF4J: " + msg);
83    }
84}
85