summaryrefslogtreecommitdiff
path: root/elisp/geiser-log.el
blob: 6444335b226a96dae146d0778d9cfe4b3e7e72a2 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;; geiser-log.el -- logging utilities

;; Copyright (C) 2009, 2010, 2012, 2019 Jose Antonio Ortega Ruiz

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.

;; Start date: Sat Feb 07, 2009 12:07

;;; Commentary:

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


;;; Code:

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

(require 'comint)


;;; Customization:

(geiser-custom--defcustom geiser-log-autoscroll-buffer-p nil
  "Set this so than the buffer *geiser messages* always shows the last message"
  :group 'geiser
  :type 'boolean)

(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 2048
  "Maximum size of individual Geiser log messages.")

(defvar geiser-log-verbose-p nil
  "Log purely informational messages. Useful for debugging.")

(defvar geiser-log-verbose-debug-p nil
  "Log very verbose informational messages. Useful only for debugging.")


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


;;; Log buffer and mode:

(defvar geiser-messages-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "c" 'geiser-log-clear)
    (define-key map "Q" 'geiser-log--deactivate)
    map))

(define-derived-mode geiser-messages-mode fundamental-mode "Geiser Messages"
  "Simple mode for Geiser log messages buffer."
  (buffer-disable-undo)
  (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)
  ;; Maybe this feature would better be implemented as a revert-buffer function?
  (add-hook 'after-change-functions
            '(lambda (b e len)
               (when geiser-log-autoscroll-buffer-p
                 (let ((my-window (get-buffer-window (geiser-log--buffer) t)))
                   (when (window-live-p my-window)
                     (set-window-point my-window (point))))))
            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
      (goto-char (point-max))
      (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) ""))

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


;;; User commands:

(defun geiser-show-logs (&optional arg)
  "Show Geiser log messages.
With prefix, activates all logging levels."
  (interactive "P")
  (when arg (setq geiser-log-verbose-p t))
  (geiser-log--pop-to-buffer))

(defun geiser-log-clear ()
  "Clean all logs."
  (interactive)
  (geiser-log--with-buffer (delete-region (point-min) (point-max))))

(defun geiser-log-toggle-verbose ()
  "Toggle verbose logs"
  (interactive)
  (setq geiser-log-verbose-p (not geiser-log-verbose-p))
  (message "Geiser verbose logs %s"
           (if geiser-log-verbose-p "enabled" "disabled")))

(defun geiser-log--deactivate ()
  (interactive)
  (setq geiser-log-verbose-p nil)
  (when (eq (current-buffer) (geiser-log--buffer)) (View-quit)))


(provide 'geiser-log)