Changeset 32608 for main


Ignore:
Timestamp:
2018-11-16T14:24:39+13:00 (5 years ago)
Author:
ak19
Message:

Copying the methods printing out the caller (callstack) information from gli's Utility.java into core's Misc.java.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/util/Misc.java

    r31573 r32608  
    6666    } 
    6767    }
     68
     69   
     70    /**
     71     * Handy function to display the list of calling functions by
     72     * printing out the stack trace even when you don't have an exception
     73     */
     74    static public void printStackTrace() {
     75    // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
     76   
     77    //Thread.dumpStack(); // looks too much like an exception, though the newlines separating each function call is handy
     78    //new Exception().printStackTrace(); // outputs in the format of an exception too   
     79    //System.err.println("\n@@@@ stacktrace:\n" + Arrays.toString(Thread.currentThread().getStackTrace()) + "\n"); // outputs all in one line
     80   
     81    System.err.println("\n@@@@ stacktrace:");
     82    StackTraceElement[] els = new Throwable().getStackTrace(); // starts at index 1, which is this function
     83    //StackTraceElement[] els = Thread.currentThread().getStackTrace(); starts at index 0, "java.lang.Thread.getStackTrace()"
     84    for(StackTraceElement ste : els) {
     85        System.err.println("   " + ste);
     86    }
     87    }
     88   
     89    /**
     90     * Handy function to display the parent of the calling function
     91     * (the function that called the function that called printCaller())
     92     */
     93    static public void printCaller() {
     94    int parent = 1;
     95    // this function printCaller() itself adds another layer on the callstack since
     96    // it calls the overloaded method, so need to add 1 more to parent
     97    printCaller(parent++);
     98    }
     99   
     100    /**
     101     * Handy function to display the nth ancestor of the calling function
     102     * where ancestor=0 would be the calling function itself
     103     */
     104    static public void printCaller(int ancestor) {
     105    // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
     106   
     107    // Thread.currentThread().getStackTrace() starts at index 0: "java.lang.Thread.getStackTrace()"
     108    // index 1 will be this method (printCaller) and index 2 will be the calling function itself who wants
     109    // to know who called it. So need to at least start at index 3 to get informative caller information
     110   
     111    StackTraceElement[] callstack = Thread.currentThread().getStackTrace();
     112    StackTraceElement requestor = callstack[2]; // the calling function, the function that called this one
     113    StackTraceElement caller_requested = callstack[ancestor+3]; // the function requested
     114    System.err.println("@@@ Function " + requestor +  " called by:\n    "
     115               + caller_requested + " at " + ancestor + " ancestors back");
     116    }   
     117   
    68118   
    69119    /** Method to determine if the host system is Windows based.
Note: See TracChangeset for help on using the changeset viewer.