all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Lennart Borgman (gmail)" <lennart.borgman@gmail.com>
To: Emacs Devel <emacs-devel@gnu.org>
Subject: Re: Feature request: A way to get a traceback to a buffer or a string
Date: Tue, 11 Dec 2007 11:12:46 +0100	[thread overview]
Message-ID: <475E629E.4040302@gmail.com> (raw)
In-Reply-To: <475DE033.6060403@gmail.com>

Lennart Borgman (gmail) wrote:
> In some situations you may know that a function call will cause an 
> error. You may then want to get a traceback from this particular call, 
> without user intervention. I have attached a macro that can give this. I 
> would find it practical if this were included in Emacs.
> 
> (defmacro mumamo-get-backtrace (bodyform)
>   "Evaluate BODYFORM, return backtrace as a string.
> If there is an error in BODYFORM then return the backtrace as a
> string, otherwise return nil."
>   `(let ((debugger (lambda (&rest debugger-args)
>                      (setq debugger-ret (with-output-to-string 
> (backtrace)))))
>          (debug-on-error t)
>          (debug-on-signal t)
>          (debugger-ret nil))
>      (condition-case err
>          (progn
>            ,bodyform
>            nil)
>        (error
>         (let* ((errmsg (error-message-string err))
>                (debugger-lines (split-string debugger-ret "\n"))
>                (dbg-ret (mapconcat 'identity (nthcdr 6 debugger-lines) 
> "\n")))
>           (concat errmsg "\n" dbg-ret))))))


The code above does not give a backtrace always, but I think this will:

(defmacro mumamo-get-backtrace-if-error (bodyform)
   "Evaluate BODYFORM, return a list with error message and backtrace.
If there is an error in BODYFORM then return a list with the
error message and the backtrace as a string. Otherwise return
nil."
   `(let* ((debugger
            (lambda (&rest debugger-args)
              (let ((debugger-ret (with-output-to-string (backtrace))))
                ;; I believe we must put the result in a buffer,
                ;; otherwise `condition-case' might erase it:
                (with-current-buffer (get-buffer-create "TEMP GET 
BACKTRACE")
                  (erase-buffer)
                  (insert debugger-ret)))))
           (debug-on-error t)
           (debug-on-signal t))
      (condition-case err
          (progn
            ,bodyform
            nil)
        (error
         (let* ((errmsg (error-message-string err))
                (dbg1-ret
                 (with-current-buffer
                     (get-buffer "TEMP GET BACKTRACE") (buffer-string)))
                ;; Remove lines from this routine:
                (debugger-lines (split-string dbg1-ret "\n"))
                (dbg-ret (mapconcat 'identity (nthcdr 6 debugger-lines) 
"\n"))
                )
           (list errmsg (concat errmsg "\n" dbg-ret)))))))

  reply	other threads:[~2007-12-11 10:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-11  0:56 Feature request: A way to get a traceback to a buffer or a string Lennart Borgman (gmail)
2007-12-11 10:12 ` Lennart Borgman (gmail) [this message]
2007-12-11 19:01 ` Richard Stallman
2007-12-12  1:24   ` Lennart Borgman (gmail)
2007-12-12 12:53     ` Johan Bockgård
2007-12-12 15:21       ` Stefan Monnier
2007-12-12 16:52         ` Lennart Borgman (gmail)
2007-12-12 22:52     ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=475E629E.4040302@gmail.com \
    --to=lennart.borgman@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.