Changeset 32271


Ignore:
Timestamp:
2018-07-13T19:36:50+12:00 (6 years ago)
Author:
ak19
Message:

Some more handy functions to print calling methods and tidied up the method that printed the entire callstack, before I start on committing the PDFPlugin restructuring stuff.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r32270 r32271  
    101101    static public void printStackTrace() {
    102102    // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
    103     //new Exception().printStackTrace();
    104     //StackTraceElement[] el = Thread.currentThread().getStackTrace();
    105     System.err.println("\n@@@@ stacktrace:\n" + Arrays.toString(Thread.currentThread().getStackTrace()) + "\n");
     103   
     104    //Thread.dumpStack(); // looks too much like an exception, though the newlines separating each function call is handy
     105    //new Exception().printStackTrace(); // outputs in the format of an exception too   
     106    //System.err.println("\n@@@@ stacktrace:\n" + Arrays.toString(Thread.currentThread().getStackTrace()) + "\n"); // outputs all in one line
     107   
     108    System.err.println("\n@@@@ stacktrace:");
     109    StackTraceElement[] els = new Throwable().getStackTrace(); // starts at index 1, which is this function
     110    //StackTraceElement[] els = Thread.currentThread().getStackTrace(); starts at index 0, "java.lang.Thread.getStackTrace()"
     111    for(StackTraceElement ste : els) {
     112        System.err.println("   " + ste);
     113    }
     114    }
     115   
     116    /**
     117     * Handy function to display the parent of the calling function
     118     * (the function that called the function that called printCaller())
     119     */
     120    static public void printCaller() {
     121    int parent = 1;
     122    // this function printCaller() itself adds another layer on the callstack since
     123    // it calls the overloaded method, so need to add 1 more to parent
     124    printCaller(parent++);
     125    }
     126   
     127    /**
     128     * Handy function to display the nth ancestor of the calling function
     129     * where ancestor=0 would be the calling function itself
     130     */
     131    static public void printCaller(int ancestor) {
     132    // https://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java
     133   
     134    // Thread.currentThread().getStackTrace() starts at index 0: "java.lang.Thread.getStackTrace()"
     135    // index 1 will be this method (printCaller) and index 2 will be the calling function itself who wants
     136    // to know who called it. So need to at least start at index 3 to get informative caller information
     137   
     138    StackTraceElement[] callstack = Thread.currentThread().getStackTrace();
     139    StackTraceElement requestor = callstack[2]; // the calling function, the function that called this one
     140    StackTraceElement caller_requested = callstack[ancestor+3]; // the function requested
     141    System.err.println("@@@ Function " + requestor +  " called by:\n    "
     142               + caller_requested + " at " + ancestor + " ancestors back");
    106143    }
    107144   
Note: See TracChangeset for help on using the changeset viewer.