Fixed.
[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-1 (message detail)
31   (message "riece-debug: %s" message)
32   (save-excursion
33     (set-buffer riece-debug-buffer)
34     (goto-char (point-max))
35     (let ((time (format-time-string "%Y-%m-%d:%H:%M:%S")))
36       (if detail
37           (insert "*** " time ": " message "\n" detail "\n")
38         (insert "*** " time ": " message "\n")))))
39
40 (defun riece-debug (message &optional detail)
41   "Print a one-line debug MESSAGE at the bottom of the frame.
42 If the optional 2nd argument DETAIL is specified, it is stored into
43 `riece-debug-buffer'."
44   (ignore (riece-debug-1 message detail)))
45
46 (defun riece-debug-reset-standard-output ()
47   "Reset `riece-temp-buffer' to be used as `standard-output'."
48   (save-excursion
49     (set-buffer riece-temp-buffer)
50     (buffer-disable-undo)
51     (erase-buffer)))
52
53 (defmacro riece-debug-with-backtrace (&rest body)
54   "Execute BODY and send a backtrace to `riece-temp-buffer'."
55   `(unwind-protect
56        (progn ,@body)
57      (riece-debug-reset-standard-output)
58      (let ((standard-output riece-temp-buffer))
59        (backtrace))))
60
61 (put 'riece-debug-with-backtrace 'lisp-indent-function 0)
62 (put 'riece-debug-with-backtrace 'edebug-form-spec '(form body))
63
64 (defmacro riece-ignore-errors (location &rest body)
65   "Execute BODY; if an error occurs, return nil.
66 Otherwise, return result of last FORM.
67 If `riece-debug' is non-nil and an error occurred, it sends a
68 backtrace to standard-output."
69   `(condition-case error
70        (if riece-debug
71            (riece-debug-with-backtrace ,@body)
72          ,@body)
73      (error
74       (if riece-debug
75           (save-excursion
76             (set-buffer riece-temp-buffer)
77             (goto-char (point-min))
78             (if (re-search-forward "^  signal(" nil t)
79                 (delete-region (point-min) (match-beginning 0)))
80             (riece-debug (format "Error in `%s': %S" ,location error)
81                          (buffer-string))))
82       nil)))
83
84 (put 'riece-ignore-errors 'lisp-indent-function 1)
85 (put 'riece-ignore-errors 'edebug-form-spec '(form body))
86
87 (defun riece-funcall-ignore-errors (location function &rest args)
88   "Call FUNCTION with ARGS; if an error occurs, return nil.
89 Otherwise, return result of the function.
90 If `riece-debug' is non-nil and an error occurred, it sends a
91 backtrace to standard-output."
92   (riece-ignore-errors location
93     (apply function args)))
94
95 (provide 'riece-debug)
96
97 ;;; riece-debug.el ends here