From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8856 invoked by alias); 12 Feb 2008 13:29:36 -0000 Received: (qmail 8849 invoked by uid 22791); 12 Feb 2008 13:29:35 -0000 X-Spam-Status: No, hits=-1.5 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_34,J_CHICKENPOX_73 X-Spam-Check-By: sourceware.org Received: from wildebeest.demon.nl (HELO gnu.wildebeest.org) (83.160.170.119) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 12 Feb 2008 13:29:18 +0000 Received: from dijkstra.wildebeest.org ([192.168.1.29]) by gnu.wildebeest.org with esmtp (Exim 4.63) (envelope-from ) id 1JOvCF-0004Yt-0Y; Tue, 12 Feb 2008 14:29:15 +0100 Subject: Re: a simpler logger [?] From: Mark Wielaard To: Phil Muldoon Cc: frysk In-Reply-To: <47B18FA6.3090507@redhat.com> References: <475976DF.6070302@redhat.com> <4766D650.6080102@redhat.com> <47B09909.4070707@redhat.com> <1202818497.3378.8.camel@dijkstra.wildebeest.org> <47B18FA6.3090507@redhat.com> Content-Type: multipart/mixed; boundary="=-we3QVYkrBCRGXB8gBnPs" Date: Tue, 12 Feb 2008 13:29:00 -0000 Message-Id: <1202822954.3378.14.camel@dijkstra.wildebeest.org> Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 (2.12.3-1.fc8) X-Spam-Score: -4.4 (----) X-Virus-Checked: Checked by ClamAV on sourceware.org X-IsSubscribed: yes Mailing-List: contact frysk-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: frysk-owner@sourceware.org X-SW-Source: 2008-q1/txt/msg00062.txt.bz2 --=-we3QVYkrBCRGXB8gBnPs Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-length: 444 Hi Phil, On Tue, 2008-02-12 at 12:23 +0000, Phil Muldoon wrote: > As fine.caller() is an optional call (you have to supply it in the > arguments), the overhead of the Throwable can be managed by the > programmer. Personally I find it immensely useful to detect the origin > of a call. I would like to see it the Log code. Thanks. Attached is what I will most likely push, unless you find any troubles while testing with it. Cheers, Mark --=-we3QVYkrBCRGXB8gBnPs Content-Disposition: inline; filename=Log-caller.patch Content-Type: text/x-patch; name=Log-caller.patch; charset=utf-8 Content-Transfer-Encoding: 7bit Content-length: 3535 commit d9c5abf617a6157c7df7db340f8f9fdb9b42e538 Author: Mark Wielaard 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 * Log.java (caller): New public method. (empty): New private empty String[]. (callersArray): New private method. (callers): New public method. (callers(int)): New public method. diff --git a/frysk-sys/frysk/rsl/ChangeLog b/frysk-sys/frysk/rsl/ChangeLog index a2997cf..2b84755 100644 --- a/frysk-sys/frysk/rsl/ChangeLog +++ b/frysk-sys/frysk/rsl/ChangeLog @@ -1,3 +1,11 @@ +2008-02-12 Mark Wielaard + + * 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-11 Andrew Cagney * Log.java (prefixTime()): Print time in DAY HH:MM:SS.mmm format; diff --git a/frysk-sys/frysk/rsl/Log.java b/frysk-sys/frysk/rsl/Log.java index 1ddfa73..e77aaed 100644 --- a/frysk-sys/frysk/rsl/Log.java +++ b/frysk-sys/frysk/rsl/Log.java @@ -420,4 +420,75 @@ public final class Log { return; prefix(self); print(p1); print(p2); print(p3); print(p4); print(p5); print(p6); 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 "" if caller cannot be found or if + * logger isn't logging. Use as: + * log.log(this, "method called by ", log.caller());. + */ + public String caller() + { + if (logging) + { + Throwable t = new Throwable(); + StackTraceElement[] stackTrace = t.getStackTrace(); + if (stackTrace.length > 2) + return stackTrace[2].toString(); + } + + return ""; + } + + // 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: + * log.log(this, "method called by ", log.callers());. + * 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); + } } --=-we3QVYkrBCRGXB8gBnPs--