public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Refactor Log to Callers.
@ 2008-02-13  0:05 cagney
  0 siblings, 0 replies; only message in thread
From: cagney @ 2008-02-13  0:05 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  f0e1d0ce0f3e015de79f183f90533865aad6fc7c (commit)
      from  3d895232d71bbdf785a82ea0a0b7d43b6b0f77d6 (commit)

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

- Log -----------------------------------------------------------------
commit f0e1d0ce0f3e015de79f183f90533865aad6fc7c
Author: Andrew Cagney <cagney@redhat.com>
Date:   Tue Feb 12 18:59:00 2008 -0500

    Refactor Log to Callers.
    
    frysk-core/frysk/proc/dead/ChangeLog
    2008-02-12  Andrew Cagney  <cagney@redhat.com>
    
    	* LinuxCoreProc.java: Use Log.CALLER.
    	* LinuxCoreTask.java: Ditto.
    
    frysk-sys/frysk/rsl/ChangeLog
    2008-02-12  Andrew Cagney  <cagney@redhat.com>
    
    	* Node.java (set(Level)): Return the node.
    	* Callers.java: Extract from Log.java.
    	* TestCallers.java: New.
    	* Log.java (CALLER): New.
    	(CALLERS): New.
    	(dump(Object)): Check for null.
    	(set(PrintWriter)): New.
    	(set(PrintStream)): Return old writer.

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

Summary of changes:
 frysk-core/frysk/proc/dead/ChangeLog               |    5 +
 frysk-core/frysk/proc/dead/LinuxCoreProc.java      |    4 +-
 frysk-core/frysk/proc/dead/LinuxCoreTask.java      |    2 +-
 frysk-sys/frysk/rsl/{Setting.java => Callers.java} |   88 ++++++++++++-------
 frysk-sys/frysk/rsl/ChangeLog                      |    9 ++
 frysk-sys/frysk/rsl/Log.java                       |   95 +++++---------------
 frysk-sys/frysk/rsl/Node.java                      |    3 +-
 .../TestRegisterSet.java => rsl/TestCallers.java}  |   65 +++++++------
 8 files changed, 134 insertions(+), 137 deletions(-)
 copy frysk-sys/frysk/rsl/{Setting.java => Callers.java} (58%)
 copy frysk-sys/frysk/{sys/ptrace/TestRegisterSet.java => rsl/TestCallers.java} (59%)

First 500 lines of diff:
diff --git a/frysk-core/frysk/proc/dead/ChangeLog b/frysk-core/frysk/proc/dead/ChangeLog
index 9f8091a..d58e759 100644
--- a/frysk-core/frysk/proc/dead/ChangeLog
+++ b/frysk-core/frysk/proc/dead/ChangeLog
@@ -1,3 +1,8 @@
+2008-02-12  Andrew Cagney  <cagney@redhat.com>
+
+	* LinuxCoreProc.java: Use Log.CALLER.
+	* LinuxCoreTask.java: Ditto.
+
 2008-02-08  Andrew Cagney  <cagney@redhat.com>
 
 	* LinuxCoreHost.java (getName()): Implement.
diff --git a/frysk-core/frysk/proc/dead/LinuxCoreProc.java b/frysk-core/frysk/proc/dead/LinuxCoreProc.java
index 09f0b44..5ea4396 100644
--- a/frysk-core/frysk/proc/dead/LinuxCoreProc.java
+++ b/frysk-core/frysk/proc/dead/LinuxCoreProc.java
@@ -157,7 +157,7 @@ public class LinuxCoreProc extends DeadProc {
 		throw new RuntimeException(e);
 	    }
 
-	fine.log(this,"getMemory() caller: ", fine.caller());
+	fine.log(this, "getMemory() caller: ", Log.CALLER);
 	return memory;
     }
 
@@ -271,7 +271,7 @@ public class LinuxCoreProc extends DeadProc {
     private Auxv[] auxv;
     public Auxv[] getAuxv() {
 
-	fine.log(this,"getAuxv()",fine.caller());
+	fine.log(this,"getAuxv()", Log.CALLER);
 
 	if (auxv == null) {
 	    fine.log(this,"Auxv is null, building");
diff --git a/frysk-core/frysk/proc/dead/LinuxCoreTask.java b/frysk-core/frysk/proc/dead/LinuxCoreTask.java
index 98d399a..590bcc2 100644
--- a/frysk-core/frysk/proc/dead/LinuxCoreTask.java
+++ b/frysk-core/frysk/proc/dead/LinuxCoreTask.java
@@ -64,7 +64,7 @@ public class LinuxCoreTask extends DeadTask {
 	// ISAs. In an attempt to save system resources, get a
 	// reference to the proc's maps for now.
 
-	fine.log(this,"getMemory() called by ",fine.caller());
+	fine.log(this,"getMemory() called by ",Log.CALLER);
 	return parent.getMemory();
     }
 
diff --git a/frysk-sys/frysk/rsl/Setting.java b/frysk-sys/frysk/rsl/Callers.java
similarity index 58%
copy from frysk-sys/frysk/rsl/Setting.java
copy to frysk-sys/frysk/rsl/Callers.java
index b2ca236..5c1d84e 100644
--- a/frysk-sys/frysk/rsl/Setting.java
+++ b/frysk-sys/frysk/rsl/Callers.java
@@ -39,47 +39,73 @@
 
 package frysk.rsl;
 
+import java.util.LinkedList;
 
 /**
- * The level the node is set at.
+ * Class for constructing a backtrace string.
  */
-class Setting {
+public final class Callers {
 
-    static final Setting EPOCH = new Setting(Level.NONE);
+    private int start;
+    private int stop;
 
-    // A private variable to track a settings age.  Each time a new
-    // setting is created the age is incremented; the one with the
-    // largest age (most recent) wins.
-    private static int currentAge;
-
-    private int age;
-    private Level level;
-
-    Setting(Level level) {
-	this.age = currentAge++;
-	this.level = level;
+    Callers(int start, int stop) {
+	this.start = start;
+	this.stop = stop;
     }
 
     public String toString() {
-	return level.toPrint() + "@" + age;
-    }
-
-    Level level() {
-	return level;
+	Throwable t = new Throwable();
+	StackTraceElement[] stackTrace = t.getStackTrace();
+	if (stackTrace.length > start) {
+	    if (start == stop) {
+		return stackTrace[start].toString();
+	    } else {
+		LinkedList l = new LinkedList();
+		int i;
+		for (i = start; i < stop && i < stackTrace.length; i++) {
+		    l.add(stackTrace[i].toString());
+		}
+		if (i < stackTrace.length)
+		    l.add("...");
+		return l.toString();
+	    }
+	}
+	return "<unknown>";
     }
 
-    boolean isNewer(Setting setting) {
-	return this.age > setting.age;
-    }
-
-    boolean isLarger(Level level) {
-	return this.level.compareTo(level) > 0;
-    }
+    // Empty caller array for use in callers.
+    private static final String[] unknown = new String[] { "<unknown>" };
 
-    Level level(Setting setting) {
-	if (this.age > setting.age)
-	    return this.level;
-	else
-	    return setting.level;
+    /**
+     * Return the N callers as an array.
+     */
+    public static String[] callers(Log logger, int max) {
+	if (!logger.logging())
+	    return unknown;
+	int chop = 1;
+	Throwable t = new Throwable();
+	StackTraceElement[] stackTrace = t.getStackTrace();
+	if (stackTrace.length <= chop)
+	    // something screwed up
+	    return unknown;
+	if (max <= 1)
+	    // can't backtrace nothing
+	    return unknown;
+	int length = stackTrace.length - chop;
+	String[] callers;
+	// If trimming, leave space for "..."
+	if (length > max) {
+	    callers = new String[max];
+	    length = max - 1;
+	    callers[length] = "...";
+	} else {
+	    callers = new String[length];
+	}
+	for (int i = 0; i < length; i++) {
+	    callers[i]
+		= stackTrace[i + chop].toString();
+	}
+	return callers;
     }
 }
diff --git a/frysk-sys/frysk/rsl/ChangeLog b/frysk-sys/frysk/rsl/ChangeLog
index 810a3aa..cfce596 100644
--- a/frysk-sys/frysk/rsl/ChangeLog
+++ b/frysk-sys/frysk/rsl/ChangeLog
@@ -1,5 +1,14 @@
 2008-02-12  Andrew Cagney  <cagney@redhat.com>
 
+	* Node.java (set(Level)): Return the node.
+	* Callers.java: Extract from Log.java.
+	* TestCallers.java: New.
+	* Log.java (CALLER): New.
+	(CALLERS): New.
+	(dump(Object)): Check for null.
+	(set(PrintWriter)): New.
+	(set(PrintStream)): Return old writer.
+	
 	* Log.java: Add more loggers.
 
 2008-02-12  Mark Wielaard  <mwielaard@redhat.com>
diff --git a/frysk-sys/frysk/rsl/Log.java b/frysk-sys/frysk/rsl/Log.java
index 4d13663..be46a3b 100644
--- a/frysk-sys/frysk/rsl/Log.java
+++ b/frysk-sys/frysk/rsl/Log.java
@@ -116,8 +116,15 @@ public final class Log {
 
     // Static?
     private static PrintWriter out = new PrintWriter(System.out);
-    static void set(PrintStream out) {
+    static PrintWriter set(PrintStream out) {
+	PrintWriter old = Log.out;
 	Log.out = new PrintWriter(out);
+	return old;
+    }
+    static PrintWriter set(PrintWriter out) {
+	PrintWriter old = Log.out;
+	Log.out = out;
+	return old;
     }
 
     private static final long startTime = System.currentTimeMillis();
@@ -202,7 +209,9 @@ public final class Log {
      * @param o the object to dump
      */
     private void dump(Object o) {
-	if (o instanceof char[]) {
+	if (o == null) {
+	    out.print("<<null>>");
+	} if (o instanceof char[]) {
 	    dump((char[])o);
 	} else if (o instanceof int[]) {
 	    dump((int[])o);
@@ -490,74 +499,16 @@ public final class Log {
 	prefix(self); print(p1); print(p2); print(p3); print(p4); print(p5); print(p6); print(p7); print(p8); print(p9); print(p10); print(p11); suffix();
     }
 
-  /**
-   * 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);
-  }
+
+    /**
+     * Assuming the use: <tt>log("caller", log.CALLER)</tt> prints the
+     * caller of the logging fuction.
+     */
+    public static final Callers CALLER = new Callers(4, 4);
+
+    /**
+     * Assuming the use: <tt>log("caller", log.CALLER)</tt> a list of
+     * callers.
+     */
+    public static final Callers CALLERS = new Callers(4, Integer.MAX_VALUE);
 }
diff --git a/frysk-sys/frysk/rsl/Node.java b/frysk-sys/frysk/rsl/Node.java
index 0c8145f..81b9d4f 100644
--- a/frysk-sys/frysk/rsl/Node.java
+++ b/frysk-sys/frysk/rsl/Node.java
@@ -91,13 +91,14 @@ public final class Node {
     /**
      * Set this Node, all child nodes, and extensions, to level.
      */
-    public void set(Level level) {
+    public Node set(Level level) {
 	synchronized (LogFactory.root) {
 	    Setting newSetting = new Setting(level);
 	    setChildren(newSetting);
 	    setExtensions(newSetting);
 	    setLoggers(level);
 	}
+	return this;
     }
     private void setLoggers(Level level) {
 	for (int i = 0; i < Level.MAX.intValue(); i++) {
diff --git a/frysk-sys/frysk/sys/ptrace/TestRegisterSet.java b/frysk-sys/frysk/rsl/TestCallers.java
similarity index 59%
copy from frysk-sys/frysk/sys/ptrace/TestRegisterSet.java
copy to frysk-sys/frysk/rsl/TestCallers.java
index 4d6b3d4..a009cae 100644
--- a/frysk-sys/frysk/sys/ptrace/TestRegisterSet.java
+++ b/frysk-sys/frysk/rsl/TestCallers.java
@@ -37,46 +37,51 @@
 // version and license this file solely under the GPL without
 // exception.
 
-package frysk.sys.ptrace;
+package frysk.rsl;
 
-import frysk.sys.ProcessIdentifier;
-import frysk.junit.TestCase;
-import frysk.testbed.TearDownProcess;
-import frysk.testbed.ForkFactory;
+import inua.util.PrintWriter;
+import java.io.StringWriter;
 
 /**
- * Trace a process.
+ * Class for constructing a backtrace string.
  */
-
-public class TestRegisterSet extends TestCase {
-    /**
-     * Rip down everything related to PID.
-     */
+public class TestCallers extends TestLib {
+    private PrintWriter oldPrintWriter;
     public void tearDown() {
-	TearDownProcess.tearDown ();
+	if (oldPrintWriter != null) {
+	    Log.set(oldPrintWriter);
+	    oldPrintWriter = null;
+	}
     }
 
-    private void verifyTransfer(String what, RegisterSet regs) {
-	if (unsupported(what, regs == null))
-	    return;
-	ProcessIdentifier pid = ForkFactory.attachedDaemon();
-	// Read, write, read.
-	byte[] bytes = new byte[regs.length()];
-	regs.transfer(pid, bytes, false); // read
-	byte old = bytes[0];
-	bytes[0] = (byte) ~old;
-	regs.transfer(pid, bytes, true); // write
-	regs.transfer(pid, bytes, false); // read
-	assertEquals("modified", (byte)~old, bytes[0]);
+    public void testCALLER() {
+	StringWriter buf = new StringWriter();
+	// XXX: Restored during tearDown
+	oldPrintWriter = Log.set(new PrintWriter(buf));
+	Log log = get("").set(Level.FINE).get(Level.FINE);
+	log.log("caller", Log.CALLER);
+	assertTrue("log", buf.toString().contains(" caller "));
+	assertTrue("log", buf.toString().contains("testCALLER"));
     }
 
-    public void testREGS() {
-	verifyTransfer("REGS", RegisterSet.REGS);
+    public void testNonLoggedCallers() {
+	String[] callers = Callers.callers(get("", Level.FINE), 2);
+	assertEquals("callers.length", 1, callers.length);
+	assertEquals("callers[0]", "<unknown>", callers[0]);
     }
-    public void testFPREGS() {
-	verifyTransfer("FPREGS", RegisterSet.FPREGS);
+
+    public void testLoggedCallers() {
+	Log log = get("").set(Level.FINE).get(Level.FINE);
+	String[] callers = Callers.callers(log, 2);
+	assertEquals("callers.length", 2, callers.length);
+	assertTrue("callers[0]", callers[0].contains("testLoggedCallers"));
+	assertEquals("callers[1]", "...", callers[1]);
     }
-    public void testFPXREGS() {
-	verifyTransfer("FPXREGS", RegisterSet.FPXREGS);
+
+    public void testLoggedAllCallers() {
+	Log log = get("").set(Level.FINE).get(Level.FINE);
+	String[] callers = Callers.callers(log, Integer.MAX_VALUE);
+	assertTrue("callers[0]", callers[0].contains("testLoggedAllCallers"));
+	assertTrue("callers[N]", !callers[callers.length - 1].equals("..."));
     }
 }


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


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

only message in thread, other threads:[~2008-02-13  0:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-13  0:05 [SCM] master: Refactor Log to Callers cagney

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).