* riece.el (riece-buffer-alist): Init riece-debug-buffer.
[riece] / lisp / riece-debug.el
1 ;;; riece-debug.el --- debug support
2 ;; Copyright (C) 1998-2005 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-globals)
28 (require 'riece-options)
29
30 (defun riece-debug (message &optional detail)
31   "Print a one-line debug MESSAGE at the bottom of the frame.
32 If the optional 2nd argument DETAIL is specified, it is stored into
33 `riece-debug-buffer'."
34   (message "riece-debug: %s" message)
35   (save-excursion
36     (set-buffer riece-debug-buffer)
37     (goto-char (point-max))
38     (let ((point (point)))
39       (if detail
40           (insert message "\n" detail "\n")
41         (insert message "\n"))
42       (goto-char point)
43       (beginning-of-line)
44       (while (not (eobp))
45         (insert "riece-debug: ")
46         (forward-line)))))
47
48 (defun riece-debug-reset-standard-output ()
49   "Reset `riece-temp-buffer' to be used as `standard-output'."
50   (save-excursion
51     (set-buffer riece-temp-buffer)
52     (buffer-disable-undo)
53     (erase-buffer)))
54
55 (defmacro riece-debug-with-backtrace (&rest body)
56   "Execute BODY and send a backtrace to `riece-temp-buffer'."
57   `(unwind-protect
58        (progn ,@body)
59      (riece-debug-reset-standard-output)
60      (let ((standard-output riece-temp-buffer))
61        (backtrace))))
62
63 (put 'riece-debug-with-backtrace 'lisp-indent-function 0)
64 (put 'riece-debug-with-backtrace 'edebug-form-spec '(form body))
65
66 (defmacro riece-ignore-errors (location &rest body)
67   "Execute BODY; if an error occurs, return nil.
68 Otherwise, return result of last FORM.
69 If `riece-debug' is non-nil and an error occurred, it sends a
70 backtrace to standard-output."
71   `(condition-case error
72        (if riece-debug
73            (riece-debug-with-backtrace ,@body)
74          ,@body)
75      (error
76       (if riece-debug
77           (save-excursion
78             (set-buffer riece-temp-buffer)
79             (goto-char (point-min))
80             (if (re-search-forward "^  signal(" nil t)
81                 (delete-region (point-min) (match-beginning 0)))
82             (riece-debug (format "Error in `%s': %S" ,location error)
83                          (buffer-string))))
84       nil)))
85
86 (put 'riece-ignore-errors 'lisp-indent-function 1)
87 (put 'riece-ignore-errors 'edebug-form-spec '(form body))
88
89 (defun riece-funcall-ignore-errors (location function &rest args)
90   "Call FUNCTION with ARGS; if an error occurs, return nil.
91 Otherwise, return result of the function.
92 If `riece-debug' is non-nil and an error occurred, it sends a
93 backtrace to standard-output."
94   (riece-ignore-errors location
95     (apply function args)))
96
97 (provide 'riece-debug)
98
99 ;;; riece-debug.el ends here