all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How can function know its own name?
@ 2022-06-25 20:21 Jean Louis
  2022-06-25 21:37 ` Emanuel Berg
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Jean Louis @ 2022-06-25 20:21 UTC (permalink / raw)
  To: Help GNU Emacs


I would like to invoke logging for specific functions automatically,
without specifying what to log, and my function should know which
function invoked it.

For example

(defun my-log (&optional function-name)
  (log-to-database function-name))

(defun logged-function ()
  (my-log (find-function-name)) ;; I would like at this point to recognize that it was invoked from within logged-function?
  (ignore))

Would this be possible somehow?


Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-25 20:21 How can function know its own name? Jean Louis
@ 2022-06-25 21:37 ` Emanuel Berg
  2022-06-26  5:46 ` tomas
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Emanuel Berg @ 2022-06-25 21:37 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> I would like to invoke logging for specific functions
> automatically, without specifying what to log, and my
> function should know which function invoked it.
>
> For example
>
> (defun my-log (&optional function-name)
>   (log-to-database function-name))
>
> (defun logged-function ()
>   (my-log (find-function-name)) ;; I would like at this point
> to recognize that it was invoked from within logged-function?
>   (ignore))
>
> Would this be possible somehow?

You can do a function called for example 'log-this-function'.

You send the function you wish to log to that function, it
then does a `advice-add' :before (or :after?) the function
that's supposed to be logged and the advice function can then
have the function's name with `symbol-name' since that's
available in 'log-this-function' ...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-25 20:21 How can function know its own name? Jean Louis
  2022-06-25 21:37 ` Emanuel Berg
@ 2022-06-26  5:46 ` tomas
  2022-06-26  8:08   ` Jean Louis
  2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-26  9:43 ` Philip Kaludercic
  3 siblings, 1 reply; 13+ messages in thread
From: tomas @ 2022-06-26  5:46 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help GNU Emacs

[-- Attachment #1: Type: text/plain, Size: 1158 bytes --]

On Sat, Jun 25, 2022 at 11:21:20PM +0300, Jean Louis wrote:
> 
> I would like to invoke logging for specific functions automatically,
> without specifying what to log, and my function should know which
> function invoked it.

This is not completely trivial. In Lisp, a function has no name.
It is a first-class object which can be bound to (the function
slot) of a symbol (or to that of two, three... symbols).

It's like the value 42. Many variables can be bound to that. Or
none.

What name has (lambda (x) (if (= (mod x 2) 1) (+ (* 3 x) 1) (/ x 2)))?

None (yet?).

Try this:

  (setf (symbol-function 'foo) (lambda (x) (+ x 1)))

Now:

  (foo 13)

  => 14

So that function that adds one to its argument is now arguably
called foo. But:

  (setf (symbol-function 'bar) (symbol-function 'foo))

Then:

  (bar 14)

  => 15

...it can be called bar at the same time. Well, I can be called
two names too, can't I?

See 13.3 "Naming a function" and 9.1 "Symbol Components" in our
beloved Emacs Lisp manual for all the gory details.

Now to the interesting question: how do debuggers pull it off?

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26  5:46 ` tomas
@ 2022-06-26  8:08   ` Jean Louis
  0 siblings, 0 replies; 13+ messages in thread
From: Jean Louis @ 2022-06-26  8:08 UTC (permalink / raw)
  To: tomas; +Cc: Help GNU Emacs

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-06-26 08:46]:
> On Sat, Jun 25, 2022 at 11:21:20PM +0300, Jean Louis wrote:
> > 
> > I would like to invoke logging for specific functions automatically,
> > without specifying what to log, and my function should know which
> > function invoked it.
> 
> This is not completely trivial. In Lisp, a function has no name.
> It is a first-class object which can be bound to (the function
> slot) of a symbol (or to that of two, three... symbols).
> 
> It's like the value 42. Many variables can be bound to that. Or
> none.
> 
> What name has (lambda (x) (if (= (mod x 2) 1) (+ (* 3 x) 1) (/ x 2)))?
> 
> None (yet?).
> 
> Try this:
> 
>   (setf (symbol-function 'foo) (lambda (x) (+ x 1)))
> 
> Now:
> 
>   (foo 13)
> 
>   => 14
> 
> So that function that adds one to its argument is now arguably
> called foo. But:
> 
>   (setf (symbol-function 'bar) (symbol-function 'foo))
> 
> Then:
> 
>   (bar 14)
> 
>   => 15
> 
> ...it can be called bar at the same time. Well, I can be called
> two names too, can't I?
> 
> See 13.3 "Naming a function" and 9.1 "Symbol Components" in our
> beloved Emacs Lisp manual for all the gory details.
> 
> Now to the interesting question: how do debuggers pull it off?

OK.

Maybe one way to give function a capacity to find out its name, the
name of the called function from inside of itself could be to call it
in a wrapper which sets global variable to be symbol of the function.

(defvar rcd-called-function)

(defun rcd-call-function (function &rest args)
  (setq rcd-called-function function)
  (apply function args)
  (setq rcd-called-function nil))

(defun my-fun ()
  (message "My function name is: %s" rcd-called-function))

(rcd-call-function 'my-fun) ⇒ nil


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-25 20:21 How can function know its own name? Jean Louis
  2022-06-25 21:37 ` Emanuel Berg
  2022-06-26  5:46 ` tomas
@ 2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-26  9:25   ` tomas
  2022-06-26 10:13   ` Jean Louis
  2022-06-26  9:43 ` Philip Kaludercic
  3 siblings, 2 replies; 13+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-06-26  8:54 UTC (permalink / raw)
  To: help-gnu-emacs

> I would like to invoke logging for specific functions automatically,
> without specifying what to log, and my function should know which
> function invoked it.

`C-h o trace-function RET`


        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-06-26  9:25   ` tomas
  2022-06-26 10:13   ` Jean Louis
  1 sibling, 0 replies; 13+ messages in thread
From: tomas @ 2022-06-26  9:25 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 404 bytes --]

On Sun, Jun 26, 2022 at 04:54:36AM -0400, Stefan Monnier via Users list for the GNU Emacs text editor wrote:
> > I would like to invoke logging for specific functions automatically,
> > without specifying what to log, and my function should know which
> > function invoked it.
> 
> `C-h o trace-function RET`

But then you'd have to know already (a) function's name, no?

;-)

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-25 20:21 How can function know its own name? Jean Louis
                   ` (2 preceding siblings ...)
  2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-06-26  9:43 ` Philip Kaludercic
  2022-06-26 10:21   ` Jean Louis
  2022-06-26 10:31   ` Jean Louis
  3 siblings, 2 replies; 13+ messages in thread
From: Philip Kaludercic @ 2022-06-26  9:43 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help GNU Emacs

Jean Louis <bugs@gnu.support> writes:

> I would like to invoke logging for specific functions automatically,
> without specifying what to log, and my function should know which
> function invoked it.
>
> For example
>
> (defun my-log (&optional function-name)
>   (log-to-database function-name))
>
> (defun logged-function ()
>   (my-log (find-function-name)) ;; I would like at this point to recognize that it was invoked from within logged-function?
>   (ignore))
>
> Would this be possible somehow?
>
>
> Jean
>
> Take action in Free Software Foundation campaigns:
> https://www.fsf.org/campaigns
>
> In support of Richard M. Stallman
> https://stallmansupport.org/

This might do the job: https://emacs.stackexchange.com/questions/2310/can-functions-access-their-name/2312#2312



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-26  9:25   ` tomas
@ 2022-06-26 10:13   ` Jean Louis
  2022-06-26 21:25     ` Stefan Monnier
  1 sibling, 1 reply; 13+ messages in thread
From: Jean Louis @ 2022-06-26 10:13 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

* Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2022-06-26 11:56]:
> > I would like to invoke logging for specific functions automatically,
> > without specifying what to log, and my function should know which
> > function invoked it.
> 
> `C-h o trace-function RET`

Interactively, that gives information.

Though within function I would not be able to automaticaly read which
function was invoked. If that is what you mean.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26  9:43 ` Philip Kaludercic
@ 2022-06-26 10:21   ` Jean Louis
  2022-06-26 10:31   ` Jean Louis
  1 sibling, 0 replies; 13+ messages in thread
From: Jean Louis @ 2022-06-26 10:21 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: Help GNU Emacs

> Jean Louis <bugs@gnu.support> writes:
> 
> > I would like to invoke logging for specific functions automatically,
> > without specifying what to log, and my function should know which
> > function invoked it.

* Philip Kaludercic <philipk@posteo.net> [2022-06-26 12:44]:
> This might do the job: https://emacs.stackexchange.com/questions/2310/can-functions-access-their-name/2312#2312

That gives me solution in sight, thank you!

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26  9:43 ` Philip Kaludercic
  2022-06-26 10:21   ` Jean Louis
@ 2022-06-26 10:31   ` Jean Louis
  2022-06-26 21:13     ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 13+ messages in thread
From: Jean Louis @ 2022-06-26 10:31 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: Help GNU Emacs

* Philip Kaludercic <philipk@posteo.net> [2022-06-26 12:45]:
> This might do the job: https://emacs.stackexchange.com/questions/2310/can-functions-access-their-name/2312#2312

This way it somehow starts working. Condition is that I have to call
`my-function-name' in the first `let' as otherwise the index would
change.

It is very useful however on my side. I can also get arguments to
function. I will use that for refresh and logging. 

(defun my-function-name ()
  (cadr (backtrace-frame 5)))

(defun my-aware-function ()
  (let ((my-name (my-function-name)))
    my-name))

(my-aware-function) ⇒ my-aware-function


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26 10:31   ` Jean Louis
@ 2022-06-26 21:13     ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-06-26 21:13 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun my-function-name ()
>   (cadr (backtrace-frame 5)))

The number will need to be different if your code is byte-compiled, or
instrumented for Edebug, or if your function is advised (e.g. which
includes the use of `elp-profile`, `debug-on-entry`, `trace-function`).


        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26 10:13   ` Jean Louis
@ 2022-06-26 21:25     ` Stefan Monnier
  2022-06-27  2:35       ` Jean Louis
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2022-06-26 21:25 UTC (permalink / raw)
  To: help-gnu-emacs

> Though within function I would not be able to automaticaly read which
> function was invoked. If that is what you mean.

trace-function does a kind of logging like the one you're trying
to implement.


        Stefan




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How can function know its own name?
  2022-06-26 21:25     ` Stefan Monnier
@ 2022-06-27  2:35       ` Jean Louis
  0 siblings, 0 replies; 13+ messages in thread
From: Jean Louis @ 2022-06-27  2:35 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

* Stefan Monnier <monnier@iro.umontreal.ca> [2022-06-27 00:27]:
> > Though within function I would not be able to automaticaly read which
> > function was invoked. If that is what you mean.
> 
> trace-function does a kind of logging like the one you're trying
> to implement.

Thank you.

I track some functions when were they invoked for statistics
purposes. 

                             ID   19552
                   Date created   "2022-06-26 11:34:49.214956"
                  Date modified   nil
                   User created   "maddox"
                  User modified   "maddox"
                        Account   nil
                        Contact   nil
                       Business   nil
                    Assigned to   nil
                      Time zone   nil
                           Date   "2022-06-26 11:34:49.214956"
                           Time   nil
                          Title   "Function `rcd-db-edit-entry'"
                    Description   "Table: activities Column: activities_name ID: 5"
                        Publish   nil
                  Hyperdocument   nil
                            Key   nil
                       Log type   "DB Column"


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-06-27  2:35 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-25 20:21 How can function know its own name? Jean Louis
2022-06-25 21:37 ` Emanuel Berg
2022-06-26  5:46 ` tomas
2022-06-26  8:08   ` Jean Louis
2022-06-26  8:54 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-06-26  9:25   ` tomas
2022-06-26 10:13   ` Jean Louis
2022-06-26 21:25     ` Stefan Monnier
2022-06-27  2:35       ` Jean Louis
2022-06-26  9:43 ` Philip Kaludercic
2022-06-26 10:21   ` Jean Louis
2022-06-26 10:31   ` Jean Louis
2022-06-26 21:13     ` Stefan Monnier via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.