public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Add caller() and callers() methods to rsl Log.
@ 2008-02-12 18:58 mark
  0 siblings, 0 replies; only message in thread
From: mark @ 2008-02-12 18:58 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  34a1f7390e8fbdac0f50d42b6ac21829be6ef993 (commit)
      from  aa9311fc8b209c13570a0e8688d1b10ca95a2efa (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit 34a1f7390e8fbdac0f50d42b6ac21829be6ef993
Author: Mark Wielaard <mwielaard@redhat.com>
Date:   Tue Feb 12 14:18:08 2008 +0100

    Add caller() and callers() methods to rsl Log.
    
    frysk-sys/frysk/rsl/ChangeLog
    2008-02-12  Mark Wielaard  <mwielaard@redhat.com>
    
            * Log.java (caller): New public method.
            (empty): New private empty String[].
            (callersArray): New private method.
            (callers): New public method.
            (callers(int)): New public method.

-----------------------------------------------------------------------

Summary of changes:
 frysk-sys/frysk/rsl/ChangeLog |    8 +++++
 frysk-sys/frysk/rsl/Log.java  |   71 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 0 deletions(-)

First 500 lines of diff:
diff --git a/frysk-sys/frysk/rsl/ChangeLog b/frysk-sys/frysk/rsl/ChangeLog
index 204f238..f916669 100644
--- a/frysk-sys/frysk/rsl/ChangeLog
+++ b/frysk-sys/frysk/rsl/ChangeLog
@@ -1,3 +1,11 @@
+2008-02-12  Mark Wielaard  <mwielaard@redhat.com>
+
+	* Log.java (caller): New public method.
+	(empty): New private empty String[].
+	(callersArray): New private method.
+	(callers): New public method.
+	(callers(int)): New public method.
+
 2008-02-12  Andrew Cagney  <cagney@redhat.com>
 
 	* Log.java: Add more log methods.
diff --git a/frysk-sys/frysk/rsl/Log.java b/frysk-sys/frysk/rsl/Log.java
index dd0bdf4..8cd165c 100644
--- a/frysk-sys/frysk/rsl/Log.java
+++ b/frysk-sys/frysk/rsl/Log.java
@@ -458,4 +458,75 @@ public final class Log {
     // 11 parameters
     public void log(Object self, String p1, Object p2, String p3, long p4, String p5, Object p6, String p7, int p8, String p9, int p10, String p11) {
     }
+
+  /**
+   * Convenience method to get the caller of a method in which you
+   * use the Log object. Returns the caller (of the caller) of this
+   * method as String or "<unknown>" if caller cannot be found or if
+   * logger isn't logging. Use as:
+   * <code>log.log(this, "method called by ", log.caller());</code>.
+   */
+  public String caller()
+  {
+    if (logging)
+      {
+	Throwable t = new Throwable();
+	StackTraceElement[] stackTrace = t.getStackTrace();
+	if (stackTrace.length > 2)
+	  return stackTrace[2].toString();
+      }
+
+    return "<unknown>";
+  }
+
+  // Empty caller array for use in callersArray.
+  static private final String[] empty = new String[0];
+
+  // Private method that should only be directly called from
+  // callers() or callers(int), which in turn should only be called
+  // directly from the method that uses the Log and wants to find
+  // its callers. Depends on actual caller being of depth 3.
+  private String[] callersArray(int max)
+  {
+    if (logging)
+      {
+        Throwable t = new Throwable();
+        StackTraceElement[] stackTrace = t.getStackTrace();
+	int length = stackTrace.length > 3 ? stackTrace.length - 3 : 0;
+	if (length > max)
+	  length = max;
+	String[] callers = new String[length];
+        while (length > 0)
+	  {
+	    callers[length - 1]
+	      = stackTrace[length + 2].toString();
+	    length--;
+	  }
+	return callers;
+      }
+
+    return empty;
+  }
+
+  /**
+   * Convenience method to get an array of callers of a method in
+   * which you use the Log object. Returns the callers (of the caller)
+   * of this method as a String[] or an empty array if the callers
+   * cannot be found or if logger isn't logging. Use as:
+   * <code>log.log(this, "method called by ", log.callers());</code>.
+   * This is pretty heavyweight when the Log is enabled, so use
+   * sparingly.
+   */
+  public String[] callers()
+  {
+    return callersArray(Integer.MAX_VALUE);
+  }
+
+  /**
+   * Same as callers() but only returns at most max callers.
+   */
+  public String[] callers(int max)
+  {
+    return callersArray(max);
+  }
 }


hooks/post-receive
--
frysk system monitor/debugger


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-02-12 18:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-12 18:58 [SCM] master: Add caller() and callers() methods to rsl Log mark

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).