* Makefile.am (EXTRA_DIST): Add riece-debug.el.
[elisp/riece.git] / 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 (defvar riece-debug-standard-output
28   (make-string 4096 ?\x0))
29
30 (defvar riece-debug-standard-output-index 0)
31
32 (defun riece-debug-standard-output (character)
33   (let ((length (length riece-debug-standard-output)))
34     (if (= riece-debug-standard-output-index length)
35         (setq riece-debug-standard-output
36               (concat riece-debug-standard-output
37                       (make-string length ?\x0))))
38     (aset riece-debug-standard-output
39           riece-debug-standard-output-index
40           character)
41     (setq riece-debug-standard-output-index
42           (1+ riece-debug-standard-output-index))))
43
44 (defmacro riece-debug-with-backtrace (&rest body)
45   `(unwind-protect
46        (progn ,@body)
47      (setq riece-debug-standard-output-index 0)
48      (let ((standard-output #'riece-debug-standard-output))
49        (backtrace))))
50
51 (put 'riece-debug-with-backtrace 'lisp-indent-function 0)
52 (put 'riece-debug-with-backtrace 'edebug-form-spec '(form body))
53
54 (defmacro riece-ignore-errors (location &rest body)
55   `(condition-case error
56        (if riece-debug
57            (riece-debug-with-backtrace ,@body)
58          ,@body)
59      (error
60       (if riece-debug
61           (let ((backtrace (substring riece-debug-standard-output
62                                       0 riece-debug-standard-output-index)))
63             (if (string-match "^  signal(" backtrace)
64                 (setq backtrace (substring backtrace 0 (match-beginning 0))))
65             (message "Error in `%s': %S\n%s" ,location error backtrace)))
66       nil)))
67
68 (put 'riece-ignore-errors 'lisp-indent-function 1)
69 (put 'riece-ignore-errors 'edebug-form-spec '(form body))
70
71 (provide 'riece-debug)
72
73 ;;; riece-debug.el ends here