summaryrefslogtreecommitdiff
path: root/elisp/geiser-log.el
blob: 68e0fae8af3d8ee2c3d57d284a5062660514ac80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
;; geiser-log.el -- logging utilities

;; Copyright (C) 2009 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sat Feb 07, 2009 12:07

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Comentary:

;; Some utilities for maintaining a simple log buffer, mainly for
;; debugging purposes.

;;; Code:

(require 'geiser-popup)
(require 'geiser-base)


;;; Customization:

(defvar geiser-log--buffer-name "*geiser messages*"
  "Name of the Geiser log buffer.")

(defvar geiser-log--max-buffer-size 32000
  "Maximum size of the Geiser messages log.")

(defvar geiser-log--max-message-size 512
  "Maximum size of individual Geiser log messages.")

(defvar geiser-log--verbose-p t
  "Log level for Geiser messages")

(defvar geiser-log--inhibit-p nil
  "Set this to t to inhibit all log messages")


;;; Log buffer and mode:

(define-derived-mode geiser-messages-mode fundamental-mode "Geiser Messages"
  "Simple mode for Geiser log messages buffer."
  (kill-all-local-variables)
  (buffer-disable-undo)
  (set (make-local-variable 'comint-redirect-subvert-readonly) t)
  (add-hook 'after-change-functions
            '(lambda (b e len)
               (let ((inhibit-read-only t))
                 (when (> b geiser-log--max-buffer-size)
                   (delete-region (point-min) b))))
            nil t)
  (setq buffer-read-only t))

(geiser-popup--define log geiser-log--buffer-name geiser-messages-mode)


;;; Logging functions:

(defun geiser-log--msg (type &rest args)
  (unless geiser-log--inhibit-p
    (geiser-log--with-buffer
      (insert (geiser--shorten-str (format "\n%s: %s\n" type (apply 'format args))
                                   geiser-log--max-message-size)))))

(defsubst geiser-log--warn (&rest args)
  (apply 'geiser-log--msg 'WARNING args))

(defsubst geiser-log--error (&rest args)
  (apply 'geiser-log--msg 'ERROR args))

(defsubst geiser-log--info (&rest args)
  (when geiser-log--verbose-p
    (apply 'geiser-log--msg 'INFO args) ""))


;;; User commands:

(defun geiser-show-logs ()
  "Show Geiser log messages."
  (interactive)
  (geiser-log--pop-to-buffer))


(provide 'geiser-log)
;;; geiser-log.el ends here