[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Feature request: A way to get a traceback to a buffer or a string
From: |
Lennart Borgman (gmail) |
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 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071031 Thunderbird/2.0.0.9 Mnenhy/0.7.5.666 |
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)))))))