I'd like to include the function name in some debug messages.
Is there a portable way to access this info? Or do I have to use a
platform-specific technique?
jmckitrick wrote:
> I'd like to include the function name in some debug messages.
CL-USER> (defmacro my-defun(name args &body body)
`(defun ,name ,args
(let ((current-function-name ',name))
,@body)))
MY-DEFUN
CL-USER> (my-defun test(x y)
(pprint current-function-name)
(+ x y))
TEST
CL-USER> (test 1 2)
TEST
3
On Wed, 23 Aug 2006 07:14:59 -0700, Alexander wrote:
> jmckitrick wrote:
>> I'd like to include the function name in some debug messages.
Quick and dirty: http://paste.lisp.org/display/24614 and
http://paste.lisp.org/display/24614#1
..pasted here also:
(defpackage :my-cl
(:use :cl)
(:shadow :defun))
(in-package :my-cl)
(defmacro defun (name args &body body)
`(cl:defun ,name ,args
(let ((current-function-name ',name))
,@body)))
(export 'defun)
#|
my-cl> (defun some-function (a b)
(format t "currently executing ~A~%" current-function-name)
(+ a b))
some-function
my-cl> (some-function 3 2)
currently executing some-function
5
|#
(defmacro defun2 (name args &body body)
`(cl:defun ,name ,args
(let ((current-function-name ',name)
(current-function-args ',args))
,@body)))
(export 'defun2)
#|
my-cl> (defun2 some-other-function (a b)
(format t "currently executing ~A, with parameters ~A~%"
current-function-name current-function-args)
(+ a b))
some-other-function
my-cl> (some-other-function 3 4)
currently executing some-other-function, with parameters (a b)
7
|#
;; Stack-thing where first is current.
(defvar *current-function-names* nil) (export '*current-function-names*)
(defvar *current-function-args* nil) (export '*current-function-args*)
(defmacro defun3 (name args &body body)
`(cl:defun ,name ,args
(unwind-protect
(progn
(push ',name *current-function-names*)
(push ',args *current-function-args*)
,@body)
(pop *current-function-names*)
(pop *current-function-args*))))
(export 'defun3)
#|
my-cl> (defun3 inner (a b)
(format t "current function: ~A, current parameters: ~A~%"
(first *current-function-names*)
(first *current-function-args*))
(format t "functions: ~A~%" *current-function-names*)
(* a b))
inner
my-cl> (defun3 outher (a b)
(format t "current function: ~A, current parameters: ~A~%"
(first *current-function-names*)
(first *current-function-args*))
(format t "functions: ~A~%" *current-function-names*)
(prog1
(inner a b)
(format t "current function: ~A, current parameters: ~A~%"
(first *current-function-names*)
(first *current-function-args*))
(format t "functions: ~A~%" *current-function-names*)))
outher
my-cl> (outher 5 2)
current function: outher, current parameters: (a b)
functions: (outher)
current function: inner, current parameters: (a b)
functions: (inner outher)
current function: outher, current parameters: (a b)
functions: (outher)
10
|#
--
Lars Rune Nøstdal
http://lars.nostdal.org/
From: Pascal Bourguignon
Subject: Re: How to access current function/method name at runtime?
Date:
Message-ID: <87zmdv35db.fsf@informatimago.com>
"jmckitrick" <···········@yahoo.com> writes:
> I'd like to include the function name in some debug messages.
>
> Is there a portable way to access this info? Or do I have to use a
> platform-specific technique?
First, note that there are a lot of _anonymous_ functions!
(asdf:oos 'asdf:load-op :com.informatimago.common-lisp)
(import '(com.informatimago.common-lisp.source:extract-documentation
com.informatimago.common-lisp.source:extract-declarations
com.informatimago.common-lisp.source:extract-body))
;; http://www.informatimago.com/develop/lisp/index.html#common-lisp
(shadow 'defun)
(defmacro defun (name arguments &body doc-decl-and-body)
(let ((doc (extract-documentation doc-decl-and-body))
(decl (extract-declarations doc-decl-and-body))
(body (extract-body doc-decl-and-body)))
`(cl:defun ,name ,arguments
,@(when doc (list doc))
,@decl
(let ((%debug-funame% ',name))
,@body))))
(defvar *%debug-level%* 0)
(defmacro debshow (&rest exprs)
`(let ((*%debug-level%* (+ 4 *%debug-level%*)))
(format *trace-output* "~&~VAIn function ~A~%"
(- *%debug-level%* 2) "" %debug-funame%)
,@(mapcar (lambda (expr)
`(format *trace-output* "~VA~S = ~S~%"
*%debug-level%* "" ',expr ,expr))
exprs)
(finish-output *trace-output*)))
(defun test-1 (x) (debshow x (* 2 x)) (* 2 x))
(defun test-2 (x) (debshow x (test-1 x)) (1+ (test-1 x)))
(test-2 3)
In function TEST-2
X = 3
In function TEST-1
X = 3
(* 2 X) = 6
(TEST-1 X) = 6
In function TEST-1
X = 3
(* 2 X) = 6
7